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

argument name with _ underscore is misaligned to - hyphen #229

Open
rbuckland opened this issue Aug 29, 2017 · 1 comment
Open

argument name with _ underscore is misaligned to - hyphen #229

rbuckland opened this issue Aug 29, 2017 · 1 comment

Comments

@rbuckland
Copy link

When an argument has an _ in the docopt, deserialising it to the Args struct fails as it is looking for an argument with an _

Example error

18:10 $ cargo run -- /
    Finished dev [unoptimized + debuginfo] target(s) in 0.0 secs
     Running `target/debug/bar /`
Could not find argument '<foo-path>' (from struct field 'arg_foo_path').
Note that each struct field must have the right key prefix, which must
be one of `cmd_`, `flag_` or `arg_`.

Code sample causing error

#[macro_use]
extern crate serde_derive;
extern crate docopt;

use docopt::Docopt;

const USAGE: &'static str = "
JSON XPather

Usage:
  bar <foo_path>

";

#[derive(Debug, Deserialize)]
struct Args {
    arg_foo_path: String,
}


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

rbuckland commented Aug 29, 2017

Here is the cause -

name.replace("_", "-")

and the workaround is to "change" docopt arguments (variables) to be - hyphened versions

eg:

Usage:
  bar <foo-path>

instead of

Usage:
  bar <foo_path>
#[macro_use]
extern crate serde_derive;
extern crate docopt;

use docopt::Docopt;

const USAGE: &'static str = "
JSON XPather

Usage:
  bar <foo-path>

";

#[derive(Debug, Deserialize)]
struct Args {
    arg_foo_path: String,
}


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

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