diff options
| author | 2018-04-26 23:15:35 +0200 | |
|---|---|---|
| committer | 2018-04-26 23:15:35 +0200 | |
| commit | 2519d584de3cba147c3c8c2469aa9fcd944589aa (patch) | |
| tree | 358af78a3f5fd7242253370ed7f637a38574c97f | |
| parent | Refactored clap.zig (diff) | |
| download | zig-clap-2519d584de3cba147c3c8c2469aa9fcd944589aa.tar.gz zig-clap-2519d584de3cba147c3c8c2469aa9fcd944589aa.tar.xz zig-clap-2519d584de3cba147c3c8c2469aa9fcd944589aa.zip | |
Added updated example and README
Diffstat (limited to '')
| -rw-r--r-- | README.md | 104 | ||||
| -rw-r--r-- | example.zig | 79 |
2 files changed, 90 insertions, 93 deletions
| @@ -1,98 +1,16 @@ | |||
| 1 | # zig-clap | 1 | # zig-clap |
| 2 | A non allocating, fast and easy to use command line argument parser library for Zig. | ||
| 3 | |||
| 4 | # Example | ||
| 5 | |||
| 6 | ``` | ||
| 7 | const std = @import("std"); | ||
| 8 | const clap = @import("clap.zig"); | ||
| 9 | |||
| 10 | const debug = std.debug; | ||
| 11 | const os = std.os; | ||
| 12 | 2 | ||
| 13 | const Clap = clap.Clap; | 3 | A non allocating, fast and easy to use command line argument parser library for Zig. |
| 14 | const Command = clap.Command; | ||
| 15 | const Argument = clap.Argument; | ||
| 16 | |||
| 17 | const Options = struct { | ||
| 18 | print_values: bool, | ||
| 19 | a: i64, | ||
| 20 | b: u64, | ||
| 21 | c: u8, | ||
| 22 | }; | ||
| 23 | |||
| 24 | pub fn main() !void { | ||
| 25 | const parser = comptime Clap(Options).Builder | ||
| 26 | .init( | ||
| 27 | Options { | ||
| 28 | .print_values = false, | ||
| 29 | .a = 0, | ||
| 30 | .b = 0, | ||
| 31 | .c = 0, | ||
| 32 | } | ||
| 33 | ) | ||
| 34 | .programName("My Test Command") | ||
| 35 | .author("Hejsil") | ||
| 36 | .version("v1") | ||
| 37 | .about("Prints some values to the screen... Maybe.") | ||
| 38 | .command( | ||
| 39 | Command.Builder | ||
| 40 | .init("command") | ||
| 41 | .arguments( | ||
| 42 | []Argument { | ||
| 43 | Argument.Builder | ||
| 44 | .init("a") | ||
| 45 | .help("Set the a field of Option.") | ||
| 46 | .short('a') | ||
| 47 | .takesValue(true) | ||
| 48 | .build(), | ||
| 49 | Argument.Builder | ||
| 50 | .init("b") | ||
| 51 | .help("Set the b field of Option.") | ||
| 52 | .short('b') | ||
| 53 | .takesValue(true) | ||
| 54 | .build(), | ||
| 55 | Argument.Builder | ||
| 56 | .init("c") | ||
| 57 | .help("Set the c field of Option.") | ||
| 58 | .short('c') | ||
| 59 | .takesValue(true) | ||
| 60 | .build(), | ||
| 61 | Argument.Builder | ||
| 62 | .init("print_values") | ||
| 63 | .help("Print all not 0 values.") | ||
| 64 | .short('p') | ||
| 65 | .long("print-values") | ||
| 66 | .build(), | ||
| 67 | } | ||
| 68 | ) | ||
| 69 | .build() | ||
| 70 | ) | ||
| 71 | .build(); | ||
| 72 | |||
| 73 | const args = try os.argsAlloc(debug.global_allocator); | ||
| 74 | defer os.argsFree(debug.global_allocator, args); | ||
| 75 | 4 | ||
| 76 | const options = try parser.parse(args[1..]); | 5 | ## Features |
| 77 | 6 | ||
| 78 | if (options.print_values) { | 7 | See [example](example.zig). |
| 79 | if (options.a != 0) debug.warn("a = {}\n", options.a); | ||
| 80 | if (options.b != 0) debug.warn("b = {}\n", options.b); | ||
| 81 | if (options.c != 0) debug.warn("c = {}\n", options.c); | ||
| 82 | } | ||
| 83 | } | ||
| 84 | ``` | ||
| 85 | 8 | ||
| 86 | Running example: | 9 | * Assosiate arguements with fields of a struct. |
| 87 | ``` | 10 | * When an arguement is found by the clap, the field is set to a parsed version of the value. |
| 88 | sample % ./sample [0] | 11 | * `zig-clap` provides some default parses, but you can make your own as well. |
| 89 | sample % ./sample -a 2 -b 4 -c 6 [0] | 12 | * Short arguments `-a` |
| 90 | sample % ./sample -a 2 -b 4 -c 6 -p [0] | 13 | * Chaining `-abc` where `a` and `b` does not take values. |
| 91 | a = 2 | 14 | * Long arguments `--long` |
| 92 | b = 4 | 15 | * Supports both passing values using spacing and `=` (`-a 100`, `-a=100`) |
| 93 | c = 6 | 16 | * Both work with chaining (`-ba 100`, `-ba=100`) |
| 94 | sample % ./sample -a 2 -b 4 -c 6 --print-values [0] | ||
| 95 | a = 2 | ||
| 96 | b = 4 | ||
| 97 | c = 6 | ||
| 98 | ``` | ||
diff --git a/example.zig b/example.zig new file mode 100644 index 0000000..c77ef5c --- /dev/null +++ b/example.zig | |||
| @@ -0,0 +1,79 @@ | |||
| 1 | const std = @import("std"); | ||
| 2 | const clap = @import("clap.zig"); | ||
| 3 | |||
| 4 | const debug = std.debug; | ||
| 5 | const os = std.os; | ||
| 6 | |||
| 7 | const Clap = clap.Clap; | ||
| 8 | const Command = clap.Command; | ||
| 9 | const Argument = clap.Argument; | ||
| 10 | |||
| 11 | const Options = struct { | ||
| 12 | print_values: bool, | ||
| 13 | a: i64, | ||
| 14 | b: u64, | ||
| 15 | c: u8, | ||
| 16 | d: []const u8, | ||
| 17 | }; | ||
| 18 | |||
| 19 | // Output on windows: | ||
| 20 | // zig-clap> .\example.exe -a 1 | ||
| 21 | // zig-clap> .\example.exe -p -a 1 | ||
| 22 | // a = 1 | ||
| 23 | // zig-clap> .\example.exe -pa 1 | ||
| 24 | // a = 1 | ||
| 25 | // zig-clap> .\example.exe -pd friend | ||
| 26 | // d = friend | ||
| 27 | // zig-clap> .\example.exe -pd=friend | ||
| 28 | // d = friend | ||
| 29 | // zig-clap> .\example.exe -p -d=friend | ||
| 30 | // d = friend | ||
| 31 | pub fn main() !void { | ||
| 32 | const parser = comptime Clap(Options).init( | ||
| 33 | Options { | ||
| 34 | .print_values = false, | ||
| 35 | .a = 0, | ||
| 36 | .b = 0, | ||
| 37 | .c = 0, | ||
| 38 | .d = "", | ||
| 39 | } | ||
| 40 | ) | ||
| 41 | .with("program_name", "My Test Command") | ||
| 42 | .with("author", "Hejsil") | ||
| 43 | .with("version", "v1") | ||
| 44 | .with("about", "Prints some values to the screen... Maybe.") | ||
| 45 | .with("command", Command.init("command") | ||
| 46 | .with("arguments", | ||
| 47 | []Argument { | ||
| 48 | Argument.arg("a") | ||
| 49 | .with("help", "Set the a field of Option.") | ||
| 50 | .with("takes_value", clap.parse.int(i64, 10)), | ||
| 51 | Argument.arg("b") | ||
| 52 | .with("help", "Set the b field of Option.") | ||
| 53 | .with("takes_value", clap.parse.int(u64, 10)), | ||
| 54 | Argument.arg("c") | ||
| 55 | .with("help", "Set the c field of Option.") | ||
| 56 | .with("takes_value", clap.parse.int(u8, 10)), | ||
| 57 | Argument.arg("d") | ||
| 58 | .with("help", "Set the d field of Option.") | ||
| 59 | .with("takes_value", clap.parse.string), | ||
| 60 | Argument.field("print_values") | ||
| 61 | .with("help", "Print all not 0 values.") | ||
| 62 | .with("short", 'p') | ||
| 63 | .with("long", "print-values"), | ||
| 64 | } | ||
| 65 | ) | ||
| 66 | ); | ||
| 67 | |||
| 68 | const args = try os.argsAlloc(debug.global_allocator); | ||
| 69 | defer os.argsFree(debug.global_allocator, args); | ||
| 70 | |||
| 71 | const options = try parser.parse(args[1..]); | ||
| 72 | |||
| 73 | if (options.print_values) { | ||
| 74 | if (options.a != 0) debug.warn("a = {}\n", options.a); | ||
| 75 | if (options.b != 0) debug.warn("b = {}\n", options.b); | ||
| 76 | if (options.c != 0) debug.warn("c = {}\n", options.c); | ||
| 77 | if (options.d.len != 0) debug.warn("d = {}\n", options.d); | ||
| 78 | } | ||
| 79 | } | ||