Skip to content

Commit

Permalink
fix(parser): Loosen asserts for now
Browse files Browse the repository at this point in the history
This is a surgical workaround for #3263.  It makes `cargo` pass tests!
  • Loading branch information
epage committed Jan 5, 2022
1 parent 2074c56 commit 8a81a87
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 5 deletions.
8 changes: 7 additions & 1 deletion src/parse/arg_matcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::{collections::HashMap, ffi::OsString, mem, ops::Deref};

// Internal
use crate::{
build::{App, Arg, ArgSettings},
build::{App, AppSettings, Arg, ArgSettings},
parse::{ArgMatches, MatchedArg, SubCommand, ValueType},
util::Id,
};
Expand All @@ -25,6 +25,12 @@ impl ArgMatcher {
},
#[cfg(debug_assertions)]
valid_subcommands: _app.subcommands.iter().map(|sc| sc.id.clone()).collect(),
// HACK: Allow an external subcommand's ArgMatches be a stand-in for any ArgMatches
// since users can't detect it and avoid the asserts.
//
// See clap-rs/clap#3263
#[cfg(debug_assertions)]
disable_asserts: _app.is_set(AppSettings::AllowExternalSubcommands),
..Default::default()
})
}
Expand Down
13 changes: 9 additions & 4 deletions src/parse/matches/arg_matches.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,8 @@ pub struct ArgMatches {
pub(crate) valid_args: Vec<Id>,
#[cfg(debug_assertions)]
pub(crate) valid_subcommands: Vec<Id>,
#[cfg(debug_assertions)]
pub(crate) disable_asserts: bool,
pub(crate) args: IndexMap<Id, MatchedArg>,
pub(crate) subcommand: Option<Box<SubCommand>>,
}
Expand Down Expand Up @@ -1005,7 +1007,7 @@ impl ArgMatches {
#[cfg(debug_assertions)]
{
let id = Id::from(_id);
id == Id::empty_hash() || self.valid_args.contains(&id)
self.disable_asserts || id == Id::empty_hash() || self.valid_args.contains(&id)
}
#[cfg(not(debug_assertions))]
{
Expand All @@ -1024,7 +1026,7 @@ impl ArgMatches {
#[cfg(debug_assertions)]
{
let id = Id::from(_id);
id == Id::empty_hash() || self.valid_subcommands.contains(&id)
self.disable_asserts || id == Id::empty_hash() || self.valid_subcommands.contains(&id)
}
#[cfg(not(debug_assertions))]
{
Expand All @@ -1040,7 +1042,7 @@ impl ArgMatches {
fn get_arg(&self, arg: &Id) -> Option<&MatchedArg> {
#[cfg(debug_assertions)]
{
if *arg == Id::empty_hash() || self.valid_args.contains(arg) {
if self.disable_asserts || *arg == Id::empty_hash() || self.valid_args.contains(arg) {
} else if self.valid_subcommands.contains(arg) {
panic!(
"Subcommand `{:?}` used where an argument or group name was expected.",
Expand All @@ -1064,7 +1066,10 @@ impl ArgMatches {
fn get_subcommand(&self, id: &Id) -> Option<&SubCommand> {
#[cfg(debug_assertions)]
{
if *id == Id::empty_hash() || self.valid_subcommands.contains(id) {
if self.disable_asserts
|| *id == Id::empty_hash()
|| self.valid_subcommands.contains(id)
{
} else if self.valid_args.contains(id) {
panic!(
"Argument or group `{:?}` used where a subcommand name was expected.",
Expand Down

0 comments on commit 8a81a87

Please sign in to comment.