docopt / docopt.rs

Docopt for Rust (command line argument parser).
The Unlicense
754 stars 83 forks source link

Positional arguments are misparsed as --required arguments #258

Open jyn514 opened 3 years ago

jyn514 commented 3 years ago
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 commented 3 years ago

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 commented 3 years ago

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.

BurntSushi commented 3 years ago

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

jyn514 commented 3 years ago

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.