-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(complete): Support flag-style subcommands in completion engine #6262
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Draft
AndreasBackx
wants to merge
2
commits into
clap-rs:master
Choose a base branch
from
AndreasBackx:feat/subcommand-flags-completion
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+189
−14
Draft
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -77,6 +77,14 @@ pub fn complete( | |
| } | ||
| } else if let Some((flag, value)) = arg.to_long() { | ||
| if let Ok(flag) = flag { | ||
| if value.is_none() { | ||
| if let Some(subcmd) = find_long_flag_subcmd(current_cmd, flag) { | ||
| current_cmd = subcmd; | ||
| pos_index = 1; | ||
| continue; | ||
| } | ||
| } | ||
|
|
||
| let opt = current_cmd.get_arguments().find(|a| { | ||
| let longs = a.get_long_and_visible_aliases(); | ||
| let is_find = longs.map(|v| { | ||
|
|
@@ -96,15 +104,39 @@ pub fn complete( | |
| parse_positional(current_cmd, pos_index, is_escaped, current_state); | ||
| } | ||
| } | ||
| } else if let Some(short) = arg.to_short() { | ||
| let (_, takes_value_opt, mut short) = parse_shortflags(current_cmd, short); | ||
| if let Some(opt) = takes_value_opt { | ||
| if short.next_value_os().is_none() { | ||
| next_state = ParseState::Opt((opt, 1)); | ||
| } else if let Some(mut short) = arg.to_short() { | ||
| // Check if the first short flag is a subcommand flag. | ||
| let mut found_subcmd = false; | ||
| let mut peek = short.clone(); | ||
| if let Some(Ok(c)) = peek.next_flag() { | ||
| if let Some(subcmd) = find_short_flag_subcmd(current_cmd, c) { | ||
| current_cmd = subcmd; | ||
| pos_index = 1; | ||
| found_subcmd = true; | ||
| // Consume the subcommand flag from the real iterator. | ||
| short.next_flag(); | ||
| // If there are remaining flags, parse them as flags of the subcommand. | ||
| if peek.next_flag().is_some() { | ||
| let (_, takes_value_opt, remaining) = parse_shortflags(current_cmd, short); | ||
| short = remaining; | ||
| if let Some(opt) = takes_value_opt { | ||
| if short.next_value_os().is_none() { | ||
| next_state = ParseState::Opt((opt, 1)); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| if !found_subcmd { | ||
| let (_, takes_value_opt, mut short) = parse_shortflags(current_cmd, short); | ||
| if let Some(opt) = takes_value_opt { | ||
| if short.next_value_os().is_none() { | ||
| next_state = ParseState::Opt((opt, 1)); | ||
| } | ||
| } else if pos_allows_hyphen(current_cmd, pos_index) { | ||
| (next_state, pos_index) = | ||
| parse_positional(current_cmd, pos_index, is_escaped, current_state); | ||
| } | ||
| } else if pos_allows_hyphen(current_cmd, pos_index) { | ||
| (next_state, pos_index) = | ||
| parse_positional(current_cmd, pos_index, is_escaped, current_state); | ||
| } | ||
| } else { | ||
| match current_state { | ||
|
|
@@ -285,10 +317,55 @@ fn complete_option( | |
| .into_iter() | ||
| .filter(|comp| comp.get_value().starts_with(format!("--{flag}").as_str())), | ||
| ); | ||
| completions.extend( | ||
| long_flag_subcommands(cmd) | ||
| .into_iter() | ||
| .filter(|comp| comp.get_value().starts_with(format!("--{flag}").as_str())), | ||
| ); | ||
| } | ||
| } | ||
| } else if let Some(short) = arg.to_short() { | ||
| if !short.is_negative_number() { | ||
| // Check if the first short flag is a subcommand flag. | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ditto |
||
| let mut peek = short.clone(); | ||
| if let Some(Ok(c)) = peek.next_flag() { | ||
| if let Some(subcmd) = find_short_flag_subcmd(cmd, c) { | ||
| // First flag is a subcommand; complete remaining flags from the subcommand. | ||
| let mut inner_short = short.clone(); | ||
| inner_short.next_flag(); // consume the subcommand flag | ||
| let subcmd_prefix = format!("-{c}"); | ||
|
|
||
| let (leading_flags, takes_value_opt, mut remaining) = | ||
| parse_shortflags(subcmd, inner_short); | ||
|
|
||
| if let Some(opt) = takes_value_opt { | ||
| let mut peek_remaining = remaining.clone(); | ||
| let has_equal = if let Some(Ok('=')) = peek_remaining.next_flag() { | ||
| remaining.next_flag(); | ||
| true | ||
| } else { | ||
| false | ||
| }; | ||
|
|
||
| let value = remaining.next_value_os().unwrap_or(OsStr::new("")); | ||
| completions.extend( | ||
| complete_arg_value(value.to_str().ok_or(value), opt, current_dir) | ||
| .into_iter() | ||
| .map(|comp| { | ||
| let sep = if has_equal { "=" } else { "" }; | ||
| comp.add_prefix(format!("{subcmd_prefix}{leading_flags}{sep}")) | ||
| }), | ||
| ); | ||
| } else { | ||
| completions.extend(shorts_and_visible_aliases(subcmd).into_iter().map( | ||
| |comp| comp.add_prefix(format!("{subcmd_prefix}{leading_flags}")), | ||
| )); | ||
| } | ||
|
|
||
| return completions; | ||
| } | ||
| } | ||
|
|
||
| // Find the first takes_values option. | ||
| let (leading_flags, takes_value_opt, mut short) = parse_shortflags(cmd, short); | ||
|
|
||
|
|
@@ -503,6 +580,30 @@ fn hidden_longs_aliases(p: &clap::Command) -> Vec<CompletionCandidate> { | |
| .collect() | ||
| } | ||
|
|
||
| /// Gets long flag subcommands as `--flag` style completion candidates. | ||
| fn long_flag_subcommands(cmd: &clap::Command) -> Vec<CompletionCandidate> { | ||
| cmd.get_subcommands() | ||
| .flat_map(|sc| { | ||
| let mut candidates = Vec::new(); | ||
| if let Some(long_flag) = sc.get_long_flag() { | ||
| candidates.push(populate_command_candidate( | ||
| CompletionCandidate::new(format!("--{long_flag}")), | ||
| cmd, | ||
| sc, | ||
| )); | ||
| } | ||
| for alias in sc.get_visible_long_flag_aliases() { | ||
| candidates.push(populate_command_candidate( | ||
| CompletionCandidate::new(format!("--{alias}")), | ||
| cmd, | ||
| sc, | ||
| )); | ||
| } | ||
| candidates | ||
| }) | ||
| .collect() | ||
| } | ||
|
|
||
| /// Gets all the short options, their visible aliases and flags of a [`clap::Command`]. | ||
| /// Includes `h` and `V` depending on the [`clap::Command`] settings. | ||
| fn shorts_and_visible_aliases(p: &clap::Command) -> Vec<CompletionCandidate> { | ||
|
|
@@ -705,3 +806,17 @@ fn opt_allows_hyphen(state: &ParseState<'_>, arg: &clap_lex::ParsedArg<'_>) -> b | |
|
|
||
| false | ||
| } | ||
|
|
||
| /// Find a subcommand by short flag (including aliases). | ||
| fn find_short_flag_subcmd(cmd: &clap::Command, flag: char) -> Option<&clap::Command> { | ||
| cmd.get_subcommands().find(|sc| { | ||
| sc.get_short_flag() == Some(flag) || sc.get_all_short_flag_aliases().any(|a| a == flag) | ||
| }) | ||
| } | ||
|
|
||
| /// Find a subcommand by long flag (including aliases). | ||
| fn find_long_flag_subcmd<'c>(cmd: &'c clap::Command, flag: &str) -> Option<&'c clap::Command> { | ||
| cmd.get_subcommands().find(|sc| { | ||
| sc.get_long_flag() == Some(flag) || sc.get_all_long_flag_aliases().any(|a| a == flag) | ||
| }) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
iiuc short flag subcommands can be anywhere in the list of short flags, not just the first