Skip to content
This repository has been archived by the owner on Sep 14, 2023. It is now read-only.

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

Open
mrowqa opened this issue Jul 26, 2019 · 0 comments

Comments

@mrowqa
Copy link

mrowqa commented Jul 26, 2019

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") }
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant