dotnet / dotnet/command-line-api

Using System.CommandLine for powershell completers

Open
#1,220 4 comments 3 reactions 0 assignees View on GitHub
Area-Completions enhancement
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.
![image](https://user-images.githubusercontent.com/3505151/110766517-892a1680-8255-11eb-9bef-552b8545f923.png)

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

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.