Skip to content

Allow hiding tab-completion values from help #1976

Closed

Description

@CurtisLusmore

I have an argument for one of my commands which represents a path/ID to a resource. I want to be able to provide tab-completion to allow easily cycling between existing paths/IDs, but I don't want these values to be enumerated if I run --help for my command.

I'm thinking this could be an option on the CompletionItem object, like a boolean property called Hidden. You could then do something this like.

var myarg = new Option<string>("--id", "The id of the item to get");
myarg.AddCompletions(ctx => GetIdSuggestions(ctx));

private IEnumerable<CompletionItem> GetIdSuggestions(CompletionContext ctx)
    => GetExistingIds(prefix: ctx.WordToComplete).Select(id => new CompletionItem(id) { Hidden = true });

and then when you attempt to run the command from the terminal

app command --id <TAB>

it will cycle through existing IDs returned by my method, but when I run

app command --help

It will still only print out

Options:
 --id <id>   The id of the item to get

rather than

Options:
 --id <id1|id2|id3|id4|...>   The id of the item to get

Activity

  1. KalleOlaviNiemitalo commented on Nov 23, 2022

    @KalleOlaviNiemitalo

    You can hide the completions of the option argument from help by setting the ArgumentHelpName property. Help will still enumerate the completions though, just not display them. dotnet/sdk#27374 (comment), #2038

  2. CurtisLusmore commented on Nov 23, 2022

    @CurtisLusmore
    Author

    Thanks! For the benefit of those who stumble upon this issue trying to do the same thing, the solution is to do the following

    var myarg = new Argument<string>("arg", "An argument with completions") { HelpName = "arg" };
    myarg.AddCompletions(/*...*/);
    var myopt = new Option<string>("--opt", "An option with completions") { ArgumentHelpName = "opt" };
    myopt.AddCompletions(/*...*/);

    which will produce the following output when you run --help

    Arguments:
      <arg>  An argument with completions
    
    Options:
      --opt <opt>  An option with completions
    
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions