dotnet / command-line-api

Command line parsing, invocation, and rendering of terminal output.
https://github.com/dotnet/command-line-api/wiki
MIT License
3.41k stars 382 forks source link

Renamed: PowerShell Tab completion issues (you need to restart commandline to see refreshed dotnet suggest list) #749

Open dpokluda opened 4 years ago

dpokluda commented 4 years ago

I did follow steps for dotnet-suggest:

I am trying to get Tab completion to work but I never see any suggestion. Are they any other requirements? I have a

I build .NET Core 3.1 console application called Test using "System.CommandLine" version "2.0.0-beta1.20071.2" (configured options are --int-option, --bool-option and --file-option). When I press Tab in the following PowerShell command line nothing happens:

PS> Test.exe --int<TAB>

When I press Tab in the following PowerShell command line I just see standard PowerShell completion (files from the same directory):

PS> Test.exe <TAB>

I am sure there is something obvious that I missing. Could you please point in the right direction to get the tab completion/suggestion to work for my CommandLine console tools?

jonsequitur commented 4 years ago

Can you provide a code sample that reproduces the issue?

dpokluda commented 4 years ago

What exactly do you want to see. Here is my simple Program.cs:

using System;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.IO;

namespace Test
{
    class Program
    {
        static int Main(string[] args)
        {
            var rootCommand = new RootCommand("Simple command line tool.");
            rootCommand.AddOption(new Option("--int-option", "Integer value option.") {Argument = new Argument<int>()});
            rootCommand.AddOption(new Option("--bool-option", "Boolean value option.") {Argument = new Argument<bool>()});
            rootCommand.AddOption(new Option("--file-option", "FileInfo value option.") {Argument = new Argument<FileInfo>()});

            rootCommand.Handler = CommandHandler.Create<int, bool, FileInfo>((intOption, boolOption, fileOption) =>
            {
                Console.WriteLine($"IntOption: {intOption}");
                Console.WriteLine($"BoolOption: {boolOption}");
                Console.WriteLine($"FileOption: {fileOption}");
            });

            return rootCommand.InvokeAsync(args).Result;
        }
    }
}

Here is my CSPROJ:

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>netcoreapp3.1</TargetFramework>
        <AssemblyName>Test</AssemblyName>
        <RootNamespace>Test</RootNamespace>
    </PropertyGroup>

    <ItemGroup>
      <PackageReference Include="System.CommandLine" Version="2.0.0-beta1.20071.2" />
    </ItemGroup>

</Project>

Here is content of my Microsoft.PowerShell_profile.ps1 file:

# dotnet suggest shell start
$availableToComplete = (dotnet-suggest list) | Out-String
$availableToCompleteArray = $availableToComplete.Split([Environment]::NewLine, [System.StringSplitOptions]::RemoveEmptyEntries) 

    Register-ArgumentCompleter -Native -CommandName $availableToCompleteArray -ScriptBlock {
        param($commandName, $wordToComplete, $cursorPosition)
        $fullpath = (Get-Command $wordToComplete.CommandElements[0]).Source

        $arguments = $wordToComplete.Extent.ToString().Replace('"', '\"')
        dotnet-suggest get -e $fullpath --position $cursorPosition -- "$arguments" | ForEach-Object {
            [System.Management.Automation.CompletionResult]::new($_, $_, 'ParameterValue', $_)
        }
    }
$env:DOTNET_SUGGEST_SCRIPT_VERSION = "1.0.0"
# dotnet suggest script end

Is this what you have asked for? Is there anything else?

jonsequitur commented 4 years ago

That's great, thanks!

If you run the following, do you see your app's name in the output?

> dotnet suggest list
dpokluda commented 4 years ago

Yes:

> dotnet suggest list
dotnet-suggest
dotnet suggest
Test
dpokluda commented 4 years ago

I got it working now. Let me investigate why it didn't work before. It might be related to using CmdEr.

Thank you very much for helping me.

jonsequitur commented 4 years ago

The app is supposed to self-register but it might not work if the first time it's ever executed is for a suggest request.

dpokluda commented 4 years ago

You are right. It doesn't work the first time. When I close command line and restart it, then it works the second time. Is that expected behavior?

jonsequitur commented 4 years ago

Nope, that's a bug. We don't have an issue open for it yet. Would you like to rename this one to be more specific and we can use this to track a fix?

dpokluda commented 4 years ago

Done. Thanks.

jonsequitur commented 4 years ago

Just to clarify, I think this only affects the first use of a given app built with System.CommandLine, and is unrelated to the shell / terminal needing to be restarted. It should continue to work after that, unless I'm misunderstanding.