A simple command line parser.

var args = try zul.CommandLineArgs.parse(allocator);
defer args.deinit();

if (args.contains("version")) {
	//todo: print the version
	os.exit(0);
}

// Values retrieved from args.get are valid until args.deinit()
// is called. Dupe the value if needed.
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:

  1. --key value
  2. -k value
  3. --key=value
  4. -k=value
  5. --key
  6. -k
  7. -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.

Parses the command line arguments This can only fail in exceptional cases (e.g. out of memory).

Releases all memory related to the parsing. This includes any string returned by get or in the list or tail slice.

Gets the value associated with the key. The returned value's lifetime is tied to the CommandLineArg.

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.