Skip to content

Commit

Permalink
init subcmd (#23)
Browse files Browse the repository at this point in the history
* init subcmd

* finish

* fix ci

* fix simargs ci

* fix version
  • Loading branch information
jiacai2050 authored Aug 11, 2024
1 parent c5e9884 commit f7f0658
Show file tree
Hide file tree
Showing 8 changed files with 531 additions and 222 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/simargs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,4 +35,4 @@ jobs:
TEST_BINARY=./zig-out/bin/simargs-demo
valgrind --leak-check=full --tool=memcheck \
--show-leak-kinds=all --error-exitcode=1 ${TEST_BINARY} --output a.out \
hello world
sub1 --a 123 hello world
2 changes: 1 addition & 1 deletion .tool-versions
Original file line number Diff line number Diff line change
@@ -1 +1 @@
zig master
zig 0.13.0
28 changes: 17 additions & 11 deletions docs/content/docs/modules/simargs.org
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#+TITLE: Simargs
#+DATE: 2023-10-21T12:04:40+0800
#+LASTMOD: 2023-10-22T19:30:17+0800
#+LASTMOD: 2024-08-11T20:30:59+0800
#+WEIGTH: 1

* Simargs
Expand All @@ -12,41 +12,47 @@ A simple, opinionated, struct-based argument parser in Zig, taking full advantag
- =Enum=
- Optional fields and fields with default value mean they are optional arguments
- Use =comptime= as possible as I can
- Provide =print_help()= out of the box
- Provide =printHelp()= out of the box
- Support sub commands
* Usage
See [[https://github.com/jiacai2050/zigcli/blob/main/examples/simargs-demo.zig][simargs-demo.zig]].

#+begin_src bash :results verbatim :exports both
# Run demo
zig build && ./zig-out/bin/simargs-demo -o /tmp/a.out --user-agent Firefox hello world 2>&1
zig build run-simargs-demo -- -o /tmp/a.out --user-agent Firefox sub1 --a 123 hello world 2>&1
#+end_src

#+RESULTS:
#+begin_example
------------------------------Program------------------------------
./zig-out/bin/simargs-demo
/Users/jiacai/gh/zigcli/.zig-cache/o/bd8a4fb104779110e787d579f1d9c6f0/simargs-demo

------------------------------Arguments------------------------------
verbose: null
user-agent: demo.main__struct_1677.main__struct_1677__enum_1777.Firefox
user-agent: simargs-demo.main__struct_1700.main__struct_1700__enum_1707.Firefox
timeout: 30
output: /tmp/a.out
help: false
__commands__: simargs-demo.main__struct_1700.main__struct_1700__union_1708{ .sub1 = simargs-demo.main__struct_1700.main__struct_1700__union_1708.main__struct_1700__union_1708__struct_1710{ .a = 123, .help = false } }

------------------------------Positionals------------------------------
1: hello
2: world

------------------------------print_help------------------------------
USAGE:
./zig-out/bin/demo [OPTIONS] [--] [file]
/Users/jiacai/gh/zigcli/.zig-cache/o/bd8a4fb104779110e787d579f1d9c6f0/simargs-demo [OPTIONS] [COMMANDS]

COMMANDS:
sub1 Subcommand 1
sub2 Subcommand 2

OPTIONS:
-v, --verbose Make the operation more talkative
-A, --user-agent STRING (valid: Chrome|Firefox|Safari)(default: Firefox)
--timeout INTEGER Max time this request can cost(default: 30)
-o, --output STRING Write to file instead of stdout(required)
-h, --help
-v, --verbose Make the operation more talkative
-A, --user-agent STRING (valid: Chrome|Firefox|Safari)(default: Firefox)
--timeout INTEGER Max time this request can cost(default: 30)
-o, --output STRING Write to file instead of stdout(required)
-h, --help
#+end_example

* Acknowledgment
Expand Down
23 changes: 20 additions & 3 deletions examples/simargs-demo.zig
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,23 @@ pub fn main() !void {
timeout: ?u16 = 30, // default value
output: []const u8,
help: bool = false,
version: bool = false,

// This special field define sub_commands,
// Each union item is a config struct, which is similar with top-level config struct.
__commands__: union(enum) {
sub1: struct {
a: u64,
help: bool = false,
},
sub2: struct { name: []const u8 },

// Define help message for sub commands.
pub const __messages__ = .{
.sub1 = "Subcommand 1",
.sub2 = "Subcommand 2",
};
},

// This declares option's short name
pub const __shorts__ = .{
Expand All @@ -33,7 +50,7 @@ pub fn main() !void {
.output = "Write to file instead of stdout",
.timeout = "Max time this request can cost",
};
}, "[file]", null);
}, "[file]", "0.1.0");
defer opt.deinit();

const sep = "-" ** 30;
Expand All @@ -49,12 +66,12 @@ pub fn main() !void {
}

std.debug.print("\n{s}Positionals{s}\n", .{ sep, sep });
for (opt.positional_args.items, 0..) |arg, idx| {
for (opt.positional_args, 0..) |arg, idx| {
std.debug.print("{d}: {s}\n", .{ idx + 1, arg });
}

// Provide a print_help util method
std.debug.print("\n{s}print_help{s}\n", .{ sep, sep });
const stdout = std.io.getStdOut();
try opt.print_help(stdout.writer());
try opt.printHelp(stdout.writer());
}
4 changes: 2 additions & 2 deletions src/bin/loc.zig
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,10 @@ pub fn main() !void {
}, "[file or directory]", util.get_build_info());
defer opt.deinit();

const file_or_dir = if (opt.positional_args.items.len == 0)
const file_or_dir = if (opt.positional_args.len == 0)
"."
else
opt.positional_args.items[0];
opt.positional_args[0];

var loc_map = LocMap{};
const dir = fs.cwd().openDir(file_or_dir, .{ .iterate = true }) catch |err| switch (err) {
Expand Down
4 changes: 2 additions & 2 deletions src/bin/repeat.zig
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,9 @@ pub fn main() !void {
}, "command", util.get_build_info());
defer opt.deinit();

const argv = if (opt.positional_args.items.len == 0) {
const argv = if (opt.positional_args.len == 0) {
return error.NoCommand;
} else opt.positional_args.items;
} else opt.positional_args;

var keep_running = true;
var i: usize = 0;
Expand Down
4 changes: 2 additions & 2 deletions src/bin/tree.zig
Original file line number Diff line number Diff line change
Expand Up @@ -88,10 +88,10 @@ pub fn main() anyerror!void {
);
defer opt.deinit();

const root_dir = if (opt.positional_args.items.len == 0)
const root_dir = if (opt.positional_args.len == 0)
"."
else
opt.positional_args.items[0];
opt.positional_args[0];

var writer = std.io.bufferedWriter(std.io.getStdOut().writer());
_ = try writer.write(root_dir);
Expand Down
Loading

0 comments on commit f7f0658

Please sign in to comment.