mirror of
https://github.com/clap-rs/clap
synced 2024-12-14 06:42:33 +00:00
Merge pull request #3255 from epage/reflect
feat(parser): Allow users to avoid undefined arg asserts
This commit is contained in:
commit
0a7e751c21
1 changed files with 38 additions and 0 deletions
|
@ -993,6 +993,44 @@ impl ArgMatches {
|
|||
pub fn subcommand_name(&self) -> Option<&str> {
|
||||
self.subcommand.as_ref().map(|sc| &*sc.name)
|
||||
}
|
||||
|
||||
/// Check if an arg can be queried
|
||||
///
|
||||
/// By default, `ArgMatches` functions assert on undefined `Id`s to help catch programmer
|
||||
/// mistakes. In some context, this doesn't work, so users can use this function to check
|
||||
/// before they do a query on `ArgMatches`.
|
||||
#[inline]
|
||||
#[doc(hidden)]
|
||||
pub fn is_valid_arg(&self, _id: impl Key) -> bool {
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
let id = Id::from(_id);
|
||||
id == Id::empty_hash() || self.valid_args.contains(&id)
|
||||
}
|
||||
#[cfg(not(debug_assertions))]
|
||||
{
|
||||
true
|
||||
}
|
||||
}
|
||||
|
||||
/// Check if a subcommand can be queried
|
||||
///
|
||||
/// By default, `ArgMatches` functions assert on undefined `Id`s to help catch programmer
|
||||
/// mistakes. In some context, this doesn't work, so users can use this function to check
|
||||
/// before they do a query on `ArgMatches`.
|
||||
#[inline]
|
||||
#[doc(hidden)]
|
||||
pub fn is_valid_subcommand(&self, _id: impl Key) -> bool {
|
||||
#[cfg(debug_assertions)]
|
||||
{
|
||||
let id = Id::from(_id);
|
||||
id == Id::empty_hash() || self.valid_subcommands.contains(&id)
|
||||
}
|
||||
#[cfg(not(debug_assertions))]
|
||||
{
|
||||
true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Private methods
|
||||
|
|
Loading…
Reference in a new issue