A simple command line parser.
var args = try zul.CommandLineArgs.parse(allocator);
defer args.deinit();
if (args.contains("version")) {
os.exit(0);
}
const host = args.get("host") orelse "127.0.0.1";
...
zul.CommandLineArgs
is a thin wrapper around std.process.argsWithAllocator
which applies simple logic to parse key=value pairs into a StringHashMap([]const u8
.
7 argument types are supported:
--key value
-k value
--key=value
-k=value
--key
-k
-xvf value
A key without a value, such as ./run --version
or ./run -v
will be given a empty string value (""
).
Parsing is simple. Keys begin with one or two dashes. If the parser sees ./run --key1 --key2 value2
, it will load "key1" with a value of "", and "key2" with a value of "value2".
Single-dash keys can be grouped together, with the last key being given the value, so parameters like ./run -xvf file.tar.gz
work like you (probably) expect.
Once the parser runs into a non key+value combo, all following arguments are treated as a "tail", which is a list of []const u8
exe: []const u8
The first command line argument is the path to the running executable.
tail: [][]const u8
A list of arguments starting from the first non key+value pair.
list: [][]const u8
A list of arguments. The first argument in the list is the path to the running executable. This is an exact collection of std.process.argsWithAllocator
.
count(self: *const CommandLineArgs) u32
The number of key value pairs. args.count() + args.tail.len + 1 == args.list.len
. The + 1
is for the path to the running executable which is included in list
.