epage / clapng

A full featured, fast Command Line Argument Parser for Rust
docs.rs/clap
Apache License 2.0
0 stars 0 forks source link

`cmd help help` includes `-h` and global arguments but errors when you pass them in #222

Open epage opened 2 years ago

epage commented 2 years ago

Issue by epage Saturday Oct 16, 2021 at 00:57 GMT Originally opened as https://github.com/clap-rs/clap/issues/2892


Please complete the following tasks

Rust Version

rustc 1.55.0 (c8dfcfe04 2021-09-06)

Clap Version

master

Minimal reproducible code

fn main() {
    use clap::*;
    let mut app = App::new("test_app")
        .arg(Arg::new("config").short('c').global(true))
        .arg(Arg::new("v").short('v'))
        .subcommand(
            App::new("test")
                .about("Subcommand")
                .arg(Arg::new("debug").short('d')),
        );

    app.get_matches_mut();
}

Steps to reproduce the bug with the above code

cargo run -- help help

Actual Behaviour

test-clap-help 

Print this message or the help of the given subcommand(s)

USAGE:
    test-clap help [OPTIONS] [SUBCOMMAND]...

ARGS:
    <SUBCOMMAND>...    The subcommand whose help message to display

OPTIONS:
    -c            
    -h, --help    Print help information

Expected Behaviour

test-clap-help 

Print this message or the help of the given subcommand(s)

USAGE:
    test-clap help [OPTIONS] [SUBCOMMAND]...

ARGS:
    <SUBCOMMAND>...    The subcommand whose help message to display

or for being able to run cargo run -- help -c but that produces

error:  The subcommand '-c' wasn't recognized

USAGE:
    test-clap <subcommands>

For more information try --help

because we are operating on raw data without the parser having pulled out -c or -h, so we look those up as if they were subcommands which fails

Additional Context

This used to be worse but #2887 made some progress towards this.

This seems to be a regression compared to clap2

test-clap-help 
Prints this message or the help of the given subcommand(s)

USAGE:
    test-clap help [subcommand]...

ARGS:
    <subcommand>...    The subcommand whose help message to display

Debug Output

No response

epage commented 2 years ago

Comment by epage Saturday Oct 16, 2021 at 00:58 GMT


I recommend we defer this out of 3.0.

I put this in the 3.0 milestone because it is a regression. I'm assuming fixing this will not be a breaking change and this is a corner of a corner case, so I think its fine for the initial release. My only concern is what other issues might be lingering from the same root cause.

epage commented 2 years ago

Comment by pksunkara Saturday Oct 16, 2021 at 01:16 GMT


This might be because we manipulate help and version global args when starting up. But this subcommand gets added during parsing (which I don't think is good code). If we can move the subcommand adding to before, that should fix it.