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

Support flags with values in native completions #3920

Open
2 of 3 tasks
epage opened this issue Jul 13, 2022 · 6 comments
Open
2 of 3 tasks

Support flags with values in native completions #3920

epage opened this issue Jul 13, 2022 · 6 comments
Labels
A-completion Area: completion generator C-enhancement Category: Raise on the bar on expectations E-easy Call for participation: Experience needed to fix: Easy / not much E-help-wanted Call for participation: Help is requested to fix this issue. 💸 $20

Comments

@epage
Copy link
Member

epage commented Jul 13, 2022

See #3166 for more context

Tasks

@epage epage added C-enhancement Category: Raise on the bar on expectations A-completion Area: completion generator E-easy Call for participation: Experience needed to fix: Easy / not much labels Jul 13, 2022
@epage epage added 💸 $20 E-help-wanted Call for participation: Help is requested to fix this issue. labels Sep 13, 2022
@epage
Copy link
Member Author

epage commented May 22, 2024

We currently support

  • --foo=bar, see
    if let Some((flag, value)) = arg.to_long() {
    if let Ok(flag) = flag {
    if let Some(value) = value {
    if let Some(arg) = cmd.get_arguments().find(|a| a.get_long() == Some(flag)) {
    completions.extend(
    complete_arg_value(value.to_str().ok_or(value), arg, current_dir)
    .into_iter()
    .map(|(os, help)| {
    // HACK: Need better `OsStr` manipulation
    (format!("--{}={}", flag, os.to_string_lossy()).into(), help)
    }),
    );
    }
    } else {
    completions.extend(longs_and_visible_aliases(cmd).into_iter().filter_map(
    |(f, help)| f.starts_with(flag).then(|| (format!("--{f}").into(), help)),
    ));
    }
    }
  • bar, see
    if let Some(positional) = cmd
    .get_positionals()
    .find(|p| p.get_index() == Some(pos_index))
    {
    completions.extend(complete_arg_value(arg.to_value(), positional, current_dir));
    }

We need to also support

  • -f=bar
  • -f bar
  • --foo bar
  • -fbar

Relevant code in the main parser

} else if let Some(short_arg) = arg_os.to_short() {

// Check for trailing concatenated value
//
// Cloning the iterator, so we rollback if it isn't there.
let val = short_arg.clone().next_value_os().unwrap_or_default();
debug!("Parser::parse_short_arg:iter:{c}: val={val:?}, short_arg={short_arg:?}");
let val = Some(val).filter(|v| !v.is_empty());
// Default to "we're expecting a value later".
//
// If attached value is not consumed, we may have more short
// flags to parse, continue.
//
// e.g. `-xvf`, when require_equals && x.min_vals == 0, we don't
// consume the `vf`, even if it's provided as value.
let (val, has_eq) = if let Some(val) = val.and_then(|v| v.strip_prefix("=")) {
(Some(val), true)
} else {
(val, false)
};

See also

@HKalbasi
Copy link
Contributor

I would like to take this.

@epage
Copy link
Member Author

epage commented May 23, 2024

Shan is going to be taking this.

@shannmu

This comment was marked as off-topic.

@shannmu

This comment was marked as off-topic.

@epage

This comment was marked as off-topic.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-completion Area: completion generator C-enhancement Category: Raise on the bar on expectations E-easy Call for participation: Experience needed to fix: Easy / not much E-help-wanted Call for participation: Help is requested to fix this issue. 💸 $20
Projects
None yet
Development

No branches or pull requests

3 participants