@@ -58,14 +58,12 @@ pub fn complete(
58
58
parse_positional ( current_cmd, pos_index, is_escaped, current_state) ;
59
59
} else if arg. is_escape ( ) {
60
60
is_escaped = true ;
61
- } else if let Some ( ( flag, value) ) = arg. to_long ( ) {
62
- if let ParseState :: Opt ( ( opt, count) ) = current_state {
63
- if opt. is_allow_hyphen_values_set ( ) {
64
- next_state = parse_opt ( opt, count) ;
65
- continue ;
66
- }
61
+ } else if opt_allows_hyphen ( & current_state, & arg) {
62
+ match current_state {
63
+ ParseState :: Opt ( ( opt, count) ) => next_state = parse_opt_value ( opt, count) ,
64
+ _ => unreachable ! ( "else branch is only reachable in Opt state" ) ,
67
65
}
68
-
66
+ } else if let Some ( ( flag , value ) ) = arg . to_long ( ) {
69
67
if let Ok ( flag) = flag {
70
68
let opt = current_cmd. get_arguments ( ) . find ( |a| {
71
69
let longs = a. get_long_and_visible_aliases ( ) ;
@@ -87,13 +85,6 @@ pub fn complete(
87
85
}
88
86
}
89
87
} else if let Some ( short) = arg. to_short ( ) {
90
- if let ParseState :: Opt ( ( opt, count) ) = current_state {
91
- if opt. is_allow_hyphen_values_set ( ) {
92
- next_state = parse_opt ( opt, count) ;
93
- continue ;
94
- }
95
- }
96
-
97
88
let ( _, takes_value_opt, mut short) = parse_shortflags ( current_cmd, short) ;
98
89
if let Some ( opt) = takes_value_opt {
99
90
if short. next_value_os ( ) . is_none ( ) {
@@ -109,7 +100,7 @@ pub fn complete(
109
100
( next_state, pos_index) =
110
101
parse_positional ( current_cmd, pos_index, is_escaped, current_state) ;
111
102
}
112
- ParseState :: Opt ( ( opt, count) ) => next_state = parse_opt ( opt, count) ,
103
+ ParseState :: Opt ( ( opt, count) ) => next_state = parse_opt_value ( opt, count) ,
113
104
}
114
105
}
115
106
}
@@ -562,7 +553,7 @@ fn parse_positional<'a>(
562
553
}
563
554
564
555
/// Parse optional flag argument. Return new state
565
- fn parse_opt ( opt : & clap:: Arg , count : usize ) -> ParseState < ' _ > {
556
+ fn parse_opt_value ( opt : & clap:: Arg , count : usize ) -> ParseState < ' _ > {
566
557
let range = opt. get_num_args ( ) . expect ( "built" ) ;
567
558
let max = range. max_values ( ) ;
568
559
if count < max {
@@ -578,3 +569,14 @@ fn pos_allows_hyphen(cmd: &clap::Command, pos_index: usize) -> bool {
578
569
. map ( |p| p. is_allow_hyphen_values_set ( ) )
579
570
. unwrap_or ( false )
580
571
}
572
+
573
+ fn opt_allows_hyphen ( state : & ParseState < ' _ > , arg : & clap_lex:: ParsedArg < ' _ > ) -> bool {
574
+ let val = arg. to_value_os ( ) ;
575
+ if val. starts_with ( "-" ) {
576
+ if let ParseState :: Opt ( ( opt, _) ) = state {
577
+ return opt. is_allow_hyphen_values_set ( ) ;
578
+ }
579
+ }
580
+
581
+ false
582
+ }
0 commit comments