Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(assert): Tweak bad arg panics #3820

Merged
merged 2 commits into from Jun 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -19,6 +19,7 @@ _gated behind `unstable-v4`_
- Verify `required` is not used with conditional required settings (#3660)
- Disallow more `value_names` than `number_of_values` (#2695)
- *(assert)* Always enforce that version is specified when the `ArgAction::Version` is used
- *(assert)* Add missing `#[track_caller]`s to make it easier to debug asserts
- *(help)* Use `Command::display_name` in the help title rather than `Command::bin_name`
- *(parser)* Assert on unknown args when using external subcommands (#3703)
- *(parser)* Always fill in `""` argument for external subcommands (#3263)
Expand Down
13 changes: 11 additions & 2 deletions src/parser/matches/arg_matches.rs
Expand Up @@ -459,6 +459,7 @@ impl ArgMatches {

/// Deprecated, replaced with [`ArgMatches::get_one()`]
#[deprecated(since = "3.2.0", note = "Replaced with `ArgMatches::get_one()`")]
#[cfg_attr(debug_assertions, track_caller)]
pub fn value_of_t<R>(&self, name: &str) -> Result<R, Error>
where
R: FromStr,
Expand All @@ -480,6 +481,7 @@ impl ArgMatches {

/// Deprecated, replaced with [`ArgMatches::get_one()`]
#[deprecated(since = "3.2.0", note = "Replaced with `ArgMatches::get_one()`")]
#[cfg_attr(debug_assertions, track_caller)]
pub fn value_of_t_or_exit<R>(&self, name: &str) -> R
where
R: FromStr,
Expand All @@ -491,6 +493,7 @@ impl ArgMatches {

/// Deprecated, replaced with [`ArgMatches::get_many()`]
#[deprecated(since = "3.2.0", note = "Replaced with `ArgMatches::get_many()`")]
#[cfg_attr(debug_assertions, track_caller)]
pub fn values_of_t<R>(&self, name: &str) -> Result<Vec<R>, Error>
where
R: FromStr,
Expand All @@ -512,6 +515,7 @@ impl ArgMatches {

/// Deprecated, replaced with [`ArgMatches::get_many()`]
#[deprecated(since = "3.2.0", note = "Replaced with `ArgMatches::get_many()`")]
#[cfg_attr(debug_assertions, track_caller)]
pub fn values_of_t_or_exit<R>(&self, name: &str) -> Vec<R>
where
R: FromStr,
Expand All @@ -527,6 +531,7 @@ impl ArgMatches {
since = "3.2.0",
note = "Replaced with either `ArgAction::SetTrue` or `ArgMatches::contains_id(...)`"
)]
#[cfg_attr(debug_assertions, track_caller)]
pub fn is_present<T: Key>(&self, id: T) -> bool {
let id = Id::from(id);

Expand Down Expand Up @@ -557,6 +562,7 @@ impl ArgMatches {
/// ```
///
/// [`default_value`]: crate::Arg::default_value()
#[cfg_attr(debug_assertions, track_caller)]
pub fn value_source<T: Key>(&self, id: T) -> Option<ValueSource> {
let id = Id::from(id);

Expand All @@ -571,6 +577,7 @@ impl ArgMatches {
since = "3.2.0",
note = "Replaced with either `ArgAction::Count` or `ArgMatches::get_many(...).len()`"
)]
#[cfg_attr(debug_assertions, track_caller)]
pub fn occurrences_of<T: Key>(&self, id: T) -> u64 {
#![allow(deprecated)]
self.get_arg(&Id::from(id))
Expand Down Expand Up @@ -711,6 +718,7 @@ impl ArgMatches {
/// assert_eq!(m.indices_of("option").unwrap().collect::<Vec<_>>(), &[2, 3, 4]);
/// ```
/// [delimiter]: crate::Arg::value_delimiter()
#[cfg_attr(debug_assertions, track_caller)]
pub fn index_of<T: Key>(&self, id: T) -> Option<usize> {
let arg = self.get_arg(&Id::from(id))?;
let i = arg.get_index(0)?;
Expand Down Expand Up @@ -793,6 +801,7 @@ impl ArgMatches {
/// ```
/// [`ArgMatches::index_of`]: ArgMatches::index_of()
/// [delimiter]: Arg::value_delimiter()
#[cfg_attr(debug_assertions, track_caller)]
pub fn indices_of<T: Key>(&self, id: T) -> Option<Indices<'_>> {
let arg = self.get_arg(&Id::from(id))?;
let i = Indices {
Expand Down Expand Up @@ -1199,7 +1208,7 @@ impl ArgMatches {
return Err(MatchesError::UnknownArgument {});
} else {
debug!(
"`{:?}` is not a name of an argument or a group.\n\
"`{:?}` is not an id of an argument or a group.\n\
Make sure you're using the name of the argument itself \
and not the name of short or long flags.",
_arg
Expand All @@ -1223,7 +1232,7 @@ impl ArgMatches {
);
} else {
panic!(
"`{:?}` is not a name of an argument or a group.\n\
"`{:?}` is not an id of an argument or a group.\n\
Make sure you're using the name of the argument itself \
and not the name of short or long flags.",
arg
Expand Down
4 changes: 2 additions & 2 deletions tests/builder/legacy/arg_matcher_assertions.rs
Expand Up @@ -3,7 +3,7 @@ use clap::{Arg, Command};

#[test]
#[cfg(debug_assertions)]
#[should_panic = "`f` is not a name of an argument or a group."]
#[should_panic = "`f` is not an id of an argument or a group."]
fn arg_matches_if_present_wrong_arg() {
let m = Command::new("test")
.arg(Arg::new("flag").short('f'))
Expand All @@ -16,7 +16,7 @@ fn arg_matches_if_present_wrong_arg() {

#[test]
#[cfg(debug_assertions)]
#[should_panic = "`o` is not a name of an argument or a group."]
#[should_panic = "`o` is not an id of an argument or a group."]
fn arg_matches_value_of_wrong_arg() {
let m = Command::new("test")
.arg(Arg::new("opt").short('o').takes_value(true))
Expand Down