Skip to content

Commit

Permalink
feat(clap_complete): Add support for --flag bar and -f bar comple…
Browse files Browse the repository at this point in the history
…tion
  • Loading branch information
shannmu committed Jun 29, 2024
1 parent 043ba0d commit 30d127b
Showing 1 changed file with 64 additions and 3 deletions.
67 changes: 64 additions & 3 deletions clap_complete/src/dynamic/completer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,63 @@ pub fn complete(
} else if arg.is_escape() {
is_escaped = true;
state = ParseState::ValueDone;
} else if let Some(_long) = arg.to_long() {
} else if let Some(_short) = arg.to_short() {
} else if let Some((flag, value)) = arg.to_long() {
if let Ok(flag) = flag {
state = if let Some(opt) = current_cmd.get_arguments().find(|a| {
a.get_long_and_visible_aliases()
.map_or(false, |v| v.into_iter().find(|s| *s == flag).is_some())
}) {
match opt.get_action() {
clap::ArgAction::Set | clap::ArgAction::Append => {
if value.is_some() {
ParseState::ValueDone
} else {
ParseState::Opt(opt.clone())
}
}
_ => {
if value.is_some() {
ParseState::ValueDone
} else {
ParseState::ValueDone
}
}
}
} else {
ParseState::ValueDone
};
}
} else if let Some(short) = arg.to_short() {
let mut short = short.clone();
let opt = short.next_flag();

state = if let Some(opt) = opt {
if let Ok(opt) = opt {
if let Some(opt) = current_cmd.get_arguments().find(|a| {
a.get_short_and_visible_aliases()
.map_or(false, |v| v.into_iter().find(|c| *c == opt).is_some())
}) {
match opt.get_action() {
clap::ArgAction::Set | clap::ArgAction::Append => {
if short.next_value_os().is_some() {
ParseState::ValueDone
} else {
ParseState::Opt(opt.clone())
}
}
_ => {
ParseState::ValueDone
}
}
} else {
ParseState::ValueDone
}
} else {
ParseState::ValueDone
}
} else {
ParseState::ValueDone
}
} else {
pos_index += 1;
state = ParseState::Pos(pos_index);
Expand All @@ -97,6 +152,9 @@ enum ParseState {

/// Parsing a positional argument
Pos(usize),

/// Parsing a optional flag argument
Opt(clap::Arg),
}

fn complete_arg(
Expand Down Expand Up @@ -157,7 +215,10 @@ fn complete_arg(
.map(|(f, help)| (format!("{}{}", dash_or_arg, f).into(), help)),
);
}
}
} else if let ParseState::Opt(opt) = &state {
completions.extend(complete_arg_value(arg.to_value(), opt, current_dir));
}


let pos_index = if let ParseState::Pos(pos_index) = state {
pos_index
Expand Down

0 comments on commit 30d127b

Please sign in to comment.