Argument doesn't conflict with subcommand even if it's set #5294
Answered
by
epage
nix-enthusiast
asked this question in
Q&A
-
Clap version: 4.4.14 use clap::{Arg, ArgAction, Command};
use itertools::Itertools;
use std::process::exit;
fn main() {
let matches = Command::new("cmd")
.subcommand_precedence_over_arg(true)
.args_conflicts_with_subcommands(true)
.arg_required_else_help(true)
.arg(
Arg::new("foo")
.short('f')
.long("foo")
.action(ArgAction::SetTrue)
.exclusive(true)
)
.subcommand(
Command::new("bar")
.short_flag('B')
.long_flag("bar")
.arg_required_else_help(true)
.arg(
Arg::new("baz")
.short('b')
.long("baz")
.action(ArgAction::Append)
.num_args(1..),
),
);
let a = matches.try_get_matches();
match &a {
Ok(ok) => {
if ok.get_flag("foo") {
println!("-f is used")
};
#[allow(clippy::disallowed_names)]
if let Some(bar) = ok.subcommand_matches("bar") {
if let Some(baz) = bar.get_many::<String>("baz")
{if baz.len() % 2 != 0 {
println!("error");
exit(1)
}
for (first, second) in baz.tuples() {
println!("{} {}", first, second)
}}
}
}
Err(err) => {
match err.kind() {
clap::error::ErrorKind::UnknownArgument => println!("Unknown Argument"),
clap::error::ErrorKind::ArgumentConflict => println!("Argument conflict"),
clap::error::ErrorKind::DisplayHelpOnMissingArgumentOrSubcommand => {
println!("Subcommand needs an argument")
}
clap::error::ErrorKind::InvalidValue => println!("Argument needs a value"),
clap::error::ErrorKind::DisplayHelp => {
println!("help");
exit(0)
}
clap::error::ErrorKind::InvalidSubcommand => println!("Invalid subcommand"),
_ => println!("Unknown error"),
}
exit(1)
}
}
} I used
|
Beta Was this translation helpful? Give feedback.
Answered by
epage
Jan 9, 2024
Replies: 1 comment 3 replies
-
This is a bug. Our handling of |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
nix-enthusiast
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is a bug. Our handling of
args_conflicts_with_subcommands
is exclusive to subcommand names and not flags and we do this by checking if the flag if an arg doesn't exist (https://github.com/clap-rs/clap/blob/master/clap_builder/src/parser/parser.rs#L493) which means it likely is also broken withsubcommand_precedence_over_arg