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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
use crate::print_command_help;
use super::command::{help, Command, Handler};
use super::context::Context;
use super::opt::{Opt, OptError, OptKind};
use std::env;
type Result<T> = std::result::Result<T, OptError>;
pub struct App {
name: Option<&'static str>,
version: Option<&'static str>,
pub root: Command,
config_filename: Option<&'static str>,
env_prefix: Option<&'static str>,
}
impl Default for App {
fn default() -> Self {
Self {
name: None,
version: None,
root: Command::new("root"),
env_prefix: None,
config_filename: None,
}
}
}
impl App {
pub fn application_header(&self) -> String {
format!(
"{} - {}",
self.name.as_ref().unwrap_or(&env!("CARGO_PKG_NAME")),
self.version.as_ref().unwrap_or(&env!("CARGO_PKG_VERSION"))
)
}
}
impl App {
/// Contructs a new App instance which can have opts defined and subcommands attached.
pub fn new() -> Self {
App::default().command(
Command::new("help")
.handler(help)
.short_desc("Displays help information"),
)
}
/// Sets the name of the application. If not set the cargo package name will be used.
///
/// Exmaple:
///
/// ```rust
/// use arkham::App;
/// let app = App::new().name("new app");
/// ```
pub fn name(mut self, name: &'static str) -> Self {
self.name = Some(name);
self
}
/// Sets the version for the application. If not set the cargo package version is used.
///
/// Example:
///
/// ```rust
/// use arkham::App;
/// App::new().version("1.0.0");
/// ```
pub fn version(mut self, version: &'static str) -> Self {
self.version = Some(version);
self
}
/// Sets the environment variable prefix for option resolution. If set to something like
/// APP_NAME a option with the name THING, will look for an environment variable named
/// APP_NAME_THING.
///
/// Example:
/// ```rust
/// use arkham::App;
/// App::new().env_prefix("APP_NAME");
///
/// ```
pub fn env_prefix(mut self, prefix: &'static str) -> Self {
self.env_prefix = Some(prefix);
self
}
/// Adds a root level command to the application. This command can then be executed with:
///
/// myapp command_name
///
/// Help flags will also be generated for the command which will display command
/// information for:
///
/// myapp --help command_name or myapp help command_name
///
/// Example:
/// ```rust
/// use arkham::{App, Command, Context};
/// App::new().command(Command::new("subcommand").handler(my_handler));
///
/// fn my_handler(app: &App, ctx: &Context, args: &[String]) {}
/// ```
pub fn command(mut self, cmd: Command) -> Self {
self.root.commands.push(cmd);
self
}
/// Adds a root level opt/flag that is available to all commands. Opts are given a name which
/// is used to reference them, as well as a short and long identifier.
///
/// Example:
/// ```rust
/// use arkham::{App, Opt};
/// App::new().opt(Opt::flag("verbose").short("v").long("verbose"));
/// ```
pub fn opt(mut self, opt: Opt) -> Self {
self.root.opts.push(opt);
self
}
/// Sets a handler function for the bare root command. If this is not set an error will be
/// generated and a help message will be displayed indicating the available subcommands.
/// The handler function takes an instance of the app, the context which contains the opts and
/// flags, and any additionally passeed arguments.
///
/// Example:
/// ```rust
/// use arkham::{App, Command, Context};
/// App::new().handler(my_handler);
///
/// fn my_handler(app: &App, ctx: &Context, args: &[String]) {}
/// ```
pub fn handler(mut self, f: Handler) -> Self {
self.root.handler = Some(f);
self
}
/// Execute the app and any specified handlers based on the passed arguemnts. This function is
/// mostly used for testing or any situation where you need to pass arbitrary arguments instead
/// of using the ones passed to the application.
/// Example:
/// ```rust
/// use arkham::{App, Command, Context, Opt};
/// App::new()
/// .opt(Opt::scalar("name").short("n").long("name"))
/// .handler(my_handler)
/// .run_with(vec!["-n".to_string(), "alice".to_string()]);
///
/// fn my_handler(app: &App, ctx: &Context, args: &[String]) {
/// println!("Hello, {}", ctx.get_string("name").unwrap());
/// }
/// ```
pub fn run_with(&mut self, args: Vec<String>) -> Result<()> {
let mut ctx = Context::new(self.root.clone());
if let Some(filename) = self.config_filename {
ctx.load_config_file(filename);
}
run_command(self, &self.root, &args, &mut ctx)
}
/// Execute the app and any specified handlers based on the arguments passsed to the
/// application.
///
/// Example:
/// running with myapp --name alice
/// ```rust
/// use arkham::{App, Command, Context, Opt};
/// App::new()
/// .opt(Opt::flag("name").short("n").long("name"))
/// .handler(my_handler)
/// .run();
///
/// fn my_handler(app: &App, ctx: &Context, args: &[String]) {
/// println!("Hello, {}", ctx.get_string("name").unwrap_or_else(|| "unnamed".into()));
/// }
/// ```
pub fn run(&mut self) -> Result<()> {
self.run_with(env::args().skip(1).collect())
}
}
/// This is the core logic for parsing arguments and executing handlers. It is ran but the App::run
/// and App::run_with functions.
fn run_command(app: &App, cmd: &Command, args: &[String], ctx: &mut Context) -> Result<()> {
// Get an iterator for the incomming arguments
let mut args = args.iter();
// We will keep track of any arguments that arent consumed by the current command.
// These will be either used to collect arguments from subcommands or passed as additional args
// to the command.
let mut ignored: Vec<String> = vec![];
// Loop through all passed in args
while let Some(arg) = args.next() {
// Check for long args
if arg.starts_with("--") {
if let Some(opt) = cmd.opts.iter().find(|o| &o.long == &arg[2..]) {
match opt.kind {
OptKind::Flag => {
ctx.set_flag(opt);
}
OptKind::String => {
if let Some(value) = args.next() {
ctx.set_opt(opt, value.clone());
} else {
return Err(OptError::InvalidOpt(opt.name.clone()));
}
}
}
} else {
ignored.push(arg.clone());
}
continue;
}
// Check for short args
if arg.starts_with("-") {
if let Some(opt) = cmd.opts.iter().find(|o| &o.short == &arg[1..]) {
match opt.kind {
OptKind::Flag => {
ctx.set_flag(opt);
}
OptKind::String => {
if let Some(value) = args.next() {
ctx.set_opt(opt, value.clone());
} else {
return Err(OptError::InvalidOpt(opt.name.clone()));
}
}
}
} else {
ignored.push(arg.clone());
}
continue;
}
ignored.push(arg.clone());
}
// Find an recurse into sub commands if the remaining argumetns match any subcommand name
if let Some(cmd) = cmd
.commands
.iter()
.find(|cmd| ignored.iter().any(|a| *a == cmd.name))
{
ignored.retain(|a| *a != cmd.name);
return run_command(app, cmd, &ignored, ctx);
}
// Automatic command help display
if ignored.iter().any(|a| a == "-h" || a == "--help") {
super::command::print_command_help(cmd, &vec![]);
return Ok(());
}
// If any ignored parameters start with "-" we will throw an unknwon flag error.
if let Some(arg) = ignored.iter().find(|a| a.starts_with("-")) {
return Err(OptError::InvalidOpt(arg.clone()));
}
// Execute the command handler
if let Some(handler) = cmd.handler {
ctx.cmd = cmd.clone();
if let Some(prefix) = app.env_prefix {
ctx.env_prefix = Some(prefix.to_string());
}
handler(app, &ctx, &ignored);
} else {
crate::vox::print(app.application_header());
print_command_help(cmd, &vec![])
}
Ok(())
}
#[cfg(not(feature = "config"))]
impl App {
pub fn config_filename(&mut self, _filename: &'static str) -> Self {
panic!("Not compiled with configuration capabilities. Add a config parser as a feature");
}
}
#[cfg(feature = "config")]
impl App {
pub fn config_filename(mut self, filename: &'static str) -> Self {
self.config_filename = Some(filename);
self
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::App;
#[test]
fn test_long_string() {
let args: Vec<String> = vec!["--user".into(), "joe".into()];
App::new()
.opt(Opt::scalar("user").short("u").long("user"))
.handler(|_, ctx, _| {
assert_eq!(ctx.get_string("user"), Some("joe".into()));
})
.run_with(args)
.unwrap();
}
#[test]
fn test_subcommand() {
let args = vec![
"--user".into(),
"joe".into(),
"--config".into(),
"c.json".into(),
"thing".into(),
];
App::new()
.opt(Opt::scalar("user").short("u").long("user"))
.command(
Command::new("thing")
.opt(Opt::scalar("config").short("c").long("config"))
.handler(|_, ctx, _| {
assert_eq!(ctx.get_string("user"), Some("joe".into()));
assert_eq!(ctx.get_string("config"), Some("c.json".into()));
}),
)
.run_with(args)
.unwrap();
}
fn function_handler(_app: &App, _ctx: &Context, _args: &[String]) {
assert!(true);
}
#[test]
fn test_function_handler() {
App::new().handler(function_handler);
}
#[test]
fn test_extra_args() {
let args = vec!["somefile".to_string()];
App::new()
.handler(|_, _, args| {
assert_eq!(args.len(), 1);
assert_eq!(args.first(), Some(&"somefile".to_string()));
})
.run_with(args)
.expect("app error");
}
#[test]
fn test_short_flag() {
App::new()
.opt(Opt::flag("verbose").short("v").long("verbose"))
.handler(|_, ctx, _| {
assert_eq!(ctx.flag("verbose"), true);
})
.run_with(vec!["-v".into()])
.unwrap();
}
#[test]
fn test_invalid_long_flag() {
let r = App::new()
.opt(Opt::flag("verbose").short("v").long("verbose"))
.run_with(vec!["--user".into()]);
assert!(r.is_err(), "Should error for invalid long flag");
}
#[test]
fn test_invalid_short_flag() {
let r = App::new()
.opt(Opt::flag("verbose").short("v").long("verbose"))
.run_with(vec!["-u".into()]);
assert!(r.is_err(), "Should error for invalid short flag");
}
#[test]
fn test_env_prefix() {
std::env::set_var("ARKHAM_thing", "1");
App::new()
.env_prefix("ARKHAM")
.opt(Opt::flag("thing").short("-t").long("--t"))
.handler(|_, ctx, _| {
assert_eq!(ctx.flag("thing"), true);
})
.run_with(vec![])
.unwrap();
}
}