-
Hi, I stumbled on a behavior that I did not understand. Here is a minimal reproduction: use clap::{CommandFactory, FromArgMatches};
#[derive(clap::Parser, Clone, Debug)]
struct Args {
#[arg(long)]
bool_flag: bool,
}
fn main() {
let app = Args::command().ignore_errors(true);
let args = vec!["my-app", "--bool-flag", "whatever"];
let matches = app
.clone()
.allow_external_subcommands(true)
.get_matches_from(&args);
println!(
"set with allow_external_subcommands: {:?}",
matches.get_one::<bool>("bool_flag")
);
let matches = app.clone().get_matches_from(&args);
println!(
"set without allow_external_subcommands: {:?}",
matches.get_one::<bool>("bool_flag")
);
let args = vec!["my-app", "whatever"];
let matches = app
.clone()
.allow_external_subcommands(true)
.get_matches_from(&args);
println!(
"unset with allow_external_subcommands: {:?}",
matches.get_one::<bool>("bool_flag")
);
let matches = app.clone().get_matches_from(&args);
println!(
"unset without allow_external_subcommands: {:?}",
matches.get_one::<bool>("bool_flag")
);
Args::from_arg_matches(&matches).unwrap();
} and the output of that is:
My question is, why does For completeness, here's the context of what I'm trying to do, though it's not that relevant to the question: contextI'm working on dynamic completions for jj. Because of reasons, we shell out to ourselves in the custom completer functions. There are a couple global flags, like --repository, which are important to pass along to the subprocess. So we try to just parse those global args to pass them along.My initial code was crashing like the above example. I just started experimenting with how I could get clap to parse my boolean flags as default-false if unset, which is what I would expect in any case. I landed on allow_external_subcommands, which seems to solve my problem, but I have no idea why it does that. Please do let me know if I'm "holding it wrong" 🙂 |
Beta Was this translation helpful? Give feedback.
Answered by
epage
Nov 11, 2024
Replies: 1 comment 3 replies
-
To more clearly see whats happening, I've switched it to using the builder API #!/usr/bin/env nargo
---
[dependencies]
clap = { version = "4", features = ["derive"] }
---
use clap::{CommandFactory, FromArgMatches, Command, Arg, builder::ArgAction};
#[derive(clap::Parser, Clone, Debug)]
struct Args {
#[arg(long)]
bool_flag: bool,
}
fn app() -> clap::Command {
Command::new("foo")
.arg(Arg::new("bool_flag").long("bool-flag").action(ArgAction::SetTrue))
.ignore_errors(true)
}
fn main() {
let args = vec!["my-app", "--bool-flag", "whatever"];
let matches = app()
.allow_external_subcommands(true)
.get_matches_from(&args);
println!(
"set with allow_external_subcommands: {:?}",
matches.get_one::<bool>("bool_flag")
);
let matches = app().get_matches_from(&args);
println!(
"set without allow_external_subcommands: {:?}",
matches.get_one::<bool>("bool_flag")
);
let args = vec!["my-app", "whatever"];
let matches = app()
.allow_external_subcommands(true)
.get_matches_from(&args);
println!(
"unset with allow_external_subcommands: {:?}",
matches.get_one::<bool>("bool_flag")
);
let matches = app().get_matches_from(&args);
println!(
"unset without allow_external_subcommands: {:?}",
matches.get_one::<bool>("bool_flag")
);
Args::from_arg_matches(&matches).unwrap();
} |
Beta Was this translation helpful? Give feedback.
3 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
The issue is more with
ignore_errors
which has a lot of unspecified behavior