clap-rs / clap

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

Prevent specifying global args multiple times #3938

Open cd-work opened 2 years ago

cd-work commented 2 years ago

Please complete the following tasks

Clap Version

3.2.8

Describe your use case

When making an argument global with Arg::global(true), it will be available to both the parent and child subcommand. To me this implies that this option is a single flag that can be modified from anywhere.

However when specifying the global twice (e.g. cmd --global x subcmd --global y), the parent's assignment of the variable will be discarded in favor of the subcommand's. While this definitely is a possible way to handle this edgecase, I think this command invocation indicates that the user is doing something wrong and it would be better if clap could tell the user that what he's doing doesn't make any sense.

In my specific scenario, I have a command which defaults to one of it subcommands, so cmd --flag x is the same as cmd list --flag x, while there's also other subcommands like cmd foo --flag x. One flag is shared between all subcommands (which I've tried making global), while some others are only available to cmd and cmd list. This would solve the issue of having an option that can be specified twice for that one shared flag, but I'd still need another solution preventing people from using the other cmd list-specific flags which can also be used with just cmd when another subcommand like cmd foo is used. So it's possible that this feature request is just a band-aid rather than solving the root issue.

Describe the solution you'd like

Emit an error when specifying the same global argument multiple times.

Alternatives, if applicable

No response

Additional Context

No response

cd-work commented 2 years ago

I'd still need another solution preventing people from using the other cmd list-specific flags which can also be used with just cmd when another subcommand like cmd foo is used. So it's possible that this feature request is just a band-aid rather than solving the root issue.

I've found the args_conflicts_with_subcommands option, so I guess that solves my root issue in a more general way. Though the error is rather... unfortunate.

epage commented 2 years ago

Just reading the description,