2020-03-05 05:47:34 +08:00
|
|
|
# Zig Argument Parser
|
2020-03-05 05:34:01 +08:00
|
|
|
Simple-to-use argument parser with struct-based config
|
2020-03-05 05:38:26 +08:00
|
|
|
|
2020-03-05 05:47:34 +08:00
|
|
|
## Features
|
|
|
|
- Automatic option generation from a config struct
|
|
|
|
- Familiar *look & feel*:
|
|
|
|
- Everything after the first `--` is assumed to be a positional argument
|
|
|
|
- A single `-` is interpreted as a positional argument which can be used as the stdin/stdout file placeholder
|
|
|
|
- Short options with no argument can be combined into a single argument: `-dfe`
|
2020-03-05 05:58:12 +08:00
|
|
|
- Long options can use either `--option=value` or `--option value` syntax
|
2020-03-05 05:47:34 +08:00
|
|
|
- Integrated support for primitive types:
|
|
|
|
- All integer types (signed & unsigned)
|
|
|
|
- Floating point types
|
2020-03-05 21:51:43 +08:00
|
|
|
- Booleans (takes optional argument. If no argument given, the bool is set, otherwise, one of `yes`, `true`, `y`, `no`, `false`, `n` is interpreted)
|
2020-03-05 05:47:34 +08:00
|
|
|
- Strings
|
2020-03-05 21:31:35 +08:00
|
|
|
- Enumerations
|
2020-03-05 05:47:34 +08:00
|
|
|
|
|
|
|
## Example
|
|
|
|
|
2020-03-05 05:38:26 +08:00
|
|
|
```zig
|
2021-05-12 16:25:14 +08:00
|
|
|
const options = argsParser.parse(struct {
|
2020-03-05 05:38:26 +08:00
|
|
|
// This declares long options for double hyphen
|
|
|
|
output: ?[]const u8 = null,
|
|
|
|
@"with-offset": bool = false,
|
|
|
|
@"with-hexdump": bool = false,
|
|
|
|
@"intermix-source": bool = false,
|
|
|
|
numberOfBytes: ?i32 = null,
|
|
|
|
|
|
|
|
// This declares short-hand options for single hyphen
|
|
|
|
pub const shorthands = .{
|
|
|
|
.S = "intermix-source",
|
|
|
|
.b = "with-hexdump",
|
|
|
|
.O = "with-offset",
|
|
|
|
.o = "output",
|
|
|
|
};
|
2021-05-12 16:25:14 +08:00
|
|
|
}, &args, argsAllocator, print) catch return 1;
|
2020-03-05 05:38:26 +08:00
|
|
|
defer options.deinit();
|
|
|
|
|
|
|
|
std.debug.warn("parsed result:\n{}\npositionals:\n", .{options.options});
|
2020-05-26 01:41:36 +08:00
|
|
|
for (options.positionals) |arg| {
|
2020-03-05 05:38:26 +08:00
|
|
|
std.debug.warn("\t{}\n", .{arg});
|
|
|
|
}
|
2020-05-26 01:41:36 +08:00
|
|
|
```
|