diff options
| author | 2021-05-26 20:46:23 +0200 | |
|---|---|---|
| committer | 2021-05-26 20:46:23 +0200 | |
| commit | bc32ab045926fb07e4c02c2dbab5aeaddd1f6a02 (patch) | |
| tree | a545acd4583d4d344d1e235c445b9b9b5060514b /README.md | |
| parent | Merge branch 'master' into zig-master (diff) | |
| parent | Modernize codebase (diff) | |
| download | zig-clap-bc32ab045926fb07e4c02c2dbab5aeaddd1f6a02.tar.gz zig-clap-bc32ab045926fb07e4c02c2dbab5aeaddd1f6a02.tar.xz zig-clap-bc32ab045926fb07e4c02c2dbab5aeaddd1f6a02.zip | |
Merge branch 'master' into zig-master
Diffstat (limited to 'README.md')
| -rw-r--r-- | README.md | 64 |
1 files changed, 28 insertions, 36 deletions
| @@ -25,10 +25,11 @@ into master on every `zig` release. | |||
| 25 | The simplest way to use this library is to just call the `clap.parse` function. | 25 | The simplest way to use this library is to just call the `clap.parse` function. |
| 26 | 26 | ||
| 27 | ```zig | 27 | ```zig |
| 28 | const std = @import("std"); | ||
| 29 | const clap = @import("clap"); | 28 | const clap = @import("clap"); |
| 29 | const std = @import("std"); | ||
| 30 | 30 | ||
| 31 | const debug = std.debug; | 31 | const debug = std.debug; |
| 32 | const io = std.io; | ||
| 32 | 33 | ||
| 33 | pub fn main() !void { | 34 | pub fn main() !void { |
| 34 | // First we specify what parameters our program can take. | 35 | // First we specify what parameters our program can take. |
| @@ -41,13 +42,12 @@ pub fn main() !void { | |||
| 41 | }; | 42 | }; |
| 42 | 43 | ||
| 43 | // Initalize our diagnostics, which can be used for reporting useful errors. | 44 | // Initalize our diagnostics, which can be used for reporting useful errors. |
| 44 | // This is optional. You can also just pass `null` to `parser.next` if you | 45 | // This is optional. You can also pass `.{}` to `clap.parse` if you don't |
| 45 | // don't care about the extra information `Diagnostics` provides. | 46 | // care about the extra information `Diagnostics` provides. |
| 46 | var diag: clap.Diagnostic = undefined; | 47 | var diag = clap.Diagnostic{}; |
| 47 | 48 | var args = clap.parse(clap.Help, ¶ms, .{ .diagnostic = &diag }) catch |err| { | |
| 48 | var args = clap.parse(clap.Help, ¶ms, std.heap.page_allocator, &diag) catch |err| { | ||
| 49 | // Report useful error and exit | 49 | // Report useful error and exit |
| 50 | diag.report(std.io.getStdErr().writer(), err) catch {}; | 50 | diag.report(io.getStdErr().writer(), err) catch {}; |
| 51 | return err; | 51 | return err; |
| 52 | }; | 52 | }; |
| 53 | defer args.deinit(); | 53 | defer args.deinit(); |
| @@ -69,15 +69,15 @@ that the strings you pass to `option`, `options` and `flag` are actually paramet | |||
| 69 | program can take: | 69 | program can take: |
| 70 | 70 | ||
| 71 | ```zig | 71 | ```zig |
| 72 | const std = @import("std"); | ||
| 73 | const clap = @import("clap"); | 72 | const clap = @import("clap"); |
| 73 | const std = @import("std"); | ||
| 74 | 74 | ||
| 75 | pub fn main() !void { | 75 | pub fn main() !void { |
| 76 | const params = comptime [_]clap.Param(clap.Help){ | 76 | const params = comptime [_]clap.Param(clap.Help){ |
| 77 | clap.parseParam("-h, --help Display this help and exit.") catch unreachable, | 77 | clap.parseParam("-h, --help Display this help and exit.") catch unreachable, |
| 78 | }; | 78 | }; |
| 79 | 79 | ||
| 80 | var args = try clap.parse(clap.Help, ¶ms, std.heap.direct_allocator, null); | 80 | var args = try clap.parse(clap.Help, ¶ms, .{}); |
| 81 | defer args.deinit(); | 81 | defer args.deinit(); |
| 82 | 82 | ||
| 83 | _ = args.flag("--helps"); | 83 | _ = args.flag("--helps"); |
| @@ -107,29 +107,27 @@ The `StreamingClap` is the base of all the other parsers. It's a streaming parse | |||
| 107 | `args.Iterator` to provide it with arguments lazily. | 107 | `args.Iterator` to provide it with arguments lazily. |
| 108 | 108 | ||
| 109 | ```zig | 109 | ```zig |
| 110 | const std = @import("std"); | ||
| 111 | const clap = @import("clap"); | 110 | const clap = @import("clap"); |
| 111 | const std = @import("std"); | ||
| 112 | 112 | ||
| 113 | const debug = std.debug; | 113 | const debug = std.debug; |
| 114 | const io = std.io; | ||
| 114 | 115 | ||
| 115 | pub fn main() !void { | 116 | pub fn main() !void { |
| 116 | const allocator = std.heap.page_allocator; | 117 | const allocator = std.heap.page_allocator; |
| 117 | 118 | ||
| 118 | // First we specify what parameters our program can take. | 119 | // First we specify what parameters our program can take. |
| 119 | const params = [_]clap.Param(u8){ | 120 | const params = [_]clap.Param(u8){ |
| 120 | clap.Param(u8){ | 121 | .{ |
| 121 | .id = 'h', | 122 | .id = 'h', |
| 122 | .names = clap.Names{ .short = 'h', .long = "help" }, | 123 | .names = .{ .short = 'h', .long = "help" }, |
| 123 | }, | 124 | }, |
| 124 | clap.Param(u8){ | 125 | .{ |
| 125 | .id = 'n', | 126 | .id = 'n', |
| 126 | .names = clap.Names{ .short = 'n', .long = "number" }, | 127 | .names = .{ .short = 'n', .long = "number" }, |
| 127 | .takes_value = .One, | 128 | .takes_value = .one, |
| 128 | }, | ||
| 129 | clap.Param(u8){ | ||
| 130 | .id = 'f', | ||
| 131 | .takes_value = .One, | ||
| 132 | }, | 129 | }, |
| 130 | .{ .id = 'f', .takes_value = .one }, | ||
| 133 | }; | 131 | }; |
| 134 | 132 | ||
| 135 | // We then initialize an argument iterator. We will use the OsIterator as it nicely | 133 | // We then initialize an argument iterator. We will use the OsIterator as it nicely |
| @@ -137,21 +135,20 @@ pub fn main() !void { | |||
| 137 | var iter = try clap.args.OsIterator.init(allocator); | 135 | var iter = try clap.args.OsIterator.init(allocator); |
| 138 | defer iter.deinit(); | 136 | defer iter.deinit(); |
| 139 | 137 | ||
| 140 | // Initialize our streaming parser. | 138 | // Initalize our diagnostics, which can be used for reporting useful errors. |
| 139 | // This is optional. You can also leave the `diagnostic` field unset if you | ||
| 140 | // don't care about the extra information `Diagnostic` provides. | ||
| 141 | var diag = clap.Diagnostic{}; | ||
| 141 | var parser = clap.StreamingClap(u8, clap.args.OsIterator){ | 142 | var parser = clap.StreamingClap(u8, clap.args.OsIterator){ |
| 142 | .params = ¶ms, | 143 | .params = ¶ms, |
| 143 | .iter = &iter, | 144 | .iter = &iter, |
| 145 | .diagnostic = &diag, | ||
| 144 | }; | 146 | }; |
| 145 | 147 | ||
| 146 | // Initalize our diagnostics, which can be used for reporting useful errors. | ||
| 147 | // This is optional. You can also just pass `null` to `parser.next` if you | ||
| 148 | // don't care about the extra information `Diagnostics` provides. | ||
| 149 | var diag: clap.Diagnostic = undefined; | ||
| 150 | |||
| 151 | // Because we use a streaming parser, we have to consume each argument parsed individually. | 148 | // Because we use a streaming parser, we have to consume each argument parsed individually. |
| 152 | while (parser.next(&diag) catch |err| { | 149 | while (parser.next() catch |err| { |
| 153 | // Report useful error and exit | 150 | // Report useful error and exit |
| 154 | diag.report(std.io.getStdErr().writer(), err) catch {}; | 151 | diag.report(io.getStdErr().writer(), err) catch {}; |
| 155 | return err; | 152 | return err; |
| 156 | }) |arg| { | 153 | }) |arg| { |
| 157 | // arg.param will point to the parameter which matched the argument. | 154 | // arg.param will point to the parameter which matched the argument. |
| @@ -179,18 +176,15 @@ The `help`, `helpEx` and `helpFull` are functions for printing a simple list of | |||
| 179 | program can take. | 176 | program can take. |
| 180 | 177 | ||
| 181 | ```zig | 178 | ```zig |
| 182 | const std = @import("std"); | ||
| 183 | const clap = @import("clap"); | 179 | const clap = @import("clap"); |
| 180 | const std = @import("std"); | ||
| 184 | 181 | ||
| 185 | pub fn main() !void { | 182 | pub fn main() !void { |
| 186 | const stderr_file = std.io.getStdErr(); | ||
| 187 | var stderr_out_stream = stderr_file.writer(); | ||
| 188 | |||
| 189 | // clap.help is a function that can print a simple help message, given a | 183 | // clap.help is a function that can print a simple help message, given a |
| 190 | // slice of Param(Help). There is also a helpEx, which can print a | 184 | // slice of Param(Help). There is also a helpEx, which can print a |
| 191 | // help message for any Param, but it is more verbose to call. | 185 | // help message for any Param, but it is more verbose to call. |
| 192 | try clap.help( | 186 | try clap.help( |
| 193 | stderr_out_stream, | 187 | std.io.getStdErr().writer(), |
| 194 | comptime &[_]clap.Param(clap.Help){ | 188 | comptime &[_]clap.Param(clap.Help){ |
| 195 | clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, | 189 | clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, |
| 196 | clap.parseParam("-v, --version Output version information and exit.") catch unreachable, | 190 | clap.parseParam("-v, --version Output version information and exit.") catch unreachable, |
| @@ -220,17 +214,15 @@ The `usage`, `usageEx` and `usageFull` are functions for printing a small abbrev | |||
| 220 | of the help message. | 214 | of the help message. |
| 221 | 215 | ||
| 222 | ```zig | 216 | ```zig |
| 223 | const std = @import("std"); | ||
| 224 | const clap = @import("clap"); | 217 | const clap = @import("clap"); |
| 218 | const std = @import("std"); | ||
| 225 | 219 | ||
| 226 | pub fn main() !void { | 220 | pub fn main() !void { |
| 227 | const stderr = std.io.getStdErr().writer(); | ||
| 228 | |||
| 229 | // clap.usage is a function that can print a simple usage message, given a | 221 | // clap.usage is a function that can print a simple usage message, given a |
| 230 | // slice of Param(Help). There is also a usageEx, which can print a | 222 | // slice of Param(Help). There is also a usageEx, which can print a |
| 231 | // usage message for any Param, but it is more verbose to call. | 223 | // usage message for any Param, but it is more verbose to call. |
| 232 | try clap.usage( | 224 | try clap.usage( |
| 233 | stderr, | 225 | std.io.getStdErr().writer(), |
| 234 | comptime &[_]clap.Param(clap.Help){ | 226 | comptime &[_]clap.Param(clap.Help){ |
| 235 | clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, | 227 | clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, |
| 236 | clap.parseParam("-v, --version Output version information and exit.") catch unreachable, | 228 | clap.parseParam("-v, --version Output version information and exit.") catch unreachable, |