-
-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat(complete): Support require_equals in completion engine #6260
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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -88,7 +88,10 @@ pub fn complete( | |
| }); | ||
|
|
||
| if let Some(opt) = opt { | ||
| if opt.get_num_args().expect("built").takes_values() && value.is_none() { | ||
| if opt.get_num_args().expect("built").takes_values() | ||
| && value.is_none() | ||
| && !opt.is_require_equals_set() | ||
| { | ||
| next_state = ParseState::Opt((opt, 1)); | ||
| }; | ||
| } else if pos_allows_hyphen(current_cmd, pos_index) { | ||
|
|
@@ -478,9 +481,16 @@ fn longs_and_visible_aliases(p: &clap::Command) -> Vec<CompletionCandidate> { | |
| p.get_arguments() | ||
| .filter_map(|a| { | ||
| a.get_long_and_visible_aliases().map(|longs| { | ||
| longs | ||
| .into_iter() | ||
| .map(|s| populate_arg_candidate(CompletionCandidate::new(format!("--{s}")), a)) | ||
| longs.into_iter().map(|s| { | ||
| let suffix = if a.is_require_equals_set() | ||
| && a.get_num_args().expect("built").takes_values() | ||
|
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.
|
||
| { | ||
| "=" | ||
| } else { | ||
| "" | ||
| }; | ||
| populate_arg_candidate(CompletionCandidate::new(format!("--{s}{suffix}")), a) | ||
| }) | ||
| }) | ||
| }) | ||
| .flatten() | ||
|
|
@@ -495,7 +505,15 @@ fn hidden_longs_aliases(p: &clap::Command) -> Vec<CompletionCandidate> { | |
| .filter_map(|a| { | ||
| a.get_aliases().map(|longs| { | ||
| longs.into_iter().map(|s| { | ||
| populate_arg_candidate(CompletionCandidate::new(format!("--{s}")), a).hide(true) | ||
| let suffix = if a.is_require_equals_set() | ||
| && a.get_num_args().expect("built").takes_values() | ||
| { | ||
| "=" | ||
| } else { | ||
| "" | ||
| }; | ||
| populate_arg_candidate(CompletionCandidate::new(format!("--{s}{suffix}")), a) | ||
| .hide(true) | ||
| }) | ||
| }) | ||
| }) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1443,6 +1443,58 @@ pos-c | |
| ); | ||
| } | ||
|
|
||
| #[test] | ||
| fn suggest_require_equals() { | ||
| let mut cmd = Command::new("exhaustive") | ||
| .arg( | ||
| clap::Arg::new("format") | ||
| .long("format") | ||
| .require_equals(true) | ||
| .value_parser(["json", "yaml", "toml"]), | ||
| ) | ||
| .arg( | ||
| clap::Arg::new("name") | ||
| .long("name") | ||
| .short('n'), | ||
| ); | ||
|
|
||
|
Comment on lines
+1447
to
+1460
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. We need a test case with |
||
| // When completing after an empty input, --format should appear with = appended | ||
| assert_data_eq!( | ||
| complete!(cmd, " [TAB]"), | ||
| snapbox::str![[r#" | ||
| --format= | ||
| --name | ||
| --help Print help | ||
| "#]] | ||
| ); | ||
|
|
||
| // Should complete values after --format= | ||
| assert_data_eq!( | ||
| complete!(cmd, "--format=[TAB]"), | ||
| snapbox::str![[r#" | ||
| --format=json | ||
| --format=yaml | ||
| --format=toml | ||
| "#]] | ||
| ); | ||
|
|
||
| // Should complete values after --format=j | ||
| assert_data_eq!( | ||
| complete!(cmd, "--format=j[TAB]"), | ||
| snapbox::str!["--format=json"] | ||
| ); | ||
|
|
||
| // When typing --format (space), should NOT suggest values since require_equals forbids it | ||
| assert_data_eq!( | ||
| complete!(cmd, "--format [TAB]"), | ||
| snapbox::str![[r#" | ||
| --format= | ||
| --name | ||
| --help Print help | ||
| "#]] | ||
| ); | ||
| } | ||
|
|
||
| fn complete(cmd: &mut Command, args: impl AsRef<str>, current_dir: Option<&Path>) -> String { | ||
| let input = args.as_ref(); | ||
| let mut args = vec![std::ffi::OsString::from(cmd.get_name())]; | ||
|
|
||
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.
This change is for
The intention might not be obvious in the code. Would be good to find a way to make this clear