Clap: Error message for conflicted args outputs conflicting USAGE

Created on 5 May 2019  路  18Comments  路  Source: clap-rs/clap

Rust Version

rustc 1.34.1 (fc50f328b 2019-04-24)

Affected Version of clap

2.33.0

Bug or Feature Request Summary

There are 2 conflicting args: a and b. When both are issued, USAGE outputs the invalid command again.

Expected Behavior Summary

error: The argument '-b' cannot be used with '-a'

USAGE:
    clap-fix -a
    clap-fix -b
### Actual Behavior Summary
error: The argument '-b' cannot be used with '-a'

USAGE:
    clap-fix -a -b
### Steps to Reproduce the issue `cargo run -- -a -b` ### Sample Code or Link to Sample Code
extern crate clap;
use clap::{Arg, App};

fn main() {
    App::new("Conflict USAGE")
        .arg(Arg::with_name("a")
           .short("a")
           .conflicts_with("b")
        )
        .arg(Arg::with_name("b")
            .short("b")
            .conflicts_with("a")
        )
        .get_matches();
}
### Debug output
Debug Output


     Running `target/debug/clap-fix -a -b`
DEBUG:clap:Parser::propagate_settings: self=Conflict USAGE, g_settings=AppFlags(
    (empty)
)
DEBUG:clap:Parser::get_matches_with;
DEBUG:clap:Parser::create_help_and_version;
DEBUG:clap:Parser::create_help_and_version: Building --help
DEBUG:clap:Parser::create_help_and_version: Building --version
DEBUG:clap:Parser::get_matches_with: Begin parsing '"-a"' ([45, 97])
DEBUG:clap:Parser::is_new_arg:"-a":NotFound
DEBUG:clap:Parser::is_new_arg: arg_allows_tac=false
DEBUG:clap:Parser::is_new_arg: - found
DEBUG:clap:Parser::is_new_arg: starts_new_arg=true
DEBUG:clap:Parser::possible_subcommand: arg="-a"
DEBUG:clap:Parser::get_matches_with: possible_sc=false, sc=None
DEBUG:clap:ArgMatcher::process_arg_overrides:None;
DEBUG:clap:Parser::parse_short_arg: full_arg="-a"
DEBUG:clap:Parser::parse_short_arg:iter:a
DEBUG:clap:Parser::parse_short_arg:iter:a: Found valid flag
DEBUG:clap:Parser::check_for_help_and_version_char;
DEBUG:clap:Parser::check_for_help_and_version_char: Checking if -a is help or version...Neither
DEBUG:clap:Parser::parse_flag;
DEBUG:clap:ArgMatcher::inc_occurrence_of: arg=a
DEBUG:clap:ArgMatcher::inc_occurrence_of: first instance
DEBUG:clap:Parser::groups_for_arg: name=a
DEBUG:clap:Parser::groups_for_arg: No groups defined
DEBUG:clap:Parser:get_matches_with: After parse_short_arg Flag
DEBUG:clap:Parser::get_matches_with: Begin parsing '"-b"' ([45, 98])
DEBUG:clap:Parser::is_new_arg:"-b":Flag
DEBUG:clap:Parser::is_new_arg: arg_allows_tac=false
DEBUG:clap:Parser::is_new_arg: - found
DEBUG:clap:Parser::is_new_arg: starts_new_arg=true
DEBUG:clap:Parser::possible_subcommand: arg="-b"
DEBUG:clap:Parser::get_matches_with: possible_sc=false, sc=None
DEBUG:clap:ArgMatcher::process_arg_overrides:Some("a");
DEBUG:clap:Parser::parse_short_arg: full_arg="-b"
DEBUG:clap:Parser::parse_short_arg:iter:b
DEBUG:clap:Parser::parse_short_arg:iter:b: Found valid flag
DEBUG:clap:Parser::check_for_help_and_version_char;
DEBUG:clap:Parser::check_for_help_and_version_char: Checking if -b is help or version...Neither
DEBUG:clap:Parser::parse_flag;
DEBUG:clap:ArgMatcher::inc_occurrence_of: arg=b
DEBUG:clap:ArgMatcher::inc_occurrence_of: first instance
DEBUG:clap:Parser::groups_for_arg: name=b
DEBUG:clap:Parser::groups_for_arg: No groups defined
DEBUG:clap:Parser:get_matches_with: After parse_short_arg Flag
DEBUG:clap:ArgMatcher::process_arg_overrides:Some("b");
DEBUG:clap:Parser::remove_overrides:[];
DEBUG:clap:Validator::validate;
DEBUG:clap:Parser::add_defaults;
DEBUG:clap:Validator::validate_blacklist;
DEBUG:clap:Validator::validate_blacklist:iter:a;
DEBUG:clap:Parser::groups_for_arg: name=a
DEBUG:clap:Parser::groups_for_arg: No groups defined
DEBUG:clap:Validator::validate_blacklist:iter:b;
DEBUG:clap:Parser::groups_for_arg: name=b
DEBUG:clap:Parser::groups_for_arg: No groups defined
DEBUG:clap:Validator::validate_blacklist:iter:b: Checking blacklisted arg
DEBUG:clap:Validator::validate_blacklist:iter:b: matcher contains it...
DEBUG:clap:build_err!: name=b
DEBUG:clap:build_err!: 'Some("-a")' conflicts with 'b'
DEBUG:clap:usage::create_usage_with_title;
DEBUG:clap:usage::create_usage_no_title;
DEBUG:clap:usage::smart_usage;
DEBUG:clap:usage::get_required_usage_from: reqs=["a", "b"], extra=None
DEBUG:clap:usage::get_required_usage_from: after init desc_reqs=[]
DEBUG:clap:usage::get_required_usage_from: no more children
DEBUG:clap:usage::get_required_usage_from: final desc_reqs=["a", "b"]
DEBUG:clap:usage::get_required_usage_from: args_in_groups=[]
DEBUG:clap:usage::get_required_usage_from:iter:a:
DEBUG:clap:usage::get_required_usage_from:iter:b:
DEBUG:clap:build_err!: It was a flag...
DEBUG:clap:Parser::color;
DEBUG:clap:Parser::color: Color setting...Auto
DEBUG:clap:is_a_tty: stderr=true
DEBUG:clap:Colorizer::error;
DEBUG:clap:Colorizer::warning;
DEBUG:clap:Colorizer::warning;
DEBUG:clap:Colorizer::good;
error: The argument '-b' cannot be used with '-a'

USAGE:
    clap-fix -a -b


$10 help message easy need to have bug good first issue help wanted

All 18 comments

In fact, after reading the code, this behavior was actually already expected and almost tested!

This test:
https://github.com/clap-rs/clap/blob/784524f7eb193e35f81082cc69454c8c21b948f7/tests/conflicts.rs#L80-L83

and this one:
https://github.com/clap-rs/clap/blob/784524f7eb193e35f81082cc69454c8c21b948f7/tests/conflicts.rs#L85-L88

are both missing an assert!(...) surrounding the test::compare_output.

When I added the assert! myself both tests failed.

@jojva @CreepySkeleton Hi, can take this up?

Hi, on my side I'm not working on it, so go for it! (I feel like the current maintainers have a lot on their plate).

@captain-yossarian Go ahead :smile:

@pksunkara
Should I start from create_smart_usage method ?
Is it possible from this level to figure out which arguments are in conflict?

@captain-yossarian You seem to be working on the 2.33 branch, but you should be working on master instead.

@CreepySkeleton Thanks, I thought since there is beta on master it worth to work on 2.33

Fixing this in master will fix the upcoming 3.0, it won't affect 2.33. We are much more interested in future than in past.

Well, the code layout in 2.33 and the upcoming beta is different which means that if someone wants to backport the bugfix to 2.33, they would need to implement it from scratch against v2-master. I would accept such a PR, but I won't be putting efforts into it personally (I suspect the other maintainers feel the same).

@CreepySkeleton Ok, no problem.

So, on master I receive

error: The argument '-a' cannot be used with '-b'

USAGE:
    initial.exe [FLAGS]
USAGE:
    clap-fix -a
    clap-fix -b

[updated] Should I implement error message as above ?

Or [FLAGS] is some special keyword and requires some additional logic?
Sorry, I'm not aware of the project.

@CreepySkeleton Did we change something regarding the usage messages? Check the failing tests in the above PR.

No, we didn't. And it also haven't been working anyway, the test was not asserted. I think it's simply not implemented.

@captain-yossarian will going to have to trace where the erroneous message originates from and implement the right message.

Hey Captain! Feel free to ask for guidance!

There was a joke about a captain who doesn't guide but needs guidance instead here, but it was pretty gross and the censorship deleted it.

@CreepySkeleton Thanks a lot ! :)

This issue now has bounty of $10. More info here

@tomjw64 Please follow the redeeming procedure linked above to get the bounty.

@pksunkara If I forgo the bounty, will it be moved to a different issue?

Yes, the bounty will stay in the clap org balance.

Okay, I will forgo the bounty then. Thanks for your time :smile:

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Licenser picture Licenser  路  25Comments

kbknapp picture kbknapp  路  30Comments

joshtriplett picture joshtriplett  路  75Comments

casey picture casey  路  25Comments

kbknapp picture kbknapp  路  18Comments