diff options
Diffstat (limited to 'src/main.rs')
| -rw-r--r-- | src/main.rs | 31 |
1 files changed, 17 insertions, 14 deletions
diff --git a/src/main.rs b/src/main.rs index 347e144..4cfbcc7 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,18 @@ mod args; -use crate::args::Parser; use std::process::ExitCode; +fn guess_parser(args: &mut dyn Iterator<Item = String>) -> Box<dyn args::Parser> { + for arg in args { + if arg.len() > 2 && arg.starts_with("--") { + // --C.. can only be ShortAndLong + return Box::new(args::ShortAndLongParser::new()); + } + } + // TODO: Can we make more guesses? + Box::new(args::LongOnlyParser::new()) +} + fn parse_args() -> Result<bool, String> { let mut options = args::Options::new(); let help_idx = options.push( @@ -28,32 +38,25 @@ fn parse_args() -> Result<bool, String> { .build() .unwrap(), ); - let test_idx = options.push( - args::OptionBuilder::default() - .short('t') - .long("test") - .description("testing") - .value(args::ValueRequirement::Required("FILE")) - .build() - .unwrap(), - ); - let parser = args::ShortAndLongParser::new(); - let args = parser.run(&mut options, std::env::args())?; + let parser = guess_parser(&mut std::env::args()); + let maybe_args = parser.run(&mut options, &mut std::env::args()); + + // help is special, check for it even if parser#run returned error. let ref help = options[help_idx]; if help.is_set() { parser.print_help(&options); return Ok(true); } + + let args = maybe_args?; let ref version = options[version_idx]; if version.is_set() { println!("Version is 0.0.1"); return Ok(true); } let ref verbose = options[verbose_idx]; - let ref test = options[test_idx]; println!("verbose: {}", verbose.is_set()); - println!("test: {:?}", test.value()); println!("args: {:?}", args.args); Ok(false) } |
