JKToolKit.Spectre.AutoCompletion 0.0.5

There is a newer version of this package available.
See the version list below for details.
dotnet add package JKToolKit.Spectre.AutoCompletion --version 0.0.5                
NuGet\Install-Package JKToolKit.Spectre.AutoCompletion -Version 0.0.5                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="JKToolKit.Spectre.AutoCompletion" Version="0.0.5" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add JKToolKit.Spectre.AutoCompletion --version 0.0.5                
#r "nuget: JKToolKit.Spectre.AutoCompletion, 0.0.5"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install JKToolKit.Spectre.AutoCompletion as a Cake Addin
#addin nuget:?package=JKToolKit.Spectre.AutoCompletion&version=0.0.5

// Install JKToolKit.Spectre.AutoCompletion as a Cake Tool
#tool nuget:?package=JKToolKit.Spectre.AutoCompletion&version=0.0.5                

Spectre.Console AutoCompletion

Spectre.Console AutoCompletion

NuGet Nuget License PR GitHub Workflow Status

JKToolKit.Spectre.AutoCompletion is an extension package for adding auto completion to your Spectre.Console powered applications. </br> It comes with suggestions for Options and Branches out of the box, but you can also add your own suggestions for option and argument values.

Enabling Module

The extension can be enabled using the AddAutoCompletion method on the Configurator object.

public static void Main(string[] args)
{
    var app = new CommandApp();
    app.Configure
    (
        config =>
        {
            config
                .AddAutoCompletion(x => x.AddPowershell())
                .AddCommand<LionCommand>("lion");
        }
    ) ;
}

Shell integrations

  1. PowerShell
  2. More to come...

PowerShell

You can add autocomplete to PowerShell by running your application with the completion powershell command, as shown below:

.\AutoCompletion.exe completion powershell | Out-String | Invoke-Expression

To add autocomplete to PowerShell permanently, use the --install flag:

.\AutoCompletion.exe completion powershell --install | Out-String | Invoke-Expression

How integrations get the suggestions

The shell integration uses the completion complete command to get the suggestions for the current command line like this:

.\AutoCompletion.exe completion complete "AutoCompletion.exe Li"

Customizations

  1. Static Autocomplete
  2. Dynamic Autocomplete

Static Autocomplete

Spectre.Console auto completion allows you to specify static autocomplete suggestions for your command arguments and options. This can be done using the CompletionSuggestions attribute in your command settings class.

Here's an example of how to add static autocomplete suggestions:

public class LionSettings : CommandSettings
{
    [CommandArgument(0, "<TEETH>")]
    [Description("The number of teeth the lion has.")]
    [CompletionSuggestions("10", "15", "20", "30")]
    public int Teeth { get; set; }

    [CommandOption("-a|--age <AGE>")]
    public int Age { get; set; }

    [CommandOption("-n|--name <NAME>")]
    public string Name { get; set; }
}

Dynamic Autocomplete

In addition to static autocomplete suggestions, you can also provide dynamic autocomplete suggestions based on the user's input. This can be done by implementing the IAsyncCommandCompletable interface in your command class and overriding the GetSuggestionsAsync method.

Here's an example of how to add dynamic autocomplete suggestions:

[Description("The lion command.")]
public class LionCommand : Command<LionSettings>, IAsyncCommandCompletable
{
    public override int Execute(CommandContext context, LionSettings settings)
    {
        return 0;
    }

    public async Task<CompletionResult> GetSuggestionsAsync(ICommandParameterInfo parameter, string? prefix)
    {
        if(string.IsNullOrEmpty(prefix))
        {
            return CompletionResult.None();
        }

        return await this.MatchAsync()
            .Add(x => x.Age, (prefix) =>
            {
                if (prefix.Length != 0)
                {
                    return FindNextEvenNumber(prefix);
                }

                return "16";
            })
            .Add(x => x.Name, prefix =>
            {
                var names = new List<string>
                {
                    "angel", "angelika", "robert",
                    "jennifer", "michael", "lucy",
                    "david", "sarah", "john", "katherine",
                    "mark"
                };

                var bestMatches = names
                    .Where(name => name.StartsWith(prefix))
                    .ToList();

                return new CompletionResult(bestMatches, bestMatches.Any());
            })
            .MatchAsync(parameter, prefix)
            .WithPreventDefault();
    }
}

There is a working example of the AutoCompletion feature demonstrating this.

Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 is compatible.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 was computed.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 was computed. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.

NuGet packages

This package is not used by any NuGet packages.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
0.0.8 130 3/15/2024
0.0.7 65 3/15/2024
0.0.6 75 3/14/2024
0.0.5 73 3/13/2024
0.0.4 221 11/19/2023
0.0.3 113 10/17/2023
0.0.2 348 10/2/2023
0.0.1 89 10/2/2023

Initial Release