diff options
Diffstat (limited to 'src/index.zig')
| -rw-r--r-- | src/index.zig | 105 |
1 files changed, 105 insertions, 0 deletions
diff --git a/src/index.zig b/src/index.zig new file mode 100644 index 0000000..dde4748 --- /dev/null +++ b/src/index.zig | |||
| @@ -0,0 +1,105 @@ | |||
| 1 | const std = @import("std"); | ||
| 2 | |||
| 3 | const debug = std.debug; | ||
| 4 | |||
| 5 | pub const @"comptime" = @import("comptime.zig"); | ||
| 6 | pub const args = @import("args.zig"); | ||
| 7 | pub const streaming = @import("streaming.zig"); | ||
| 8 | |||
| 9 | test "clap" { | ||
| 10 | _ = @"comptime"; | ||
| 11 | _ = args; | ||
| 12 | _ = streaming; | ||
| 13 | } | ||
| 14 | |||
| 15 | pub const ComptimeClap = @"comptime".ComptimeClap; | ||
| 16 | pub const StreamingClap = streaming.StreamingClap; | ||
| 17 | |||
| 18 | /// The names a ::Param can have. | ||
| 19 | pub const Names = struct { | ||
| 20 | /// '-' prefix | ||
| 21 | short: ?u8, | ||
| 22 | |||
| 23 | /// '--' prefix | ||
| 24 | long: ?[]const u8, | ||
| 25 | |||
| 26 | /// Initializes no names | ||
| 27 | pub fn positional() Names { | ||
| 28 | return Names{ | ||
| 29 | .short = null, | ||
| 30 | .long = null, | ||
| 31 | }; | ||
| 32 | } | ||
| 33 | |||
| 34 | /// Initializes a short name | ||
| 35 | pub fn short(s: u8) Names { | ||
| 36 | return Names{ | ||
| 37 | .short = s, | ||
| 38 | .long = null, | ||
| 39 | }; | ||
| 40 | } | ||
| 41 | |||
| 42 | /// Initializes a long name | ||
| 43 | pub fn long(l: []const u8) Names { | ||
| 44 | return Names{ | ||
| 45 | .short = null, | ||
| 46 | .long = l, | ||
| 47 | }; | ||
| 48 | } | ||
| 49 | |||
| 50 | /// Initializes a name with a prefix. | ||
| 51 | /// ::short is set to ::name[0], and ::long is set to ::name. | ||
| 52 | /// This function asserts that ::name.len != 0 | ||
| 53 | pub fn prefix(name: []const u8) Names { | ||
| 54 | debug.assert(name.len != 0); | ||
| 55 | |||
| 56 | return Names{ | ||
| 57 | .short = name[0], | ||
| 58 | .long = name, | ||
| 59 | }; | ||
| 60 | } | ||
| 61 | }; | ||
| 62 | |||
| 63 | /// Represents a parameter for the command line. | ||
| 64 | /// Parameters come in three kinds: | ||
| 65 | /// * Short ("-a"): Should be used for the most commonly used parameters in your program. | ||
| 66 | /// * They can take a value three different ways. | ||
| 67 | /// * "-a value" | ||
| 68 | /// * "-a=value" | ||
| 69 | /// * "-avalue" | ||
| 70 | /// * They chain if they don't take values: "-abc". | ||
| 71 | /// * The last given parameter can take a value in the same way that a single parameter can: | ||
| 72 | /// * "-abc value" | ||
| 73 | /// * "-abc=value" | ||
| 74 | /// * "-abcvalue" | ||
| 75 | /// * Long ("--long-param"): Should be used for less common parameters, or when no single character | ||
| 76 | /// can describe the paramter. | ||
| 77 | /// * They can take a value two different ways. | ||
| 78 | /// * "--long-param value" | ||
| 79 | /// * "--long-param=value" | ||
| 80 | /// * Positional: Should be used as the primary parameter of the program, like a filename or | ||
| 81 | /// an expression to parse. | ||
| 82 | /// * Positional parameters have both names.long and names.short == null. | ||
| 83 | /// * Positional parameters must take a value. | ||
| 84 | pub fn Param(comptime Id: type) type { | ||
| 85 | return struct { | ||
| 86 | id: Id, | ||
| 87 | takes_value: bool, | ||
| 88 | names: Names, | ||
| 89 | |||
| 90 | pub fn init(id: Id, takes_value: bool, names: Names) @This() { | ||
| 91 | // Assert, that if the param have no name, then it has to take | ||
| 92 | // a value. | ||
| 93 | debug.assert( | ||
| 94 | names.long != null or | ||
| 95 | names.short != null or | ||
| 96 | takes_value); | ||
| 97 | |||
| 98 | return @This(){ | ||
| 99 | .id = id, | ||
| 100 | .takes_value = takes_value, | ||
| 101 | .names = names, | ||
| 102 | }; | ||
| 103 | } | ||
| 104 | }; | ||
| 105 | } | ||