I am happy to share that I have finally merged a PR to make zig-cli to bind command line options to struct fields. Before this PR zig-cli
only supported i64
, f64
, []const u8
, and bool
.
pub const OptionValue = union(enum) {
bool: bool,
string: ?[]const u8,
int: ?i64,
float: ?f64,
string_list: ?[]const []const u8,
};
After this merge, option values are bound to some value:
var config = struct {
host: []const u8 = "localhost",
port: u16 = undefined,
}{};
var host = cli.Option{
.long_name = "host",
.help = "host to listen on",
.value_ref = cli.mkRef(&config.host),
};
var port = cli.Option{
.long_name = "port",
.help = "port to bind to",
.required = true,
.value_ref = cli.mkRef(&config.port),
};
This approach has a few advantages:
- It separates the value used by the CLI tool from the CLI user interface configuration.
- It simplifies access to the provided command line options.
- It enables support for arbitrary option types, e.g. IP address, etc.
- It enables support for custom types.
The last two points are WIP.