diff options
| author | 2021-05-08 18:08:52 +0200 | |
|---|---|---|
| committer | 2021-05-08 18:08:52 +0200 | |
| commit | 4c14bfd5188bb61d7076bc33fccbcc6a5e9dac01 (patch) | |
| tree | 8f5267e4da9bbc14bd595697797457911d4b3081 /README.md | |
| parent | Refactor Diagnostic (and others) into a ParseOption struct (diff) | |
| download | zig-clap-4c14bfd5188bb61d7076bc33fccbcc6a5e9dac01.tar.gz zig-clap-4c14bfd5188bb61d7076bc33fccbcc6a5e9dac01.tar.xz zig-clap-4c14bfd5188bb61d7076bc33fccbcc6a5e9dac01.zip | |
Modernize codebase
* Better naming for variables
* Follow naming style of enums
* Use `writer()` instead of `outStream()`
* Change many initializers to be a one liner
* Don't explicitly initialize fields to their default value
Diffstat (limited to '')
| -rw-r--r-- | README.md | 38 |
1 files changed, 16 insertions, 22 deletions
| @@ -29,6 +29,7 @@ const clap = @import("clap"); | |||
| 29 | const std = @import("std"); | 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. |
| @@ -46,7 +47,7 @@ pub fn main() !void { | |||
| 46 | var diag = clap.Diagnostic{}; | 47 | var diag = clap.Diagnostic{}; |
| 47 | var args = clap.parse(clap.Help, ¶ms, .{ .diagnostic = &diag }) catch |err| { | 48 | var args = clap.parse(clap.Help, ¶ms, .{ .diagnostic = &diag }) catch |err| { |
| 48 | // Report useful error and exit | 49 | // Report useful error and exit |
| 49 | diag.report(std.io.getStdErr().outStream(), err) catch {}; | 50 | diag.report(io.getStdErr().writer(), err) catch {}; |
| 50 | return err; | 51 | return err; |
| 51 | }; | 52 | }; |
| 52 | defer args.deinit(); | 53 | defer args.deinit(); |
| @@ -68,15 +69,15 @@ that the strings you pass to `option`, `options` and `flag` are actually paramet | |||
| 68 | program can take: | 69 | program can take: |
| 69 | 70 | ||
| 70 | ```zig | 71 | ```zig |
| 71 | const std = @import("std"); | ||
| 72 | const clap = @import("clap"); | 72 | const clap = @import("clap"); |
| 73 | const std = @import("std"); | ||
| 73 | 74 | ||
| 74 | pub fn main() !void { | 75 | pub fn main() !void { |
| 75 | const params = comptime [_]clap.Param(clap.Help){ | 76 | const params = comptime [_]clap.Param(clap.Help){ |
| 76 | clap.parseParam("-h, --help Display this help and exit.") catch unreachable, | 77 | clap.parseParam("-h, --help Display this help and exit.") catch unreachable, |
| 77 | }; | 78 | }; |
| 78 | 79 | ||
| 79 | var args = try clap.parse(clap.Help, ¶ms, std.heap.direct_allocator, null); | 80 | var args = try clap.parse(clap.Help, ¶ms, .{}); |
| 80 | defer args.deinit(); | 81 | defer args.deinit(); |
| 81 | 82 | ||
| 82 | _ = args.flag("--helps"); | 83 | _ = args.flag("--helps"); |
| @@ -110,25 +111,23 @@ const clap = @import("clap"); | |||
| 110 | const std = @import("std"); | 111 | const std = @import("std"); |
| 111 | 112 | ||
| 112 | const debug = std.debug; | 113 | const debug = std.debug; |
| 114 | const io = std.io; | ||
| 113 | 115 | ||
| 114 | pub fn main() !void { | 116 | pub fn main() !void { |
| 115 | const allocator = std.heap.page_allocator; | 117 | const allocator = std.heap.page_allocator; |
| 116 | 118 | ||
| 117 | // First we specify what parameters our program can take. | 119 | // First we specify what parameters our program can take. |
| 118 | const params = [_]clap.Param(u8){ | 120 | const params = [_]clap.Param(u8){ |
| 119 | clap.Param(u8){ | 121 | .{ |
| 120 | .id = 'h', | 122 | .id = 'h', |
| 121 | .names = clap.Names{ .short = 'h', .long = "help" }, | 123 | .names = .{ .short = 'h', .long = "help" }, |
| 122 | }, | 124 | }, |
| 123 | clap.Param(u8){ | 125 | .{ |
| 124 | .id = 'n', | 126 | .id = 'n', |
| 125 | .names = clap.Names{ .short = 'n', .long = "number" }, | 127 | .names = .{ .short = 'n', .long = "number" }, |
| 126 | .takes_value = .One, | 128 | .takes_value = .one, |
| 127 | }, | ||
| 128 | clap.Param(u8){ | ||
| 129 | .id = 'f', | ||
| 130 | .takes_value = .One, | ||
| 131 | }, | 129 | }, |
| 130 | .{ .id = 'f', .takes_value = .one }, | ||
| 132 | }; | 131 | }; |
| 133 | 132 | ||
| 134 | // 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 |
| @@ -149,7 +148,7 @@ pub fn main() !void { | |||
| 149 | // 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. |
| 150 | while (parser.next() catch |err| { | 149 | while (parser.next() catch |err| { |
| 151 | // Report useful error and exit | 150 | // Report useful error and exit |
| 152 | diag.report(std.io.getStdErr().outStream(), err) catch {}; | 151 | diag.report(io.getStdErr().writer(), err) catch {}; |
| 153 | return err; | 152 | return err; |
| 154 | }) |arg| { | 153 | }) |arg| { |
| 155 | // arg.param will point to the parameter which matched the argument. | 154 | // arg.param will point to the parameter which matched the argument. |
| @@ -177,18 +176,15 @@ The `help`, `helpEx` and `helpFull` are functions for printing a simple list of | |||
| 177 | program can take. | 176 | program can take. |
| 178 | 177 | ||
| 179 | ```zig | 178 | ```zig |
| 180 | const std = @import("std"); | ||
| 181 | const clap = @import("clap"); | 179 | const clap = @import("clap"); |
| 180 | const std = @import("std"); | ||
| 182 | 181 | ||
| 183 | pub fn main() !void { | 182 | pub fn main() !void { |
| 184 | const stderr_file = std.io.getStdErr(); | ||
| 185 | var stderr_out_stream = stderr_file.outStream(); | ||
| 186 | |||
| 187 | // 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 |
| 188 | // 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 |
| 189 | // 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. |
| 190 | try clap.help( | 186 | try clap.help( |
| 191 | stderr_out_stream, | 187 | std.io.getStdErr().writer(), |
| 192 | comptime &[_]clap.Param(clap.Help){ | 188 | comptime &[_]clap.Param(clap.Help){ |
| 193 | clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, | 189 | clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, |
| 194 | clap.parseParam("-v, --version Output version information and exit.") catch unreachable, | 190 | clap.parseParam("-v, --version Output version information and exit.") catch unreachable, |
| @@ -218,17 +214,15 @@ The `usage`, `usageEx` and `usageFull` are functions for printing a small abbrev | |||
| 218 | of the help message. | 214 | of the help message. |
| 219 | 215 | ||
| 220 | ```zig | 216 | ```zig |
| 221 | const std = @import("std"); | ||
| 222 | const clap = @import("clap"); | 217 | const clap = @import("clap"); |
| 218 | const std = @import("std"); | ||
| 223 | 219 | ||
| 224 | pub fn main() !void { | 220 | pub fn main() !void { |
| 225 | const stderr = std.io.getStdErr().outStream(); | ||
| 226 | |||
| 227 | // 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 |
| 228 | // 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 |
| 229 | // 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. |
| 230 | try clap.usage( | 224 | try clap.usage( |
| 231 | stderr, | 225 | std.io.getStdErr().writer(), |
| 232 | comptime &[_]clap.Param(clap.Help){ | 226 | comptime &[_]clap.Param(clap.Help){ |
| 233 | clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, | 227 | clap.parseParam("-h, --help Display this help and exit. ") catch unreachable, |
| 234 | clap.parseParam("-v, --version Output version information and exit.") catch unreachable, | 228 | clap.parseParam("-v, --version Output version information and exit.") catch unreachable, |