Skip to content
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

feat(clap_complete): Support flags with values --flag barand -f bar in native completions #5539

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

shannmu
Copy link
Contributor

@shannmu shannmu commented Jun 19, 2024

related issue: #3920
what is left

  • support -fbar and -f=bar
  • error parsing for --flag=bar in bash
  • foo --bar\t offering both --bar <value> and --bare

Comment on lines 81 to 85
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() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if we did

let opt = ...;
match opt.map(|o| o.get_action()) {
}

This would reduce some of the rightward drift and the trailing else that is hard to match up with the if

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This still needs resolving

@shannmu shannmu force-pushed the option_value branch 3 times, most recently from 05ab44a to 456053d Compare June 29, 2024 17:15
@shannmu shannmu force-pushed the option_value branch 2 times, most recently from 4c6e44b to 46dcc90 Compare June 29, 2024 19:17
@shannmu shannmu changed the title feat(clap_complete): Add support --flag bar and -f bar completion feat(clap_complete): Support flags with values in native completions Jun 30, 2024
@shannmu
Copy link
Contributor Author

shannmu commented Jun 30, 2024

The cases of -fbar and -f=bar have not been handled yet.

@shannmu
Copy link
Contributor Author

shannmu commented Jun 30, 2024

I have added support for -fbar and -f=bar completion.

completions.extend(
shorts_and_visible_aliases(cmd)
.into_iter()
// HACK: Need better `OsStr` manipulation
.map(|(f, help)| (format!("{}{}", dash_or_arg, f).into(), help)),
);

This code didn't consider the case -f=bar[TAB], so it could generate -f=barf in completions. I will fix it later.

@epage
Copy link
Member

epage commented Jul 8, 2024

While talked about this in the call, to not lose track of this

I have added support for -fbar and -f=bar completion.

Please split that out into another branch for a later PR so we can focus on getting the current PR merged.

@shannmu shannmu changed the title feat(clap_complete): Support flags with values in native completions feat(clap_complete): Support flags with values --flag barand -f bar in native completions Jul 9, 2024
@@ -148,6 +148,12 @@ fn suggest_argument_value() {
.short('F')
.value_parser(["json", "yaml", "toml"]),
)
.arg(
Copy link
Member

@epage epage Jul 10, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please edit the original test commit with these changes

Comment on lines 73 to +79
if is_escaped {
pos_index += 1;
state = ParseState::Pos(pos_index);
} else if arg.is_escape() {
is_escaped = true;
} else if let Some(_long) = arg.to_long() {
} else if let Some(_short) = arg.to_short() {
state = ParseState::ValueDone;
} else if let Some((flag, value)) = arg.to_long() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't we need to check the state here before any of the branches? There is more nuance to it than that but we can fill that in over time

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Of course, we could check the state here before any of the branches, and I would prefer to do so. This way, ParseState can truly become a type used for state machine rather than just a type that records the current raw_arg state.

Benefits that could potentially come from this (that I can think of for now):

Comment on lines +130 to +132
let mut flag = false;
state = if let Some(o) = opt {
match o.get_action() {
Copy link
Member

@epage epage Jul 12, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

let Some(o) = opt else {
    break;
};

Comment on lines +155 to +157
if flag {
break;
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

flag is confusing because they are all flags. I think you mean takes_value?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants