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

Positional arguments are misparsed as --required arguments

Open jyn514 opened this issue 3 years ago • 4 comments

const MAIN_USAGE: &str = "
Usage:
    cargo deadlinks [--dir <directory>]
";

fn main() {
    let args: MainArgs = Docopt::new(MAIN_USAGE)
        .and_then(|d| {
            d.version(Some(env!("CARGO_PKG_VERSION").to_owned()))
                .deserialize()
        })
        .unwrap_or_else(|e| e.exit());
    dbg!(&args);
$ /home/joshua/.local/lib/cargo/target/debug/cargo-deadlinks deadlinks -- --document-private
[src/bin/cargo-deadlinks.rs:46] &args = MainArgs {
    arg_directory: Some(
        "--document-private",
    ),
}

This is wrong - docopt should either exit with an error or ignore the flag altogether (my preference is to exit with an error).

jyn514 avatar Nov 24 '20 04:11 jyn514

Hmm, looking at http://docopt.org/ it looks like this is intended behavior, since [--dir <directory>] is the same as [--dir] [<directory>]? Why is the -- being ignored, though?

jyn514 avatar Nov 24 '20 04:11 jyn514

For context, this is what I originally wanted to do:

Usage:
    cargo deadlinks [--dir <directory>] [options] [-- <cargo_arguments>...]

but cargo deadlinks -- --document-private-items was treating document-private as the <directory>, not part of cargo_arguments.

jyn514 avatar Nov 24 '20 04:11 jyn514

I don't know, sorry. I haven't used docopt in years. In your last issue, I pointed you to the project status.

I guess that wasn't enough to scare you off. I've emboldened the message: https://github.com/docopt/docopt.rs#this-crate-is-unmaintained

BurntSushi avatar Nov 24 '20 13:11 BurntSushi

Ok, no problem. I hadn't switched because I hadn't had the time. Last night I ended up rewriting this with pico args and it turned out pretty well I think: https://github.com/deadlinks/cargo-deadlinks/pull/116

Feel free to close this if you don't plan to fix it.

jyn514 avatar Nov 24 '20 13:11 jyn514