@@ -71,6 +71,116 @@ fn double_hyphen_as_value() {
7171 ) ;
7272}
7373
74+ #[ test]
75+ fn double_hyphen_not_a_value_when_disabled ( ) {
76+ let res = Command :: new ( "prog" )
77+ . arg (
78+ Arg :: new ( "value" )
79+ . action ( ArgAction :: Set )
80+ . allow_hyphen_values ( true )
81+ . allow_dash_dash_as_value ( false )
82+ . long ( "value" ) ,
83+ )
84+ . try_get_matches_from ( vec ! [ "prog" , "--value" , "--" ] ) ;
85+ assert ! ( res. is_err( ) ) ;
86+ assert_eq ! ( res. unwrap_err( ) . kind( ) , ErrorKind :: MissingRequiredArgument ) ;
87+ }
88+
89+ #[ test]
90+ fn double_hyphen_as_terminator_after_flag ( ) {
91+ let matches = Command :: new ( "prog" )
92+ . arg (
93+ Arg :: new ( "flag" )
94+ . action ( ArgAction :: SetTrue )
95+ . long ( "flag" ) ,
96+ )
97+ . try_get_matches_from ( vec ! [ "prog" , "--flag" , "--" ] )
98+ . unwrap ( ) ;
99+
100+ assert_eq ! ( matches. get_one:: <bool >( "flag" ) , Some ( & true ) ) ;
101+ }
102+
103+ #[ test]
104+ fn double_hyphen_as_terminator_between_two_flags ( ) {
105+ let matches = Command :: new ( "prog" )
106+ . arg ( Arg :: new ( "first" ) . action ( ArgAction :: SetTrue ) . long ( "first" ) )
107+ . arg ( Arg :: new ( "second" ) . action ( ArgAction :: SetTrue ) . long ( "second" ) )
108+ . try_get_matches_from ( vec ! [ "prog" , "--first" , "--" , "--second" ] ) ;
109+
110+ assert ! ( matches. is_err( ) ) ;
111+ assert_eq ! ( matches. unwrap_err( ) . kind( ) , ErrorKind :: UnknownArgument ) ;
112+ }
113+
114+ #[ test]
115+ fn double_hyphen_as_terminator_between_two_flags_before_last_positional ( ) {
116+ let matches = Command :: new ( "prog" )
117+ . arg ( Arg :: new ( "first" ) . action ( ArgAction :: SetTrue ) . long ( "first" ) )
118+ . arg ( Arg :: new ( "second" ) . action ( ArgAction :: SetTrue ) . long ( "second" ) )
119+ . arg ( Arg :: new ( "remaining" ) . num_args ( 0 ..) . last ( true ) )
120+ . try_get_matches_from ( vec ! [ "prog" , "--first" , "--" , "--second" ] )
121+ . unwrap ( ) ;
122+
123+ assert_eq ! ( matches. get_one:: <bool >( "first" ) , Some ( & true ) ) ;
124+ assert_eq ! ( matches. get_one:: <bool >( "second" ) , Some ( & false ) ) ;
125+ let remaining: Vec < _ > = matches
126+ . get_many :: < String > ( "remaining" )
127+ . into_iter ( )
128+ . flatten ( )
129+ . map ( |s| s. as_str ( ) )
130+ . collect ( ) ;
131+ assert_eq ! ( remaining, [ "--second" ] ) ;
132+ }
133+
134+ #[ test]
135+ fn double_hyphen_as_terminator_between_two_flags_before_last_positional_reversed ( ) {
136+ let matches = Command :: new ( "prog" )
137+ . arg ( Arg :: new ( "first" ) . action ( ArgAction :: SetTrue ) . long ( "first" ) )
138+ . arg ( Arg :: new ( "second" ) . action ( ArgAction :: SetTrue ) . long ( "second" ) )
139+ . arg ( Arg :: new ( "remaining" ) . num_args ( 0 ..) . last ( true ) )
140+ . try_get_matches_from ( vec ! [ "prog" , "--second" , "--" , "--first" ] )
141+ . unwrap ( ) ;
142+
143+ assert_eq ! ( matches. get_one:: <bool >( "first" ) , Some ( & false ) ) ;
144+ assert_eq ! ( matches. get_one:: <bool >( "second" ) , Some ( & true ) ) ;
145+ let remaining: Vec < _ > = matches
146+ . get_many :: < String > ( "remaining" )
147+ . into_iter ( )
148+ . flatten ( )
149+ . map ( |s| s. as_str ( ) )
150+ . collect ( ) ;
151+ assert_eq ! ( remaining, [ "--first" ] ) ;
152+ }
153+
154+ #[ test]
155+ fn double_hyphen_as_terminator_between_two_opts_before_last_positional ( ) {
156+ let matches = Command :: new ( "prog" )
157+ . arg (
158+ Arg :: new ( "first" )
159+ . action ( ArgAction :: Set )
160+ . allow_hyphen_values ( true )
161+ . long ( "first" ) ,
162+ )
163+ . arg (
164+ Arg :: new ( "second" )
165+ . action ( ArgAction :: Set )
166+ . allow_hyphen_values ( true )
167+ . long ( "second" ) ,
168+ )
169+ . arg ( Arg :: new ( "remaining" ) . num_args ( 0 ..) . last ( true ) )
170+ . try_get_matches_from ( vec ! [ "prog" , "--second" , "v2" , "--" , "--first" , "v1" ] )
171+ . unwrap ( ) ;
172+
173+ assert_eq ! ( matches. get_one:: <String >( "first" ) . map( |s| s. as_str( ) ) , None ) ;
174+ assert_eq ! ( matches. get_one:: <String >( "second" ) . map( |s| s. as_str( ) ) , Some ( "v2" ) ) ;
175+ let remaining: Vec < _ > = matches
176+ . get_many :: < String > ( "remaining" )
177+ . into_iter ( )
178+ . flatten ( )
179+ . map ( |s| s. as_str ( ) )
180+ . collect ( ) ;
181+ assert_eq ! ( remaining, [ "--first" , "v1" ] ) ;
182+ }
183+
74184#[ test]
75185fn require_equals_no_empty_values_fail ( ) {
76186 let res = Command :: new ( "prog" )
0 commit comments