summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md20
-rw-r--r--build.zig4
-rw-r--r--example/comptime-clap.zig4
-rw-r--r--example/streaming-clap.zig4
-rw-r--r--index.zig1
-rw-r--r--src/index.zig30
6 files changed, 31 insertions, 32 deletions
diff --git a/README.md b/README.md
index 2bc86a6..b64ab2b 100644
--- a/README.md
+++ b/README.md
@@ -15,14 +15,14 @@ A simple and easy to use command line argument parser library for Zig.
15 15
16### `StreamingClap` 16### `StreamingClap`
17 17
18The `StreamingClap` is base of all the other parsers. It's a streaming parser that uses an 18The `StreamingClap` is the base of all the other parsers. It's a streaming parser that uses an
19`args.Iterator` to provide it with arguments lazily. 19`args.Iterator` to provide it with arguments lazily.
20 20
21```rust 21```rust
22const params = []clap.Param(u8){ 22const params = []clap.Param(u8){
23 clap.Param(void).flag('h', false, clap.Names.prefix("help")), 23 clap.Param(u8).flag('h', clap.Names.both("help")),
24 clap.Param(void).option('n', true, clap.Names.prefix("number")), 24 clap.Param(u8).option('n', clap.Names.both("number")),
25 clap.Param(void).positional('f'), 25 clap.Param(u8).positional('f'),
26}; 26};
27 27
28var os_iter = clap.args.OsIterator.init(allocator); 28var os_iter = clap.args.OsIterator.init(allocator);
@@ -50,8 +50,8 @@ them available through three functions (`flag`, `option`, `positionals`).
50 50
51```rust 51```rust
52const params = comptime []clap.Param(void){ 52const params = comptime []clap.Param(void){
53 clap.Param(void).flag({}, false, clap.Names.prefix("help")), 53 clap.Param(void).flag({}, clap.Names.both("help")),
54 clap.Param(void).option({}, true, clap.Names.prefix("number")), 54 clap.Param(void).option({}, clap.Names.both("number")),
55 clap.Param(void).positional({}), 55 clap.Param(void).positional({}),
56}; 56};
57 57
@@ -78,7 +78,7 @@ program can take:
78 78
79```rust 79```rust
80const params = comptime []clap.Param(void){ 80const params = comptime []clap.Param(void){
81 clap.Param(void).init({}, false, clap.Names.prefix("help")), 81 clap.Param(void).flag({}, clap.Names.both("help")),
82}; 82};
83 83
84var os_iter = clap.args.OsIterator.init(allocator); 84var os_iter = clap.args.OsIterator.init(allocator);
@@ -106,7 +106,7 @@ zig-clap/example/comptime-clap.zig:41:18: note: called from here
106 ^ 106 ^
107``` 107```
108 108
109Ofc, this limits you to use only parameters that are comptime known. 109Ofc, this limits you to parameters that are comptime known.
110 110
111### `help` 111### `help`
112 112
@@ -123,11 +123,11 @@ try clap.help(
123 []clap.Param([]const u8){ 123 []clap.Param([]const u8){
124 clap.Param([]const u8).flag( 124 clap.Param([]const u8).flag(
125 "Display this help and exit.", 125 "Display this help and exit.",
126 clap.Names.prefix("help"), 126 clap.Names.both("help"),
127 ), 127 ),
128 clap.Param([]const u8).flag( 128 clap.Param([]const u8).flag(
129 "Output version information and exit.", 129 "Output version information and exit.",
130 clap.Names.prefix("version"), 130 clap.Names.both("version"),
131 ), 131 ),
132 }, 132 },
133); 133);
diff --git a/build.zig b/build.zig
index 06c012b..3ceba14 100644
--- a/build.zig
+++ b/build.zig
@@ -13,7 +13,7 @@ pub fn build(b: *Builder) void {
13 "streaming-clap", 13 "streaming-clap",
14 }) |example_name| { 14 }) |example_name| {
15 const example = b.addExecutable(example_name, "example/" ++ example_name ++ ".zig"); 15 const example = b.addExecutable(example_name, "example/" ++ example_name ++ ".zig");
16 example.addPackagePath("clap", "src/index.zig"); 16 example.addPackagePath("clap", "index.zig");
17 example.setBuildMode(mode); 17 example.setBuildMode(mode);
18 example_step.dependOn(&example.step); 18 example_step.dependOn(&example.step);
19 } 19 }
@@ -22,7 +22,7 @@ pub fn build(b: *Builder) void {
22 inline for ([]Mode{ Mode.Debug, Mode.ReleaseFast, Mode.ReleaseSafe, Mode.ReleaseSmall }) |test_mode| { 22 inline for ([]Mode{ Mode.Debug, Mode.ReleaseFast, Mode.ReleaseSafe, Mode.ReleaseSmall }) |test_mode| {
23 const mode_str = comptime modeToString(test_mode); 23 const mode_str = comptime modeToString(test_mode);
24 24
25 const tests = b.addTest("src/index.zig"); 25 const tests = b.addTest("index.zig");
26 tests.setBuildMode(test_mode); 26 tests.setBuildMode(test_mode);
27 tests.setNamePrefix(mode_str ++ " "); 27 tests.setNamePrefix(mode_str ++ " ");
28 28
diff --git a/example/comptime-clap.zig b/example/comptime-clap.zig
index 8517db8..8d2d8a8 100644
--- a/example/comptime-clap.zig
+++ b/example/comptime-clap.zig
@@ -16,11 +16,11 @@ pub fn main() !void {
16 const params = comptime []clap.Param([]const u8){ 16 const params = comptime []clap.Param([]const u8){
17 clap.Param([]const u8).flag( 17 clap.Param([]const u8).flag(
18 "Display this help and exit.", 18 "Display this help and exit.",
19 clap.Names.prefix("help") 19 clap.Names.both("help"),
20 ), 20 ),
21 clap.Param([]const u8).option( 21 clap.Param([]const u8).option(
22 "An option parameter, which takes a value.", 22 "An option parameter, which takes a value.",
23 clap.Names.prefix("number"), 23 clap.Names.both("number"),
24 ), 24 ),
25 clap.Param([]const u8).positional(""), 25 clap.Param([]const u8).positional(""),
26 }; 26 };
diff --git a/example/streaming-clap.zig b/example/streaming-clap.zig
index a52a222..aad3e71 100644
--- a/example/streaming-clap.zig
+++ b/example/streaming-clap.zig
@@ -10,8 +10,8 @@ pub fn main() !void {
10 10
11 // First we specify what parameters our program can take. 11 // First we specify what parameters our program can take.
12 const params = []clap.Param(u8){ 12 const params = []clap.Param(u8){
13 clap.Param(u8).flag('h', clap.Names.prefix("help")), 13 clap.Param(u8).flag('h', clap.Names.both("help")),
14 clap.Param(u8).option('n', clap.Names.prefix("number")), 14 clap.Param(u8).option('n', clap.Names.both("number")),
15 clap.Param(u8).positional('f'), 15 clap.Param(u8).positional('f'),
16 }; 16 };
17 17
diff --git a/index.zig b/index.zig
new file mode 100644
index 0000000..b867913
--- /dev/null
+++ b/index.zig
@@ -0,0 +1 @@
pub use @import("src/index.zig");
diff --git a/src/index.zig b/src/index.zig
index e85470c..40ad849 100644
--- a/src/index.zig
+++ b/src/index.zig
@@ -41,10 +41,10 @@ pub const Names = struct {
41 }; 41 };
42 } 42 }
43 43
44 /// Initializes a name with a prefix. 44 /// Initializes a name that is long and short, from the same string.
45 /// ::short is set to ::name[0], and ::long is set to ::name. 45 /// ::short is set to ::name[0], and ::long is set to ::name.
46 /// This function asserts that ::name.len != 0 46 /// This function asserts that ::name.len != 0
47 pub fn prefix(name: []const u8) Names { 47 pub fn both(name: []const u8) Names {
48 debug.assert(name.len != 0); 48 debug.assert(name.len != 0);
49 49
50 return Names{ 50 return Names{
@@ -119,14 +119,13 @@ pub fn helpFull(
119 params: []const Param(Id), 119 params: []const Param(Id),
120 comptime Error: type, 120 comptime Error: type,
121 context: var, 121 context: var,
122 help_text: fn(@typeOf(context), Param(Id)) Error![]const u8, 122 help_text: fn (@typeOf(context), Param(Id)) Error![]const u8,
123 value_text: fn(@typeOf(context), Param(Id)) Error![]const u8, 123 value_text: fn (@typeOf(context), Param(Id)) Error![]const u8,
124) !void { 124) !void {
125 const max_spacing = blk: { 125 const max_spacing = blk: {
126 var null_stream = io.NullOutStream.init();
127 var res: usize = 0; 126 var res: usize = 0;
128 for (params) |param| { 127 for (params) |param| {
129 var counting_stream = io.CountingOutStream(io.NullOutStream.Error).init(&null_stream.stream); 128 var counting_stream = io.CountingOutStream(io.NullOutStream.Error).init(io.null_out_stream);
130 try printParam(&counting_stream.stream, Id, param, Error, context, value_text); 129 try printParam(&counting_stream.stream, Id, param, Error, context, value_text);
131 if (res < counting_stream.bytes_written) 130 if (res < counting_stream.bytes_written)
132 res = counting_stream.bytes_written; 131 res = counting_stream.bytes_written;
@@ -153,7 +152,7 @@ fn printParam(
153 param: Param(Id), 152 param: Param(Id),
154 comptime Error: type, 153 comptime Error: type,
155 context: var, 154 context: var,
156 value_text: fn(@typeOf(context), Param(Id)) Error![]const u8, 155 value_text: fn (@typeOf(context), Param(Id)) Error![]const u8,
157) @typeOf(stream.*).Error!void { 156) @typeOf(stream.*).Error!void {
158 if (param.names.short) |s| { 157 if (param.names.short) |s| {
159 try stream.print("-{c}", s); 158 try stream.print("-{c}", s);
@@ -179,12 +178,12 @@ pub fn helpEx(
179 stream: var, 178 stream: var,
180 comptime Id: type, 179 comptime Id: type,
181 params: []const Param(Id), 180 params: []const Param(Id),
182 help_text: fn(Param(Id)) []const u8, 181 help_text: fn (Param(Id)) []const u8,
183 value_text: fn(Param(Id)) []const u8, 182 value_text: fn (Param(Id)) []const u8,
184) !void { 183) !void {
185 const Context = struct { 184 const Context = struct {
186 help_text: fn(Param(Id)) []const u8, 185 help_text: fn (Param(Id)) []const u8,
187 value_text: fn(Param(Id)) []const u8, 186 value_text: fn (Param(Id)) []const u8,
188 187
189 pub fn help(c: @This(), p: Param(Id)) error{}![]const u8 { 188 pub fn help(c: @This(), p: Param(Id)) error{}![]const u8 {
190 return c.help_text(p); 189 return c.help_text(p);
@@ -223,7 +222,6 @@ fn getValueSimple(param: Param([]const u8)) []const u8 {
223 return "VALUE"; 222 return "VALUE";
224} 223}
225 224
226
227test "clap.help" { 225test "clap.help" {
228 var buf: [1024]u8 = undefined; 226 var buf: [1024]u8 = undefined;
229 var slice_stream = io.SliceOutStream.init(buf[0..]); 227 var slice_stream = io.SliceOutStream.init(buf[0..]);
@@ -248,19 +246,19 @@ test "clap.help" {
248 ), 246 ),
249 Param([]const u8).flag( 247 Param([]const u8).flag(
250 "Both flag.", 248 "Both flag.",
251 Names.prefix("cc"), 249 Names.both("cc"),
252 ), 250 ),
253 Param([]const u8).option( 251 Param([]const u8).option(
254 "Both option.", 252 "Both option.",
255 Names.prefix("dd"), 253 Names.both("dd"),
256 ), 254 ),
257 Param([]const u8).positional( 255 Param([]const u8).positional(
258 "Positional. This should not appear in the help message." 256 "Positional. This should not appear in the help message.",
259 ), 257 ),
260 }, 258 },
261 ); 259 );
262 260
263 const expected = 261 const expected = "" ++
264 "\t-a \tShort flag.\n" ++ 262 "\t-a \tShort flag.\n" ++
265 "\t-b=VALUE \tShort option.\n" ++ 263 "\t-b=VALUE \tShort option.\n" ++
266 "\t --aa \tLong flag.\n" ++ 264 "\t --aa \tLong flag.\n" ++