Skip to content

Commit

Permalink
Merge pull request #91 from t-rapp/opt-panic-docs
Browse files Browse the repository at this point in the history
Update documentation of opt_present() and other functions that might panic
  • Loading branch information
alexcrichton committed Sep 6, 2019
2 parents 286b2c9 + 03273d2 commit f2ecc31
Showing 1 changed file with 56 additions and 20 deletions.
76 changes: 56 additions & 20 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -799,23 +799,35 @@ impl Matches {
self.opt_vals(nm).into_iter().map(|(_, o)| o).next()
}
/// Returns true if an option was defined
pub fn opt_defined(&self, nm: &str) -> bool {
find_opt(&self.opts, &Name::from_str(nm)).is_some()
pub fn opt_defined(&self, name: &str) -> bool {
find_opt(&self.opts, &Name::from_str(name)).is_some()
}

/// Returns true if an option was matched.
pub fn opt_present(&self, nm: &str) -> bool {
!self.opt_vals(nm).is_empty()
///
/// # Panics
///
/// This function will panic if the option name is not defined.
pub fn opt_present(&self, name: &str) -> bool {
!self.opt_vals(name).is_empty()
}

/// Returns the number of times an option was matched.
pub fn opt_count(&self, nm: &str) -> usize {
self.opt_vals(nm).len()
///
/// # Panics
///
/// This function will panic if the option name is not defined.
pub fn opt_count(&self, name: &str) -> usize {
self.opt_vals(name).len()
}

/// Returns a vector of all the positions in which an option was matched.
pub fn opt_positions(&self, nm: &str) -> Vec<usize> {
self.opt_vals(nm).into_iter().map(|(pos, _)| pos).collect()
///
/// # Panics
///
/// This function will panic if the option name is not defined.
pub fn opt_positions(&self, name: &str) -> Vec<usize> {
self.opt_vals(name).into_iter().map(|(pos, _)| pos).collect()
}

/// Returns true if any of several options were matched.
Expand Down Expand Up @@ -918,8 +930,12 @@ impl Matches {
/// option.
///
/// Used when an option accepts multiple values.
pub fn opt_strs(&self, nm: &str) -> Vec<String> {
self.opt_vals(nm)
///
/// # Panics
///
/// This function will panic if the option name is not defined.
pub fn opt_strs(&self, name: &str) -> Vec<String> {
self.opt_vals(name)
.into_iter()
.filter_map(|(_, v)| match v {
Val(s) => Some(s),
Expand All @@ -932,8 +948,12 @@ impl Matches {
/// option, together with their positions.
///
/// Used when an option accepts multiple values.
pub fn opt_strs_pos(&self, nm: &str) -> Vec<(usize, String)> {
self.opt_vals(nm)
///
/// # Panics
///
/// This function will panic if the option name is not defined.
pub fn opt_strs_pos(&self, name: &str) -> Vec<(usize, String)> {
self.opt_vals(name)
.into_iter()
.filter_map(|(p, v)| match v {
Val(s) => Some((p, s)),
Expand All @@ -943,8 +963,12 @@ impl Matches {
}

/// Returns the string argument supplied to a matching option or `None`.
pub fn opt_str(&self, nm: &str) -> Option<String> {
match self.opt_val(nm) {
///
/// # Panics
///
/// This function will panic if the option name is not defined.
pub fn opt_str(&self, name: &str) -> Option<String> {
match self.opt_val(name) {
Some(Val(s)) => Some(s),
_ => None,
}
Expand All @@ -955,8 +979,12 @@ impl Matches {
/// Returns `None` if the option was not present, `def` if the option was
/// present but no argument was provided, and the argument if the option was
/// present and an argument was provided.
pub fn opt_default(&self, nm: &str, def: &str) -> Option<String> {
match self.opt_val(nm) {
///
/// # Panics
///
/// This function will panic if the option name is not defined.
pub fn opt_default(&self, name: &str, def: &str) -> Option<String> {
match self.opt_val(name) {
Some(Val(s)) => Some(s),
Some(_) => Some(def.to_string()),
None => None,
Expand All @@ -966,11 +994,15 @@ impl Matches {
/// Returns some matching value or `None`.
///
/// Similar to opt_str, also converts matching argument using FromStr.
pub fn opt_get<T>(&self, nm: &str) -> result::Result<Option<T>, T::Err>
///
/// # Panics
///
/// This function will panic if the option name is not defined.
pub fn opt_get<T>(&self, name: &str) -> result::Result<Option<T>, T::Err>
where
T: FromStr,
{
match self.opt_val(nm) {
match self.opt_val(name) {
Some(Val(s)) => Ok(Some(s.parse()?)),
Some(Given) => Ok(None),
None => Ok(None),
Expand All @@ -982,11 +1014,15 @@ impl Matches {
/// Similar to opt_default, except the two differences.
/// Instead of returning None when argument was not present, return `def`.
/// Instead of returning &str return type T, parsed using str::parse().
pub fn opt_get_default<T>(&self, nm: &str, def: T) -> result::Result<T, T::Err>
///
/// # Panics
///
/// This function will panic if the option name is not defined.
pub fn opt_get_default<T>(&self, name: &str, def: T) -> result::Result<T, T::Err>
where
T: FromStr,
{
match self.opt_val(nm) {
match self.opt_val(name) {
Some(Val(s)) => s.parse(),
Some(Given) => Ok(def),
None => Ok(def),
Expand Down

0 comments on commit f2ecc31

Please sign in to comment.