diff options
Diffstat (limited to 'clap.zig')
| -rw-r--r-- | clap.zig | 41 |
1 files changed, 41 insertions, 0 deletions
| @@ -223,6 +223,47 @@ fn find(str: []const u8, f: []const u8) []const u8 { | |||
| 223 | return str[i..][0..f.len]; | 223 | return str[i..][0..f.len]; |
| 224 | } | 224 | } |
| 225 | 225 | ||
| 226 | pub fn Args(comptime Id: type, comptime params: []const Param(Id)) type { | ||
| 227 | return struct { | ||
| 228 | arena: std.heap.ArenaAllocator, | ||
| 229 | clap: ComptimeClap(Id, params), | ||
| 230 | exe_arg: ?[]const u8, | ||
| 231 | |||
| 232 | pub fn deinit(a: *@This()) void { | ||
| 233 | a.clap.deinit(); | ||
| 234 | a.arena.deinit(); | ||
| 235 | } | ||
| 236 | |||
| 237 | pub fn flag(a: @This(), comptime name: []const u8) bool { | ||
| 238 | return a.clap.flag(name); | ||
| 239 | } | ||
| 240 | |||
| 241 | pub fn option(a: @This(), comptime name: []const u8) ?[]const u8 { | ||
| 242 | return a.clap.option(name); | ||
| 243 | } | ||
| 244 | |||
| 245 | pub fn positionals(a: @This()) []const []const u8 { | ||
| 246 | return a.clap.positionals(); | ||
| 247 | } | ||
| 248 | }; | ||
| 249 | } | ||
| 250 | |||
| 251 | /// Parses the command line arguments passed into the program based on an | ||
| 252 | /// array of `Param`s. | ||
| 253 | pub fn parse( | ||
| 254 | comptime Id: type, | ||
| 255 | comptime params: []const Param(Id), | ||
| 256 | allocator: *mem.Allocator, | ||
| 257 | ) !Args(Id, params) { | ||
| 258 | var iter = try args.OsIterator.init(allocator); | ||
| 259 | const clap = try ComptimeClap(Id, params).parse(allocator, args.OsIterator, &iter); | ||
| 260 | return Args(Id, params){ | ||
| 261 | .arena = iter.arena, | ||
| 262 | .clap = clap, | ||
| 263 | .exe_arg = iter.exe_arg, | ||
| 264 | }; | ||
| 265 | } | ||
| 266 | |||
| 226 | /// Will print a help message in the following format: | 267 | /// Will print a help message in the following format: |
| 227 | /// -s, --long <value_text> help_text | 268 | /// -s, --long <value_text> help_text |
| 228 | /// -s, help_text | 269 | /// -s, help_text |