From 2519d584de3cba147c3c8c2469aa9fcd944589aa Mon Sep 17 00:00:00 2001 From: Jimmi Holst Christensen Date: Thu, 26 Apr 2018 23:15:35 +0200 Subject: Added updated example and README --- README.md | 104 +++++++----------------------------------------------------- example.zig | 79 +++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 93 deletions(-) create mode 100644 example.zig diff --git a/README.md b/README.md index b678b87..b90f5cd 100644 --- a/README.md +++ b/README.md @@ -1,98 +1,16 @@ # zig-clap -A non allocating, fast and easy to use command line argument parser library for Zig. - -# Example - -``` -const std = @import("std"); -const clap = @import("clap.zig"); - -const debug = std.debug; -const os = std.os; -const Clap = clap.Clap; -const Command = clap.Command; -const Argument = clap.Argument; - -const Options = struct { - print_values: bool, - a: i64, - b: u64, - c: u8, -}; - -pub fn main() !void { - const parser = comptime Clap(Options).Builder - .init( - Options { - .print_values = false, - .a = 0, - .b = 0, - .c = 0, - } - ) - .programName("My Test Command") - .author("Hejsil") - .version("v1") - .about("Prints some values to the screen... Maybe.") - .command( - Command.Builder - .init("command") - .arguments( - []Argument { - Argument.Builder - .init("a") - .help("Set the a field of Option.") - .short('a') - .takesValue(true) - .build(), - Argument.Builder - .init("b") - .help("Set the b field of Option.") - .short('b') - .takesValue(true) - .build(), - Argument.Builder - .init("c") - .help("Set the c field of Option.") - .short('c') - .takesValue(true) - .build(), - Argument.Builder - .init("print_values") - .help("Print all not 0 values.") - .short('p') - .long("print-values") - .build(), - } - ) - .build() - ) - .build(); - - const args = try os.argsAlloc(debug.global_allocator); - defer os.argsFree(debug.global_allocator, args); +A non allocating, fast and easy to use command line argument parser library for Zig. - const options = try parser.parse(args[1..]); +## Features - if (options.print_values) { - if (options.a != 0) debug.warn("a = {}\n", options.a); - if (options.b != 0) debug.warn("b = {}\n", options.b); - if (options.c != 0) debug.warn("c = {}\n", options.c); - } -} -``` +See [example](example.zig). -Running example: -``` -sample % ./sample [0] -sample % ./sample -a 2 -b 4 -c 6 [0] -sample % ./sample -a 2 -b 4 -c 6 -p [0] -a = 2 -b = 4 -c = 6 -sample % ./sample -a 2 -b 4 -c 6 --print-values [0] -a = 2 -b = 4 -c = 6 -``` +* Assosiate arguements with fields of a struct. + * When an arguement is found by the clap, the field is set to a parsed version of the value. + * `zig-clap` provides some default parses, but you can make your own as well. +* Short arguments `-a` + * Chaining `-abc` where `a` and `b` does not take values. +* Long arguments `--long` +* Supports both passing values using spacing and `=` (`-a 100`, `-a=100`) + * Both work with chaining (`-ba 100`, `-ba=100`) diff --git a/example.zig b/example.zig new file mode 100644 index 0000000..c77ef5c --- /dev/null +++ b/example.zig @@ -0,0 +1,79 @@ +const std = @import("std"); +const clap = @import("clap.zig"); + +const debug = std.debug; +const os = std.os; + +const Clap = clap.Clap; +const Command = clap.Command; +const Argument = clap.Argument; + +const Options = struct { + print_values: bool, + a: i64, + b: u64, + c: u8, + d: []const u8, +}; + +// Output on windows: +// zig-clap> .\example.exe -a 1 +// zig-clap> .\example.exe -p -a 1 +// a = 1 +// zig-clap> .\example.exe -pa 1 +// a = 1 +// zig-clap> .\example.exe -pd friend +// d = friend +// zig-clap> .\example.exe -pd=friend +// d = friend +// zig-clap> .\example.exe -p -d=friend +// d = friend +pub fn main() !void { + const parser = comptime Clap(Options).init( + Options { + .print_values = false, + .a = 0, + .b = 0, + .c = 0, + .d = "", + } + ) + .with("program_name", "My Test Command") + .with("author", "Hejsil") + .with("version", "v1") + .with("about", "Prints some values to the screen... Maybe.") + .with("command", Command.init("command") + .with("arguments", + []Argument { + Argument.arg("a") + .with("help", "Set the a field of Option.") + .with("takes_value", clap.parse.int(i64, 10)), + Argument.arg("b") + .with("help", "Set the b field of Option.") + .with("takes_value", clap.parse.int(u64, 10)), + Argument.arg("c") + .with("help", "Set the c field of Option.") + .with("takes_value", clap.parse.int(u8, 10)), + Argument.arg("d") + .with("help", "Set the d field of Option.") + .with("takes_value", clap.parse.string), + Argument.field("print_values") + .with("help", "Print all not 0 values.") + .with("short", 'p') + .with("long", "print-values"), + } + ) + ); + + const args = try os.argsAlloc(debug.global_allocator); + defer os.argsFree(debug.global_allocator, args); + + const options = try parser.parse(args[1..]); + + if (options.print_values) { + if (options.a != 0) debug.warn("a = {}\n", options.a); + if (options.b != 0) debug.warn("b = {}\n", options.b); + if (options.c != 0) debug.warn("c = {}\n", options.c); + if (options.d.len != 0) debug.warn("d = {}\n", options.d); + } +} -- cgit v1.2.3