Powershell Linux: How to create simple argument TAB Completion for native Linux command

swick 1 Reputation point
2020-09-29T12:42:44.397+00:00

Hi,

I just started learning Powershell on Linux (Debian).

What I try to do is the following.

I have a Bash script called "zst" in /usr/bin/
with TAB completions for it to complete arguments like:

zst <TAB> 

which gives me a list of possible arguments.
The TAB completer is a standard Bash script to create the strings ( just a curl call to some REST API).

If I use Powershell, of course I can run "zst" but without the TAB completions.

How can I achieve this?

I read something about ArgumentCompleters

https://adamtheautomator.com/powershell-parameters-argumentcompleter/

Can someone point me to the right direction?

Thanks in advance.

Windows for business Windows Server User experience PowerShell
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Rich Matheisen 47,901 Reputation points
    2020-09-29T18:52:00.077+00:00

    Not ever having done this, I expect you'd start by using "Set-PSReadlineKeyHandler" and including in its scriptblock the completions you want to offer for a given keystroke or chord. Include the Set-PSReadlineKeyHandler cmdlet(s) in your PowerShell profile (sorry, I don't know where that is located in *nix) -- if it was Windows it'd be here: C:\Users[User]\Documents\WindowsPowerShell\profile.ps1.

    0 comments No comments

  2. swick 1 Reputation point
    2020-09-30T11:37:01.957+00:00

    Thanks for the hint. I will try it with Set-PSReadlineKeyHandler.

    In the meantime I succeeded with the following solution

    Register-ArgumentCompleter -Native -CommandName zst -ScriptBlock {
         param($wordToComplete)
         (curl -s https://server/resources/txt | grep ^$wordToComplete)
    }
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.