Skip to content

Commit f2ecc31

Browse files
authored
Merge pull request #91 from t-rapp/opt-panic-docs
Update documentation of opt_present() and other functions that might panic
2 parents 286b2c9 + 03273d2 commit f2ecc31

File tree

1 file changed

+56
-20
lines changed

1 file changed

+56
-20
lines changed

src/lib.rs

+56-20
Original file line numberDiff line numberDiff line change
@@ -799,23 +799,35 @@ impl Matches {
799799
self.opt_vals(nm).into_iter().map(|(_, o)| o).next()
800800
}
801801
/// Returns true if an option was defined
802-
pub fn opt_defined(&self, nm: &str) -> bool {
803-
find_opt(&self.opts, &Name::from_str(nm)).is_some()
802+
pub fn opt_defined(&self, name: &str) -> bool {
803+
find_opt(&self.opts, &Name::from_str(name)).is_some()
804804
}
805805

806806
/// Returns true if an option was matched.
807-
pub fn opt_present(&self, nm: &str) -> bool {
808-
!self.opt_vals(nm).is_empty()
807+
///
808+
/// # Panics
809+
///
810+
/// This function will panic if the option name is not defined.
811+
pub fn opt_present(&self, name: &str) -> bool {
812+
!self.opt_vals(name).is_empty()
809813
}
810814

811815
/// Returns the number of times an option was matched.
812-
pub fn opt_count(&self, nm: &str) -> usize {
813-
self.opt_vals(nm).len()
816+
///
817+
/// # Panics
818+
///
819+
/// This function will panic if the option name is not defined.
820+
pub fn opt_count(&self, name: &str) -> usize {
821+
self.opt_vals(name).len()
814822
}
815823

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

821833
/// Returns true if any of several options were matched.
@@ -918,8 +930,12 @@ impl Matches {
918930
/// option.
919931
///
920932
/// Used when an option accepts multiple values.
921-
pub fn opt_strs(&self, nm: &str) -> Vec<String> {
922-
self.opt_vals(nm)
933+
///
934+
/// # Panics
935+
///
936+
/// This function will panic if the option name is not defined.
937+
pub fn opt_strs(&self, name: &str) -> Vec<String> {
938+
self.opt_vals(name)
923939
.into_iter()
924940
.filter_map(|(_, v)| match v {
925941
Val(s) => Some(s),
@@ -932,8 +948,12 @@ impl Matches {
932948
/// option, together with their positions.
933949
///
934950
/// Used when an option accepts multiple values.
935-
pub fn opt_strs_pos(&self, nm: &str) -> Vec<(usize, String)> {
936-
self.opt_vals(nm)
951+
///
952+
/// # Panics
953+
///
954+
/// This function will panic if the option name is not defined.
955+
pub fn opt_strs_pos(&self, name: &str) -> Vec<(usize, String)> {
956+
self.opt_vals(name)
937957
.into_iter()
938958
.filter_map(|(p, v)| match v {
939959
Val(s) => Some((p, s)),
@@ -943,8 +963,12 @@ impl Matches {
943963
}
944964

945965
/// Returns the string argument supplied to a matching option or `None`.
946-
pub fn opt_str(&self, nm: &str) -> Option<String> {
947-
match self.opt_val(nm) {
966+
///
967+
/// # Panics
968+
///
969+
/// This function will panic if the option name is not defined.
970+
pub fn opt_str(&self, name: &str) -> Option<String> {
971+
match self.opt_val(name) {
948972
Some(Val(s)) => Some(s),
949973
_ => None,
950974
}
@@ -955,8 +979,12 @@ impl Matches {
955979
/// Returns `None` if the option was not present, `def` if the option was
956980
/// present but no argument was provided, and the argument if the option was
957981
/// present and an argument was provided.
958-
pub fn opt_default(&self, nm: &str, def: &str) -> Option<String> {
959-
match self.opt_val(nm) {
982+
///
983+
/// # Panics
984+
///
985+
/// This function will panic if the option name is not defined.
986+
pub fn opt_default(&self, name: &str, def: &str) -> Option<String> {
987+
match self.opt_val(name) {
960988
Some(Val(s)) => Some(s),
961989
Some(_) => Some(def.to_string()),
962990
None => None,
@@ -966,11 +994,15 @@ impl Matches {
966994
/// Returns some matching value or `None`.
967995
///
968996
/// Similar to opt_str, also converts matching argument using FromStr.
969-
pub fn opt_get<T>(&self, nm: &str) -> result::Result<Option<T>, T::Err>
997+
///
998+
/// # Panics
999+
///
1000+
/// This function will panic if the option name is not defined.
1001+
pub fn opt_get<T>(&self, name: &str) -> result::Result<Option<T>, T::Err>
9701002
where
9711003
T: FromStr,
9721004
{
973-
match self.opt_val(nm) {
1005+
match self.opt_val(name) {
9741006
Some(Val(s)) => Ok(Some(s.parse()?)),
9751007
Some(Given) => Ok(None),
9761008
None => Ok(None),
@@ -982,11 +1014,15 @@ impl Matches {
9821014
/// Similar to opt_default, except the two differences.
9831015
/// Instead of returning None when argument was not present, return `def`.
9841016
/// Instead of returning &str return type T, parsed using str::parse().
985-
pub fn opt_get_default<T>(&self, nm: &str, def: T) -> result::Result<T, T::Err>
1017+
///
1018+
/// # Panics
1019+
///
1020+
/// This function will panic if the option name is not defined.
1021+
pub fn opt_get_default<T>(&self, name: &str, def: T) -> result::Result<T, T::Err>
9861022
where
9871023
T: FromStr,
9881024
{
989-
match self.opt_val(nm) {
1025+
match self.opt_val(name) {
9901026
Some(Val(s)) => s.parse(),
9911027
Some(Given) => Ok(def),
9921028
None => Ok(def),

0 commit comments

Comments
 (0)