diff options
Diffstat (limited to '')
| -rw-r--r-- | README.md | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/README.md b/README.md new file mode 100644 index 0000000..b678b87 --- /dev/null +++ b/README.md | |||
| @@ -0,0 +1,98 @@ | |||
| 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 | |||
| 13 | const Clap = clap.Clap; | ||
| 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 | |||
| 76 | const options = try parser.parse(args[1..]); | ||
| 77 | |||
| 78 | if (options.print_values) { | ||
| 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 | |||
| 86 | Running example: | ||
| 87 | ``` | ||
| 88 | sample % ./sample [0] | ||
| 89 | sample % ./sample -a 2 -b 4 -c 6 [0] | ||
| 90 | sample % ./sample -a 2 -b 4 -c 6 -p [0] | ||
| 91 | a = 2 | ||
| 92 | b = 4 | ||
| 93 | c = 6 | ||
| 94 | sample % ./sample -a 2 -b 4 -c 6 --print-values [0] | ||
| 95 | a = 2 | ||
| 96 | b = 4 | ||
| 97 | c = 6 | ||
| 98 | ``` | ||