Merge branch 'short-flags' into 'v0.1.1'
Combined shortflags See merge request arkham/arkham!2
This commit is contained in:
commit
e38edc6a68
|
@ -27,4 +27,5 @@ structures: Detail lists, headers, etc.
|
|||
# Basic Usage
|
||||
|
||||
* [fib](https://git.5sigma.io/arkham/arkham/-/blob/master/examples/fib.rs) - An example using subcommands and command line options
|
||||
* [config](https://git.5sigma.io/arkham/arkham/-/blob/master/examples/config.rs) - An example loading a configuration file. This must be ran with `cargo run --example config --features toml_config`
|
||||
|
||||
|
|
56
src/app.rs
56
src/app.rs
|
@ -1,4 +1,4 @@
|
|||
use crate::print_command_help;
|
||||
use crate::{print_command_help, vox};
|
||||
|
||||
use super::command::{help, Command, Handler};
|
||||
use super::context::Context;
|
||||
|
@ -172,7 +172,16 @@ impl App {
|
|||
if let Some(filename) = self.config_filename {
|
||||
ctx.load_config_file(filename);
|
||||
}
|
||||
run_command(self, &self.root, &args, &mut ctx)
|
||||
if let Err(e) = run_command(self, &self.root, &args, &mut ctx) {
|
||||
match e {
|
||||
OptError::InvalidOpt(opt) => {
|
||||
vox::error(format!("Invalid options {}", &opt));
|
||||
Err(OptError::InvalidOpt(opt))
|
||||
}
|
||||
}
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
/// Execute the app and any specified handlers based on the arguments passsed to the
|
||||
|
@ -228,22 +237,24 @@ fn run_command(app: &App, cmd: &Command, args: &[String], ctx: &mut Context) ->
|
|||
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()));
|
||||
if arg.starts_with("-") {
|
||||
for switch in arg.chars().skip(1) {
|
||||
if let Some(opt) = cmd.opts.iter().find(|o| o.short == switch.to_string()) {
|
||||
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());
|
||||
}
|
||||
} else {
|
||||
ignored.push(arg.clone());
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
@ -363,6 +374,21 @@ mod tests {
|
|||
.unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_combined_short_flags() {
|
||||
App::new()
|
||||
.opt(Opt::flag("thingA").short("a").long("a"))
|
||||
.opt(Opt::flag("thingB").short("b").long("b"))
|
||||
.opt(Opt::flag("thingC").short("c").long("c"))
|
||||
.handler(|_, ctx, _| {
|
||||
assert_eq!(ctx.flag("thingA"), true);
|
||||
assert_eq!(ctx.flag("thingB"), true);
|
||||
assert_eq!(ctx.flag("thingC"), true);
|
||||
})
|
||||
.run_with(vec!["-abc".into()])
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_invalid_long_flag() {
|
||||
let r = App::new()
|
||||
|
|
|
@ -31,5 +31,9 @@ pub fn print<T: Display>(s: T) {
|
|||
println!("{}", s);
|
||||
}
|
||||
|
||||
pub fn error<T: Display>(s: T) {
|
||||
println!("{}", style(s).red().bold());
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {}
|
||||
|
|
Loading…
Reference in New Issue