-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
Open
Labels
A-deriveArea: #[derive]` macro APIArea: #[derive]` macro APIA-validatorsArea: ArgMatches validation logiArea: ArgMatches validation logiC-enhancementCategory: Raise on the bar on expectationsCategory: Raise on the bar on expectationsS-triageStatus: New; needs maintainer attention.Status: New; needs maintainer attention.
Description
Please complete the following tasks
- I have searched the discussions
- I have searched the open and rejected issues
Clap Version
master
Describe your use case
i have a building tool with several build backends.
depending on the used backend, it can have different other arguments.
example:
- select the build backend with
--driver docker, this unlocks other options like--dockerfile <name>. - when we instead use
--driver lxd, we have other options such as--lxc-raw "lxc.init.cmd = /bin/bash"
Describe the solution you'd like
this can be done very similar to Subcommands, but without enforcing the positional order.
this could be Subargs or something. definition wise, you could define them just like subcommands, in the above case
#[derive(Parser, Debug)]
#[command(name = "debmagic")]
struct Cli {
#[command(subcommand)]
command: Commands,
}
#[derive(Subcommand, Debug)]
pub enum Commands {
Build(BuildSubcommandArgs),
...
}
#[derive(Args, Debug)]
pub struct BuildSubcommandArgs {
#[arg(short, long)]
pub driver: Option<BuildDriverType>,
#[arg(short, long)]
pub source_dir: Option<PathBuf>,
}
#[derive(Subargs, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum, Serialize, Deserialize)]
pub enum BuildDriverType {
Docker(DockerDriverArgs),
Lxd(LxdDriverArgs),
}
#[derive(Args, Debug)]
pub struct DockerDriverSubargs {
#[arg(long)]
pub dockerfile: Option<String>,
...
}
#[derive(Args, Debug)]
pub struct LxdDriverSubargs {
#[arg(long)]
pub lxc_raw: Option<String>,
...
}Alternatives, if applicable
currently, you would
- either make the
drivera subcommand, which doesn't fit its optional nature and position independence. - or add the arguments at the same level as the driver selection, and then use ArgGroups. this doesn't model the variable relations and encapsulation properly, though.
#[derive(clap::Subcommand, Debug)]
enum Commands {
#[command(
group(
ArgGroup::new("docker_args")
.args(["docker_image"])
.conflicts_with("lxd_args")
),
group(
ArgGroup::new("lxd_args")
.args(["lxc_options"])
)
)]
Pack {
#[arg(long, value_enum)]
driver: Option<Driver>,
/// docker options
#[arg(long, group = "docker_args")]
docker_image: Option<String>,
...
/// lxd options
#[arg(long, group = "lxd_args")]
lxc_options: Option<String>,
....
},
}Additional Context
No response
Reactions are currently unavailable
Metadata
Metadata
Assignees
Labels
A-deriveArea: #[derive]` macro APIArea: #[derive]` macro APIA-validatorsArea: ArgMatches validation logiArea: ArgMatches validation logiC-enhancementCategory: Raise on the bar on expectationsCategory: Raise on the bar on expectationsS-triageStatus: New; needs maintainer attention.Status: New; needs maintainer attention.