Zig utility library
1
fork

Configure Feed

Select the types of activity you want to include in your feed.

Document `flag` module

IamPyu 68dd843b 31ac8a0e

+17 -7
+1 -1
examples/flag.zig
··· 17 17 const stdout = std.fs.File.stdout(); 18 18 var buffer: [1024]u8 = undefined; 19 19 var writer = stdout.writer(&buffer); 20 - try Parser.print_help(&writer.interface, args[0]); 20 + try Parser.printHelp(&writer.interface, args[0]); 21 21 try writer.interface.flush(); 22 22 return; 23 23 }
+16 -6
src/flag.zig
··· 4 4 5 5 const std = @import("std"); 6 6 7 + /// Flag kind 7 8 pub const FlagKind = enum { valued, boolean }; 8 9 10 + /// Flag options 9 11 pub const Flag = struct { 10 12 name: []const u8, 11 13 kind: FlagKind, 12 14 desc: ?[]const u8 = null, 13 15 }; 14 16 17 + /// Argument parse error 15 18 pub const ParseError = error{ 16 19 MissingValue, 17 20 UnknownFlag, 18 21 }; 19 22 23 + /// Parse options 24 + pub const ParseOptions = struct { 25 + log: bool = true, 26 + }; 27 + 28 + /// Create an argument parser from `flags` 20 29 pub fn parser(comptime flags: []const Flag) type { 21 30 const flag_map = comptime map: { 22 31 const N = flags.len; ··· 68 77 }; 69 78 70 79 return struct { 80 + /// Structure of parsed flags 71 81 pub const Flags = FlagsType; 82 + 83 + /// Output of `parse`, containing parsed flags and unparsed arguments 72 84 pub const Result = struct { 73 85 args: []const []const u8, 74 86 flags: Flags, 75 87 }; 76 88 77 - pub const Options = struct { 78 - log: bool = true, 79 - }; 80 - 81 - pub fn print_help(writer: *std.Io.Writer, program_name: []const u8) !void { 89 + /// Print the help to `writer` with program name `program_name` 90 + pub fn printHelp(writer: *std.Io.Writer, program_name: []const u8) !void { 82 91 try writer.print("help for {s}:\n\n", .{program_name}); 83 92 84 93 for (flags) |f| { ··· 90 99 } 91 100 } 92 101 93 - pub fn parse(args: []const []const u8, options: Options) ParseError!Result { 102 + /// Parse `args` with `options` 103 + pub fn parse(args: []const []const u8, options: ParseOptions) ParseError!Result { 94 104 var out_flags: Flags = .{}; 95 105 96 106 var i: usize = 0;