Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

powershell : dynamic autocomplete on custom executable

So I have a code that generates an executable, and I want to be able to add argument autocompletion on my executable.
I can’t even generate a small and easy example from where I could start from. I have looked into the Register-ArgumentCompleter command, but it seems it’s made for powershell functions and cmdlets. Maybe I could make my exe a cmdlet with some kind of encapsulation, but I am a bit of a noob in powershell so not sure I know what I’m talking about…

If someone could help me set up a basic example, that’d be great !
for example I have an executable called example.exe (don’t care what it does), and when in powershell I run .\example.exe TAB it suggests foo or bar.
That possible ?

Thanks 🙂

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I tried this (and more)

$scriptBlock = { 'foo','bar' }
Register-ArgumentCompleter -CommandName .\example.exe -ParameterName Name -ScriptBlock $scriptBlock

.\example.exe TAB
=> that shows just the files in current folder

>Solution :

Ditch the parametername argument. I’d also use just the executable name (if it’s in your path environment variable) or the full path to the executable name.

$scriptBlock = { 'foo','bar' }
Register-ArgumentCompleter -CommandName example.exe -ScriptBlock $scriptBlock

or

$scriptBlock = { 'foo','bar' }
Register-ArgumentCompleter -CommandName full\path\to\example.exe -ScriptBlock $scriptBlock

Now when you type example.exe TAB it will complete with foo/bar

If you don’t want to have to type .exe as well, omit it from the definition

$scriptBlock = { 'foo','bar' }
Register-ArgumentCompleter -CommandName example -ScriptBlock $scriptBlock
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading