-
Notifications
You must be signed in to change notification settings - Fork 13
/
Copy pathArgMatches.zig
160 lines (153 loc) · 4.94 KB
/
ArgMatches.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
//! A structure for querying the parse result.
const ArgMatches = @This();
const std = @import("std");
const ParseResult = @import("parser/ParseResult.zig");
/// Core structure containing parse result.
///
/// It is not intended to use directly instead methods provided by this
/// structure should be use.
parse_result: *const ParseResult,
/// Checks whether any arguments were present on the command line.
///
/// ## Examples
///
/// ```zig
/// var app = App.init(allocator, "myapp", "My app description");
/// defer app.deinit();
///
/// var root = app.rootCommand();
/// try root.addArg(Arg.booleanOption("verbose", 'v', "Enable verbose output"));
/// try root.addSubcommand(app.createCommand("init-exe", "Initilize project"));
///
/// const matches = try app.parseProcess();
///
/// if (!matches.containsArgs()) {
/// try app.displayHelp();
/// return;
/// }
/// ```
pub fn containsArgs(self: *const ArgMatches) bool {
return !self.parse_result.isEmpty();
}
/// Checks whether an option, positional argument or subcommand with the
/// specified name was present on the command line.
///
/// ## Examples
///
/// ```zig
/// var app = App.init(allocator, "myapp", "My app description");
/// defer app.deinit();
///
/// var root = app.rootCommand();
/// try root.addArg(Arg.booleanOption("verbose", 'v', "Enable verbose output"));
///
/// // Define a subcommand
/// var build_cmd = app.createCommand("build", "Build the project");
/// try build_cmd.addArg(Arg.booleanOption("release", 'r', "Build in release mode"));
/// try root.addSubcommand(build_cmd);
///
/// const matches = try app.parseProcess();
///
/// if (matches.containsArg("verbose")) {
/// // Handle verbose operation
/// }
///
/// if (matches.containsArg("build")) {
/// const build_cmd_matches = matches.subcommandMatches("build").?;
///
/// if (build_cmd_matches.containsArg("release")) {
/// // Build for release mode
/// }
/// }
///
/// ```
pub fn containsArg(self: *const ArgMatches, arg: []const u8) bool {
if (self.parse_result.getArgs().contains(arg)) {
return true;
} else if (self.parse_result.getSubcommandParseResult()) |subcmd_result| {
return std.mem.eql(u8, subcmd_result.getCommand().deref().name, arg);
}
return false;
}
/// Returns the value of an option or positional argument if it was present
/// on the command line; otherwise, returns `null`.
///
/// ## Examples
///
/// ```zig
/// var app = App.init(allocator, "myapp", "My app description");
/// defer app.deinit();
///
/// var root = app.rootCommand();
/// try root.addArg(Arg.singleValueOption("config", 'c', "Config file"));
///
/// const matches = try app.parseProcess();
///
/// if (matches.getSingleValue("config")) |config_file| {
/// std.debug.print("Config file name: {s}", .{config_file});
/// }
/// ```
pub fn getSingleValue(self: *const ArgMatches, arg: []const u8) ?[]const u8 {
if (self.parse_result.getArgs().get(arg)) |value| {
if (value.isSingle()) return value.single;
}
return null;
}
/// Returns the values of an option if it was present on the command line;
/// otherwise, returns `null`.
///
/// ## Examples
///
/// ```zig
/// var app = App.init(allocator, "myapp", "My app description");
/// defer app.deinit();
///
/// var root = app.rootCommand();
/// try root.addArg(Arg.multiValuesOption("nums", 'n', "Numbers to add", 2));
///
/// const matches = try app.parseProcess();
///
/// if (matches.getMultiValues("nums")) |numbers| {
/// std.debug.print("Add {s} + {s}", .{ numbers[0], numbers[1] });
/// }
/// ```
pub fn getMultiValues(self: *const ArgMatches, arg: []const u8) ?[][]const u8 {
if (self.parse_result.getArgs().get(arg)) |value| {
if (value.isMany()) return value.many.items[0..];
}
return null;
}
/// Returns the `ArgMatches` for a specific subcommand if it was present on
/// on the command line; otherwise, returns `null`.
///
/// ## Examples
///
/// ```zig
/// var app = App.init(allocator, "myapp", "My app description");
/// defer app.deinit();
///
/// var root = app.rootCommand();
///
/// var build_cmd = app.createCommand("build", "Build the project");
/// try build_cmd.addArg(Arg.booleanOption("release", 'r', "Build in release mode"));
/// try build_cmd.addArg(Arg.singleValueOption("target", 't', "Build for given target"));
/// try root.addSubcommand(build_cmd);
///
/// const matches = try app.parseProcess();
///
/// if (matches.subcommandMatches("build")) |build_cmd_matches| {
/// if (build_cmd_matches.containsArg("release")) {
/// const target = build_cmd_matches.getSingleValue("target") orelse "default";
/// // Build for release mode to given target
/// }
/// }
///
/// ```
pub fn subcommandMatches(self: *const ArgMatches, subcmd: []const u8) ?ArgMatches {
if (self.parse_result.getSubcommandParseResult()) |subcmd_result| {
if (std.mem.eql(u8, subcmd_result.getCommand().deref().name, subcmd)) {
return ArgMatches{ .parse_result = subcmd_result };
}
}
return null;
}