Better support chaining multiple enums for subcommands without arguments #2004
-
In the wild, I often see command line applications that group related commands under a single subcommand that takes no arguments. Currently, this seems to be achievable in two ways. I have tried to illustrate them in the Example section. Now I would personally argue that the second way of implementing a subcommand without arguments is perfectly valid and prettier than the first one. However, it has the ugly flaw of panicking whenever the subcommand is called without another subcommand. This can be fixed by adding So would it maybe be possible, or even desirable, for clap to automatically behave as if Thank you for your hard work on clap! Example
Version 1First, the more verbose way. #[derive(Clap)]
pub struct App {
#[clap(long = "verbose")]
verbose: bool,
// ...
#[clap(subcommand)]
subcommand: Subcommand,
}
#[derive(Clap)]
pub struct Subcommand {
Config(Config)
// ...
}
#[derive(Clap)]
pub struct Config {
#[clap(subcommand)]
subcommand: ConfigSubcommand
}
#[derive(Clap, Debug)]
pub enum ConfigSubcommand {
Show,
Edit,
} Version 2But because the #[derive(Clap)]
pub struct App {
#[clap(long = "verbose")]
verbose: bool,
// ...
#[clap(subcommand)]
subcommand: Subcommand,
}
#[derive(Clap)]
pub struct Subcommand {
Config(Config)
// ...
}
#[derive(Clap, Debug)]
pub enum Config {
Show,
Edit,
} Version 2 (fixed)This problem can however be fixed by adding #[derive(Clap)]
pub struct App {
#[clap(long = "verbose")]
verbose: bool,
// ...
#[clap(subcommand)]
subcommand: Subcommand,
}
#[derive(Clap)]
pub struct Subcommand {
Config(Config)
// ...
}
#[derive(Clap, Debug)]
#[clap(setting = AppSettings::SubcommandRequiredElseHelp)]
pub enum Config {
Show,
Edit,
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 2 replies
-
And we actually do that so long as the subcommand in question isn't optional. This is a bug, could you please file an issue? |
Beta Was this translation helpful? Give feedback.
And we actually do that so long as the subcommand in question isn't optional. This is a bug, could you please file an issue?