Is it possible to have common argument for subcommands? #6192
-
|
I want my app to be able to process next line in exact order:
Where PATH is common argument for all commands, like global. |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 2 replies
-
|
This would be done by composition. For instance, if you are using the derive API, you could define a struct for I generally recommend being cautious of even what is marked as |
Beta Was this translation helpful? Give feedback.
-
|
Closest I get is, as git example. By repeating args all over the subcommands: pub fn cli() -> Command {
Command::new("pgweasel")
.about("A PostgreSQL log parser")
.version("0.1")
.arg(arg!(--debug <DEBUG>).short('d').help("Verbose. Show debug information").action(ArgAction::SetTrue))
.arg(arg!(--mask <MASK>).short('m').help("Postgres log timestamp mask (e.g. \"2025-05-21 12:57\" - will show all events at 12:57)"))
.arg(arg!(--begin <BEGIN>).short('b'))
.arg(arg!(--end <END>).short('e'))
.subcommand_required(true)
.subcommand(
Command::new("error")
.about("Show or summarize error messages")
.args_conflicts_with_subcommands(true)
.flatten_help(true)
.args(error_args())
.args(filelist_args())
.subcommand(Command::new("top").args(error_args()).args(filelist_args()))
)
}
fn error_args() -> Vec<Arg> {
vec![arg!(--level <LEVEL>)]
}
fn filelist_args() -> Vec<Arg> {
vec![arg!(<PATH> ..."Log files to analyze").value_parser(clap::value_parser!(PathBuf))]
}But now, when I read resulting cli, I have file list all over different places. |
Beta Was this translation helpful? Give feedback.
You enable
flatten_help(true)for theerrorsubcommand so that it will flatten any subcommands into that one. If you want to flatten your subcommands intopgweasel, you need to put it on the top levelCommand.From
flatten_helpAlso, notice how it is used in the git stash example