dotnet / dotnet/command-line-api

Allow to disable option completion when defining the option

Open
#1,496 0 comments 0 reactions 0 assignees View on GitHub
Area-Completions
Dominant language
C#
Stars
3.7k
Forks
428
PR merge metrics
No merged PRs in 30d

Description

In dotnet new dynamical command building the ability to disable option completion is required.

Since dotnet new instantiation command is built dynamically, some of the options existing in initial command definition may not provide correct tab completion before command is rebuilt, and should be disabled for tab completion. This scenario might be useful for all commands that have custom suggestion logic, not only for dynamically built ones.

The ask is basically define new boolean property for `Option`: `DisableCompletion` as example. This option should be checked when evaluating [`SymbolToComplete`](https://github.com/dotnet/command-line-api/blob/804c6e3e08570e3f303dded19edae4b3e32786ee/src/System.CommandLine/Parsing/ParseResultExtensions.cs#L332) and if set the option should be skipped for completion falling back to its parent.

Skipping this completion on option level means that parent command `GetSuggestions` overload will take care about correct completion, even though position may be on option.

Example of use will be:
```C#
[Fact]
public void CustomHandler()
{
Command command = new MyCustomCommand("new")
{
new Option("--language")
{
DisableCompletion = true
}
};

var parseResult = command.Parse("new --language ");

Assert.Throws(() => parseResult.GetSuggestions()); // handler below should be called, without the change it won't be called

}

private class MyCustomCommand : Command
{
public MyCustomCommand(string name, string description = null) : base(name, description)
{
}

public override IEnumerable GetSuggestions(ParseResult parseResult = null, string textToMatch = null)
{
//custom logic goes here
//it also handles option completion
throw new NotImplementedException();
}
}

```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.