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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
use crate::arg::Arg;
use crate::command::{Command, Matches};
use std::collections::HashMap;
use std::process::exit;
use thiserror::Error;
/// Custom error types for the application.
#[derive(Debug, Error)]
pub enum AppError {
#[error("No arguments provided.")]
NoArguments,
#[error("Unknown command: {0}")]
UnknownCommand(String),
#[error("Missing value for argument: {0}")]
MissingValue(String),
}
/// A simple command line argument parser.
pub struct App<'a> {
/// Name of the application.
name: &'a str,
/// Version of the application.
version: &'a str,
/// Author of the application.
author: &'a str,
/// Brief description of the application.
about: &'a str,
/// List of arguments for the application.
args: Vec<Arg<'a>>,
/// HashMap of commands for the application.
commands: HashMap<&'a str, Command<'a>>,
}
impl<'a> App<'a> {
/// Creates a new instance of `App`.
///
/// # Arguments
///
/// * `name` - The name of the application.
///
/// # Returns
///
/// * A new instance of `App`.
pub fn new(name: &'a str) -> Self {
App {
name,
version: "",
author: "",
about: "",
args: Vec::new(),
commands: HashMap::new(),
}
}
/// Sets the version of the application.
///
/// # Arguments
///
/// * `version` - The version of the application.
///
/// # Returns
///
/// * An instance of `App` with the version set.
pub fn version(mut self, version: &'a str) -> Self {
self.version = version;
self
}
/// Sets the author of the application.
///
/// # Arguments
///
/// * `author` - The author of the application.
///
/// # Returns
///
/// * An instance of `App` with the author set.
pub fn author(mut self, author: &'a str) -> Self {
self.author = author;
self
}
/// Sets the description of the application.
///
/// # Arguments
///
/// * `about` - The description of the application.
///
/// # Returns
///
/// * An instance of `App` with the description set.
pub fn about(mut self, about: &'a str) -> Self {
self.about = about;
self
}
/// Adds an argument to the application.
///
/// # Arguments
///
/// * `arg` - An argument to be added.
///
/// # Returns
///
/// * An instance of `App` with the argument added.
pub fn arg(mut self, arg: Arg<'a>) -> Self {
self.args.push(arg);
self
}
/// Adds a command to the application.
///
/// # Arguments
///
/// * `command` - A command to be added.
///
/// # Returns
///
/// * An instance of `App` with the command added.
pub fn command(mut self, command: Command<'a>) -> Self {
self.commands.insert(command.name, command);
self
}
/// Parses the command line arguments and returns the matches.
///
/// # Returns
///
/// * An instance of `Matches` containing the parsed arguments or an `AppError`.
pub fn get_matches(&self) -> Result<Matches, AppError> {
let args: Vec<String> = std::env::args().collect();
// Check if no arguments are provided
if args.len() == 1 {
self.print_help();
return Err(AppError::NoArguments);
}
// Check for help flag first
if args.iter().any(|arg| arg == "--help" || arg == "-h") {
self.print_help();
exit(0);
}
let mut matches = Matches::new();
// Parse global arguments
self.parse_args(&mut matches, &self.args, &args)?;
// Check for command execution
if args.len() > 1 {
if let Some(command) = self.commands.get(args[1].as_str()) {
let mut command_matches = self.parse_command_args(command, &args[2..])?;
for (k, v) in matches.args.iter() {
command_matches.insert(k, v.clone());
}
if let Some(execute) = &command.execute {
execute(&command_matches);
exit(0);
} else {
self.handle_subcommand(command, &args[2..]);
}
} else {
return Err(AppError::UnknownCommand(args[1].clone()));
}
}
Ok(matches)
}
/// Parses the command line arguments.
///
/// # Arguments
///
/// * `matches` - The `Matches` instance to store the parsed arguments.
/// * `arg_definitions` - The list of argument definitions.
/// * `args` - The list of command line arguments.
fn parse_args(&self, matches: &mut Matches, arg_definitions: &[Arg], args: &[String]) -> Result<(), AppError> {
let mut i = 0;
while i < args.len() {
if args[i].starts_with("--") {
let arg_name = &args[i][2..];
if let Some(arg_def) = arg_definitions.iter().find(|a| a.name == arg_name) {
if arg_def.is_flag {
matches.insert(arg_name, "true".to_string());
} else if i + 1 < args.len() {
matches.insert(arg_name, args[i + 1].clone());
i += 1;
} else if let Some(default) = arg_def.default {
matches.insert(arg_name, default.to_string());
} else {
return Err(AppError::MissingValue(arg_name.to_string()));
}
} else {
// Handle custom arguments
if i + 1 < args.len() && !args[i + 1].starts_with("--") {
matches.insert(arg_name, args[i + 1].clone());
i += 1;
} else {
matches.insert(arg_name, "true".to_string());
}
}
}
i += 1;
}
Ok(())
}
/// Parses the command line arguments for a specific command.
///
/// # Arguments
///
/// * `command` - The command definition.
/// * `args` - The list of command line arguments.
///
/// # Returns
///
/// * An instance of `Matches` containing the parsed arguments.
fn parse_command_args(&self, command: &Command, args: &[String]) -> Result<Matches, AppError> {
let mut matches = Matches::new();
self.parse_args(&mut matches, &command.args, args)?;
Ok(matches)
}
/// Handles the subcommands of a command.
///
/// # Arguments
///
/// * `command` - The command definition.
/// * `args` - The list of command line arguments.
fn handle_subcommand(&self, command: &Command, args: &[String]) {
if !args.is_empty() {
if let Some(subcommand) = command.subcommands.get(args[0].as_str()) {
let subcommand_matches = self.parse_command_args(subcommand, &args[1..]).unwrap();
if let Some(execute) = &subcommand.execute {
execute(&subcommand_matches);
} else {
self.print_command_help(subcommand);
}
exit(0);
}
}
self.print_command_help(command);
}
/// Prints the help information for the application.
fn print_help(&self) {
println!("{} {}", self.name, self.version);
println!("{}", self.about);
println!("Author: {}", self.author);
println!("\nUsage:");
for arg in &self.args {
println!(" {}: {}", arg.name, arg.about);
}
if !self.commands.is_empty() {
println!("\nCommands:");
for (name, command) in &self.commands {
println!(" {}: {}", name, command.about);
for (sub_name, sub_command) in &command.subcommands {
println!(" {}: {}", sub_name, sub_command.about);
}
}
}
}
/// Prints the help information for a specific command.
///
/// # Arguments
///
/// * `command` - The command definition.
fn print_command_help(&self, command: &Command) {
println!("{}", command.about);
println!("\nUsage:");
for arg in &command.args {
println!(" {}: {}", arg.name, arg.about);
}
if !command.subcommands.is_empty() {
println!("\nSubcommands:");
for (name, subcommand) in &command.subcommands {
println!(" {}: {}", name, subcommand.about);
for arg in &subcommand.args {
println!(" {}: {}", arg.name, arg.about);
}
}
}
}
}