dotnet / dotnet/command-line-api
Using System.CommandLine for powershell completers
- Dominant language
- C#
- Stars
- 3.7k
- Forks
- 428
- PR merge metrics
- No merged PRs in 30d
Description
When writing a completer for an existing executable, the PowerShell API requires the return of an `IEnumerable`. It is a somewhat richer model, where you can provide more info than just a string, such as tooltips.
For example, completing `git a` gives me documentation as a tooltip.

However, the suggestion APIs are using `string` as its hard-coded data type, making this kind of rich completion more difficult.
Sure, I can encode the info as json, or have null-separated fields, but it adds complexity.
What do you think of adding a more generic api, a `IEnumerable GetSuggestion()`?
Edit:
More info.
In PowerShell, Native commands are completed by a script like this:
```powershell
{
param($wordToComplete, $commandAst, $cursorPosition)
[SampleCompleter]::CompleteInput($wordToComplete, $commandAst, $cursorPosition)
}
```
```csharp
public static class SampleCompleter{
public static IEnumerable CompleteInput(string wordToComplete, CommandAst commandAst, int cursorPosition) {
return GetOptions().Where(o=>o.CompletionText.StartsWith(wordTocomplete);
}
IEnumerable GetOptions(){
yield return new CompletionResult("-a", "-a", CompletionResultType.ParameterName, "tooltip for a");
yield return new CompletionResult("-b", "-b", CompletionResultType.ParameterName, "tooltip for b");
}
}
```
The nice thing would be to use System.CommandLine to get the suggestions, but as `CompletionResult` instead of `string`.
Contributor guide
Assessment
This issue has not been assessed yet.