-
Notifications
You must be signed in to change notification settings - Fork 13
/
App.zig
207 lines (194 loc) · 5.97 KB
/
App.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
const App = @This();
const std = @import("std");
const builtin = @import("builtin");
const Allocator = std.mem.Allocator;
const ArgMatches = @import("ArgMatches.zig");
const Command = @import("Command.zig");
const HelpMessageWriter = @import("HelpMessageWriter.zig");
const Parser = @import("parser/Parser.zig");
const ParseResult = @import("parser/ParseResult.zig");
const YazapError = @import("error.zig").YazapError;
/// Top level allocator for the entire library.
allocator: Allocator,
/// Root command of the app.
command: Command,
/// Core structure containing parse result.
///
/// It is not intended for direct access, use `ArgMatches` instead.
parse_result: ?ParseResult = null,
/// Raw buffer containing command line arguments.
process_args: ?[]const [:0]u8 = null,
/// Creates a new instance of `App`.
///
/// ## Examples
///
/// ```zig
/// var app = App.init("myapp", "My app description");
/// ```
pub fn init(allocator: Allocator, name: []const u8, description: ?[]const u8) App {
return App{
.allocator = allocator,
.command = Command.init(allocator, name, description),
};
}
/// Deinitializes the library by releasing all the allocated memory and cleaning
/// up structures.
///
/// ## Examples
///
/// ```zig
/// var app = App.init("myapp", "My app description");
/// defer app.deinit();
/// ```
pub fn deinit(self: *App) void {
if (self.parse_result) |*parse_result| {
parse_result.deinit();
}
if (self.process_args) |args| {
std.process.argsFree(self.allocator, args);
}
self.command.deinit();
self.parse_result = null;
}
/// Creates a new `Command` with given name and optional description.
///
/// ## Examples
///
/// ```zig
/// var app = App.init("myapp", "My app description");
/// defer app.deinit();
///
/// var subcmd1 = app.createCommand("subcmd1", "First Subcommand");
/// ```
pub fn createCommand(self: *App, name: []const u8, description: ?[]const u8) Command {
return Command.init(self.allocator, name, description);
}
/// Returns a pointer to the root `Command` of the application.
///
/// ## Examples
///
/// ```zig
/// var app = App.init("myapp", "My app description");
/// defer app.deinit();
///
/// var root = app.rootCommand();
/// root.addProperty(.help_on_empty_args);
///
/// // Add arguments and subcommands using `root`.
/// ```
pub fn rootCommand(self: *App) *Command {
return &self.command;
}
/// Parses the arguments passed to the current process.
///
/// ## Examples
///
/// ```zig
/// var app = App.init("myapp", "My app description");
/// defer app.deinit();
///
/// var root = app.rootCommand();
///
/// // Add arguments and subcommands using `root`.
///
/// const matches = try app.parseProcess();
/// ```
pub fn parseProcess(self: *App) YazapError!ArgMatches {
self.process_args = try std.process.argsAlloc(self.allocator);
return self.parseFrom(self.process_args.?[1..]);
}
/// Parses the given arguments.
///
/// ## Examples
///
/// ```zig
/// var app = App.init("myapp", "My app description");
/// defer app.deinit();
///
/// var root = app.rootCommand();
///
/// // Add arguments and subcommands using `root`.
///
/// const matches = try app.parseFrom(&.{ "arg1", "--some-option" "subcmd" });
/// ```
pub fn parseFrom(self: *App, argv: []const [:0]const u8) YazapError!ArgMatches {
var parser = Parser.init(self.allocator, argv, self.rootCommand());
var result = parser.parse() catch |err| {
// Don't clutter the test result with error messages.
if (!builtin.is_test) {
try parser.perror.print();
}
return err;
};
if (result.getCommandContainingHelpFlag()) |command| {
var help_writer = HelpMessageWriter.init(command);
try help_writer.write();
result.deinit();
self.deinit();
std.process.exit(0);
}
self.parse_result = result;
return ArgMatches{ .parse_result = &self.parse_result.? };
}
/// Displays the overall usage and description of the application.
///
/// **NOTE:** By default, the handling of the `-h` and `--help` options,
/// and the automatic display of the usage message are taken care of. Use this
/// function if you want to display the usage message when the `-h` or `--help`
/// options are not 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"));
///
/// const matches = try app.parseProcess();
///
/// if (!matches.containsArgs()) {
/// try app.displayHelp();
/// return;
/// }
/// ```
pub fn displayHelp(self: *App) YazapError!void {
if (self.parse_result) |parse_result| {
var help_writer = HelpMessageWriter.init(parse_result.getCommand());
try help_writer.write();
}
}
/// Displays the usage message of specified subcommand on the command line.
///
/// **NOTE:** By default, the handling of the `-h` and `--help` options,
/// and the automatic display of the usage message are taken care of. Use this
/// function if you want to display the usage message when the `-h` or `--help`
/// options are not present on the command line.
///
/// ## Examples
///
/// ```zig
/// var app = App.init(allocator, "myapp", "My app description");
/// defer app.deinit();
///
/// var root = app.rootCommand();
///
/// var subcmd = app.createCommand("subcmd", "Subcommand description");
/// try subcmd.addArg(Arg.booleanOption("verbose", 'v', "Enable verbose output"));
/// try root.addSubcommand(subcmd);
///
/// const matches = try app.parseProcess();
///
/// if (matches.subcommandMatches("subcmd")) |subcmd_matches| {
/// if (!subcmd_matches.containsArgs()) {
/// try app.displaySubcommandHelp();
/// }
/// ```
pub fn displaySubcommandHelp(self: *App) YazapError!void {
const parse_result = self.parse_result orelse return;
if (parse_result.getActiveSubcommand()) |subcmd| {
var help_writer = HelpMessageWriter.init(subcmd);
try help_writer.write();
}
}