Skip to content

Commit 57b6cb8

Browse files
committed
refactor(complete): Simplify engine::complete
1 parent fbec05e commit 57b6cb8

File tree

1 file changed

+18
-16
lines changed

1 file changed

+18
-16
lines changed

clap_complete/src/engine/complete.rs

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -58,14 +58,12 @@ pub fn complete(
5858
parse_positional(current_cmd, pos_index, is_escaped, current_state);
5959
} else if arg.is_escape() {
6060
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"),
6765
}
68-
66+
} else if let Some((flag, value)) = arg.to_long() {
6967
if let Ok(flag) = flag {
7068
let opt = current_cmd.get_arguments().find(|a| {
7169
let longs = a.get_long_and_visible_aliases();
@@ -87,13 +85,6 @@ pub fn complete(
8785
}
8886
}
8987
} 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-
9788
let (_, takes_value_opt, mut short) = parse_shortflags(current_cmd, short);
9889
if let Some(opt) = takes_value_opt {
9990
if short.next_value_os().is_none() {
@@ -109,7 +100,7 @@ pub fn complete(
109100
(next_state, pos_index) =
110101
parse_positional(current_cmd, pos_index, is_escaped, current_state);
111102
}
112-
ParseState::Opt((opt, count)) => next_state = parse_opt(opt, count),
103+
ParseState::Opt((opt, count)) => next_state = parse_opt_value(opt, count),
113104
}
114105
}
115106
}
@@ -562,7 +553,7 @@ fn parse_positional<'a>(
562553
}
563554

564555
/// 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<'_> {
566557
let range = opt.get_num_args().expect("built");
567558
let max = range.max_values();
568559
if count < max {
@@ -578,3 +569,14 @@ fn pos_allows_hyphen(cmd: &clap::Command, pos_index: usize) -> bool {
578569
.map(|p| p.is_allow_hyphen_values_set())
579570
.unwrap_or(false)
580571
}
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

Comments
 (0)