@@ -799,23 +799,35 @@ impl Matches {
799
799
self . opt_vals ( nm) . into_iter ( ) . map ( |( _, o) | o) . next ( )
800
800
}
801
801
/// 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 ( )
804
804
}
805
805
806
806
/// 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 ( )
809
813
}
810
814
811
815
/// 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 ( )
814
822
}
815
823
816
824
/// 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 ( )
819
831
}
820
832
821
833
/// Returns true if any of several options were matched.
@@ -918,8 +930,12 @@ impl Matches {
918
930
/// option.
919
931
///
920
932
/// 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)
923
939
. into_iter ( )
924
940
. filter_map ( |( _, v) | match v {
925
941
Val ( s) => Some ( s) ,
@@ -932,8 +948,12 @@ impl Matches {
932
948
/// option, together with their positions.
933
949
///
934
950
/// 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)
937
957
. into_iter ( )
938
958
. filter_map ( |( p, v) | match v {
939
959
Val ( s) => Some ( ( p, s) ) ,
@@ -943,8 +963,12 @@ impl Matches {
943
963
}
944
964
945
965
/// 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) {
948
972
Some ( Val ( s) ) => Some ( s) ,
949
973
_ => None ,
950
974
}
@@ -955,8 +979,12 @@ impl Matches {
955
979
/// Returns `None` if the option was not present, `def` if the option was
956
980
/// present but no argument was provided, and the argument if the option was
957
981
/// 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) {
960
988
Some ( Val ( s) ) => Some ( s) ,
961
989
Some ( _) => Some ( def. to_string ( ) ) ,
962
990
None => None ,
@@ -966,11 +994,15 @@ impl Matches {
966
994
/// Returns some matching value or `None`.
967
995
///
968
996
/// 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 >
970
1002
where
971
1003
T : FromStr ,
972
1004
{
973
- match self . opt_val ( nm ) {
1005
+ match self . opt_val ( name ) {
974
1006
Some ( Val ( s) ) => Ok ( Some ( s. parse ( ) ?) ) ,
975
1007
Some ( Given ) => Ok ( None ) ,
976
1008
None => Ok ( None ) ,
@@ -982,11 +1014,15 @@ impl Matches {
982
1014
/// Similar to opt_default, except the two differences.
983
1015
/// Instead of returning None when argument was not present, return `def`.
984
1016
/// 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 >
986
1022
where
987
1023
T : FromStr ,
988
1024
{
989
- match self . opt_val ( nm ) {
1025
+ match self . opt_val ( name ) {
990
1026
Some ( Val ( s) ) => s. parse ( ) ,
991
1027
Some ( Given ) => Ok ( def) ,
992
1028
None => Ok ( def) ,
0 commit comments