-
-
Notifications
You must be signed in to change notification settings - Fork 3k
[std] add minimal CLI parser std.cli
#24881
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Conversation
/// --seed=0x<something> is actually passed in by the `zig test` system (as of 0.14.1), which we receive here. | ||
seed: u32 = 0, | ||
@"cache-dir": []const u8 = "", | ||
listen: []const u8 = "", |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this is questionable.
|
||
fn checkArgsType(comptime Args: type) void { | ||
const args_fields = @typeInfo(Args).@"struct".fields; | ||
if (!(args_fields.len == 2 and mem.eql(u8, args_fields[0].name, "named") and mem.eql(u8, args_fields[1].name, "positional"))) @compileError("expected Args to have exactly these fields in this order: named, positional"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
requiring the order here seems questionable maybe.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It is. People WILL stumble upon it since they (understandably) think of structs more similar to a hashmap than an array.
But using std.sort.block
and std.sort.binarySearch
(there is no other search function in the standard library where you can define the predicate afaik) could help here.
I recommend giving it a trial run by porting all the files in tools/ to use this. You can test at least that those compile successfully with |
Currently, just josh@nixos:~/dev/zig$ zig run --zig-lib-dir lib/ tools/docgen.zig -- --help
Usage: docgen [options] input output
Generates an HTML document from a docgen template.
Options:
--code-dir dir Path to directory containing code example outputs
--help Print this help and exit
error: Help
/home/josh/dev/zig/lib/std/cli.zig:275:13: 0x118c7af in innerParse__anon_25090 (std.zig)
return error.Help;
^
/home/josh/dev/zig/lib/std/cli.zig:159:5: 0x118399b in parseIter__anon_24026 (std.zig)
return innerParse(Args, arena, iter, prog, options.writer);
^
/home/josh/dev/zig/lib/std/cli.zig:115:5: 0x1169426 in parse__anon_22563 (std.zig)
return parseIter(Args, arena, &iter, options);
^
/home/josh/dev/zig/tools/docgen.zig:41:18: 0x1166557 in main (docgen.zig)
const args = try std.cli.parse(Args, arena, .{});
^ This appears to be done unconditionally for all error codes: Lines 638 to 640 in 623290e
Is there any appetite for special casing the cli errors If we don't want to modify |
sounds like #24510 |
Just my 2c, but if I propagated
I think that would be the simplest, most local solution. |
EDIT: |
I'm no longer calling |
Closes #24601