docopt / docopt.rs

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

cargo.rs problem #83

Closed alec-c4 closed 9 years ago

alec-c4 commented 9 years ago

Hi, i try to compile cargo.rs (from examples dir) - everything is ok, but on attempt to run app without any argument i see error

Could not match '' with any of the allowed variants: ["Build", "Clean", "Doc", "New", "Run", "Test", "Bench", "Update"]

Whats wrong? I'm newbie in rust and i hope you can explain me how to solve my stupid issue :)

BurntSushi commented 9 years ago

Did you try running cargo -h? It will tell you what parameters you can pass:

[andrew@Liger docopt] cargo clean
# output elided
[andrew@Liger docopt] cargo test
# output elided
[andrew@Liger docopt] ./target/examples/
cargo             cp                decode            hashmap           verbose_multiple  
[andrew@Liger docopt] ./target/examples/cargo 
Could not match '' with any of the allowed variants: ["Build", "Clean", "Doc", "New", "Run", "Test", "Bench", "Update"]
[andrew@Liger docopt] ./target/examples/cargo -h
Rust's package manager

Usage:
    cargo <command> [<args>...]
    cargo [options]

Options:
    -h, --help       Display this message
    -V, --version    Print version info and exit
    --list           List installed commands
    -v, --verbose    Use verbose output

Some common cargo commands are:
    build       Compile the current project
    clean       Remove the target directory
    doc         Build this project's and its dependencies' documentation
    new         Create a new cargo project
    run         Build and execute src/main.rs
    test        Run the tests
    bench       Run the benchmarks
    update      Update dependencies listed in Cargo.lock

See 'cargo help <command>' for more information on a specific command.
[andrew@Liger docopt] ./target/examples/cargo build
Args { arg_command: Build, arg_args: [], flag_list: false, flag_verbose: false }

The issue is that the example requires you to pass a command. The error message is unfortunately a bit cryptic.

alec-c4 commented 9 years ago

yep, but what about to display usage text when no any command submitted?

BurntSushi commented 9 years ago

If you make this an Option<Command>, then you can detect if it's None and print the usage. But I don't think that's Docopt's job. From Docopt's perspective, if you don't match the Command, then it's a decoding error. If you want "no command" to be valid then you need to use Option<Command>.

alec-c4 commented 9 years ago

Thanks a lot!!!