docopt.rs icon indicating copy to clipboard operation
docopt.rs copied to clipboard

Optional argument parsed as None instead of Some(_) if using '_' in option name

Open mrowqa opened this issue 6 years ago • 0 comments

The following example:

use docopt::Docopt;
use serde::Deserialize;

const USAGE: &'static str = "
Test.

Usage:
  test [--cache_dir=<cache_dir>]

Options:
  --cache_dir=<cache_dir>  enables cache
";

#[derive(Debug, Deserialize)]
struct Args {
    flag_cache_dir: Option<String>,
}

fn main() {
    let args: Args = Docopt::new(USAGE)
        .and_then(|d| d.deserialize())
        .unwrap_or_else(|e| e.exit());
    println!("{:?}", args);
}

Gives result:

$ cargo run -- --cache_dir=abc
    Finished dev [unoptimized + debuginfo] target(s) in 0.03s
     Running `target\debug\docopt-test.exe --cache_dir=abc`
Args { flag_cache_dir: None }

If option name is changed to --cache-dir, then it parses the optional argument correctly:

Test.

Usage:
  test [--cache-dir=<cache_dir>]

Options:
  --cache-dir=<cache_dir>  enables cache
$ cargo run -- --cache-dir=abc
    Finished dev [unoptimized + debuginfo] target(s) in 0.04s
     Running `target\debug\docopt-test.exe --cache-dir=abc`
Args { flag_cache_dir: Some("abc") }

mrowqa avatar Jul 26 '19 22:07 mrowqa