summaryrefslogtreecommitdiff
path: root/clap.zig
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--clap.zig (renamed from src/index.zig)130
1 files changed, 40 insertions, 90 deletions
diff --git a/src/index.zig b/clap.zig
index 40ad849..8823b59 100644
--- a/src/index.zig
+++ b/clap.zig
@@ -4,9 +4,9 @@ const debug = std.debug;
4const io = std.io; 4const io = std.io;
5const mem = std.mem; 5const mem = std.mem;
6 6
7pub const @"comptime" = @import("comptime.zig"); 7pub const @"comptime" = @import("src/comptime.zig");
8pub const args = @import("args.zig"); 8pub const args = @import("src/args.zig");
9pub const streaming = @import("streaming.zig"); 9pub const streaming = @import("src/streaming.zig");
10 10
11test "clap" { 11test "clap" {
12 _ = @"comptime"; 12 _ = @"comptime";
@@ -20,38 +20,10 @@ pub const StreamingClap = streaming.StreamingClap;
20/// The names a ::Param can have. 20/// The names a ::Param can have.
21pub const Names = struct { 21pub const Names = struct {
22 /// '-' prefix 22 /// '-' prefix
23 short: ?u8, 23 short: ?u8 = null,
24 24
25 /// '--' prefix 25 /// '--' prefix
26 long: ?[]const u8, 26 long: ?[]const u8 = null,
27
28 /// Initializes a short name
29 pub fn short(s: u8) Names {
30 return Names{
31 .short = s,
32 .long = null,
33 };
34 }
35
36 /// Initializes a long name
37 pub fn long(l: []const u8) Names {
38 return Names{
39 .short = null,
40 .long = l,
41 };
42 }
43
44 /// Initializes a name that is long and short, from the same string.
45 /// ::short is set to ::name[0], and ::long is set to ::name.
46 /// This function asserts that ::name.len != 0
47 pub fn both(name: []const u8) Names {
48 debug.assert(name.len != 0);
49
50 return Names{
51 .short = name[0],
52 .long = name,
53 };
54 }
55}; 27};
56 28
57/// Represents a parameter for the command line. 29/// Represents a parameter for the command line.
@@ -77,35 +49,9 @@ pub const Names = struct {
77/// * Positional parameters must take a value. 49/// * Positional parameters must take a value.
78pub fn Param(comptime Id: type) type { 50pub fn Param(comptime Id: type) type {
79 return struct { 51 return struct {
80 id: Id, 52 id: Id = Id{},
81 takes_value: bool, 53 names: Names = Names{},
82 names: Names, 54 takes_value: bool = false,
83
84 pub fn flag(id: Id, names: Names) @This() {
85 return init(id, false, names);
86 }
87
88 pub fn option(id: Id, names: Names) @This() {
89 return init(id, true, names);
90 }
91
92 pub fn positional(id: Id) @This() {
93 return init(id, true, Names{ .short = null, .long = null });
94 }
95
96 pub fn init(id: Id, takes_value: bool, names: Names) @This() {
97 // Assert, that if the param have no name, then it has to take
98 // a value.
99 debug.assert(names.long != null or
100 names.short != null or
101 takes_value);
102
103 return @This(){
104 .id = id,
105 .takes_value = takes_value,
106 .names = names,
107 };
108 }
109 }; 55 };
110} 56}
111 57
@@ -227,34 +173,38 @@ test "clap.help" {
227 var slice_stream = io.SliceOutStream.init(buf[0..]); 173 var slice_stream = io.SliceOutStream.init(buf[0..]);
228 try help( 174 try help(
229 &slice_stream.stream, 175 &slice_stream.stream,
230 []Param([]const u8){ 176 [_]Param([]const u8){
231 Param([]const u8).flag( 177 Param([]const u8){
232 "Short flag.", 178 .id = "Short flag.",
233 Names.short('a'), 179 .names = Names{ .short = 'a' },
234 ), 180 },
235 Param([]const u8).option( 181 Param([]const u8){
236 "Short option.", 182 .id = "Short option.",
237 Names.short('b'), 183 .names = Names{ .short = 'b' },
238 ), 184 .takes_value = true,
239 Param([]const u8).flag( 185 },
240 "Long flag.", 186 Param([]const u8){
241 Names.long("aa"), 187 .id = "Long flag.",
242 ), 188 .names = Names{ .long = "aa" },
243 Param([]const u8).option( 189 },
244 "Long option.", 190 Param([]const u8){
245 Names.long("bb"), 191 .id = "Long option.",
246 ), 192 .names = Names{ .long = "bb" },
247 Param([]const u8).flag( 193 .takes_value = true,
248 "Both flag.", 194 },
249 Names.both("cc"), 195 Param([]const u8){
250 ), 196 .id = "Both flag.",
251 Param([]const u8).option( 197 .names = Names{ .short = 'c', .long = "cc" },
252 "Both option.", 198 },
253 Names.both("dd"), 199 Param([]const u8){
254 ), 200 .id = "Both option.",
255 Param([]const u8).positional( 201 .names = Names{ .short = 'd', .long = "dd" },
256 "Positional. This should not appear in the help message.", 202 .takes_value = true,
257 ), 203 },
204 Param([]const u8){
205 .id = "Positional. This should not appear in the help message.",
206 .takes_value = true,
207 },
258 }, 208 },
259 ); 209 );
260 210