From fa0972ebd00eb12df779a7581e3b756af62d51de Mon Sep 17 00:00:00 2001 From: Giampaolo Mancini Date: Wed, 25 Aug 2021 13:16:13 +0200 Subject: [PATCH 1/7] Add completion for PowerShell --- cli/completion/completion.go | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/cli/completion/completion.go b/cli/completion/completion.go index 1dd2a05c6bb..b7b3afc7f81 100644 --- a/cli/completion/completion.go +++ b/cli/completion/completion.go @@ -32,8 +32,8 @@ var ( // NewCommand created a new `completion` command func NewCommand() *cobra.Command { command := &cobra.Command{ - Use: "completion [bash|zsh|fish] [--no-descriptions]", - ValidArgs: []string{"bash", "zsh", "fish"}, + Use: "completion [bash|zsh|fish|powershell] [--no-descriptions]", + ValidArgs: []string{"bash", "zsh", "fish", "powershell"}, Args: cobra.ExactArgs(1), Short: tr("Generates completion scripts"), Long: tr("Generates completion scripts for various shells"), @@ -65,5 +65,8 @@ func run(cmd *cobra.Command, args []string) { case "fish": cmd.Root().GenFishCompletion(os.Stdout, !completionNoDesc) break + case "powershell": + cmd.Root().GenPowerShellCompletion(os.Stdout) + break } } From 86b1e81b429df668bb21e7338e8c485c40290d2a Mon Sep 17 00:00:00 2001 From: Giampaolo Mancini Date: Wed, 25 Aug 2021 13:42:35 +0200 Subject: [PATCH 2/7] Manage command description for PS completion --- cli/completion/completion.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/cli/completion/completion.go b/cli/completion/completion.go index b7b3afc7f81..b73dd190ded 100644 --- a/cli/completion/completion.go +++ b/cli/completion/completion.go @@ -47,7 +47,7 @@ func NewCommand() *cobra.Command { } func run(cmd *cobra.Command, args []string) { - if completionNoDesc && (args[0] == "bash") { + if completionNoDesc && (args[0] == "bash" || args[0] == "powershell") { feedback.Errorf(tr("Error: command description is not supported by %v"), args[0]) os.Exit(errorcodes.ErrGeneric) } From 048f1261e16d0cf91296ef34c5717739194a7917 Mon Sep 17 00:00:00 2001 From: Giampaolo Mancini Date: Wed, 25 Aug 2021 13:42:54 +0200 Subject: [PATCH 3/7] Add test for PS completion --- test/test_completion.py | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/test/test_completion.py b/test/test_completion.py index 3d7b02eafef..7afd801595c 100644 --- a/test/test_completion.py +++ b/test/test_completion.py @@ -44,6 +44,13 @@ def test_completion_fish(run_command): assert "# fish completion for arduino-cli" in result.stdout assert "function __arduino_cli_perform_completion" in result.stdout +def test_completion_powershell(run_command): + result = run_command("completion powershell") + assert result.ok + assert result.stderr == "" + assert "Register-ArgumentCompleter -Native -CommandName 'arduino-cli' -ScriptBlock {" in result.stdout + assert "'arduino-cli;completion' {" in result.stdout + def test_completion_bash_no_desc(run_command): result = run_command("completion bash --no-descriptions") @@ -68,3 +75,9 @@ def test_completion_fish_no_desc(run_command): assert "# fish completion for arduino-cli" in result.stdout assert "function __arduino_cli_perform_completion" in result.stdout assert "__completeNoDesc" in result.stdout + +def test_completion_powershell_no_desc(run_command): + result = run_command("completion powershell --no-descriptions") + assert not result.ok + assert result.stdout == "" + assert "Error: command description is not supported by powershell" in result.stderr From 3091fa11fbfa0861647fab07ecd024f7a3168f9b Mon Sep 17 00:00:00 2001 From: Giampaolo Mancini Date: Wed, 25 Aug 2021 16:34:30 +0200 Subject: [PATCH 4/7] Add PS documentation --- docs/command-line-completion.md | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/docs/command-line-completion.md b/docs/command-line-completion.md index 9d696333cd7..67b266235dd 100644 --- a/docs/command-line-completion.md +++ b/docs/command-line-completion.md @@ -1,5 +1,5 @@ `arduino-cli` supports command-line completion (also known as _tab completion_) for basic commands. Currently only -`bash`, `zsh`, `fish` shells are supported +`bash`, `zsh`, `fish`, and `powershell` shells are supported ### Before you start @@ -8,7 +8,7 @@ first. ### Generate the completion file -To generate the completion file you can use `arduino-cli completion [bash|zsh|fish] [--no-descriptions]`. By default +To generate the completion file you can use `arduino-cli completion [bash|zsh|fish|powershell] [--no-descriptions]`. By default this command will print on the standard output (the shell window) the content of the completion file. To save to an actual file use the `>` redirect symbol. @@ -44,13 +44,29 @@ with `mv arduino-cli.fish ~/.config/fish/completions/` Remember to open a new shell to test the functionality. +### Powershell + +Use `arduino-cli completion powershell > arduino-cli.ps1` to generate a temporary completion file. At this point you need to add +the content of the generated file to your PowerShell profile file. + +1. `Get-Content -Path arduino-cli.ps1 | Add-Content -Path $profile` or add it by hand with your favourite text editor. +1. Move to first two "`using ...`" lines of `arduino-cli.ps1` on the top of the `$profile` file. +1. If not already done, add the line `Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete` to your `$profile` file: it is + needed to enable the TAB completion in PowerShell. +1. `del arduino-cli.ps1` to remove the temporary file. + +Remember to open a new shell to test the functionality. + +For more information on tab-completion on PowerShell, please, refer to +[Autocomplete in PowerShell](https://techcommunity.microsoft.com/t5/itops-talk-blog/autocomplete-in-powershell/ba-p/2604524). + #### Disabling command and flag descriptions By default fish and zsh completion have command and flag description enabled by default. If you want to disable this behaviour you can simply pass the `--no-descriptions` flag when calling `completion` command and the generated file will not have descriptions -_N.B._ This flag is not compatible with bash +_N.B._ This flag is not compatible with bash nor powershell ### Brew From e395ddc08fbbe9d0a9075fdcdff73a0077577681 Mon Sep 17 00:00:00 2001 From: Giampaolo Mancini Date: Wed, 25 Aug 2021 16:42:04 +0200 Subject: [PATCH 5/7] Fix formatting --- docs/command-line-completion.md | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/docs/command-line-completion.md b/docs/command-line-completion.md index 67b266235dd..e06d67ca50d 100644 --- a/docs/command-line-completion.md +++ b/docs/command-line-completion.md @@ -8,9 +8,9 @@ first. ### Generate the completion file -To generate the completion file you can use `arduino-cli completion [bash|zsh|fish|powershell] [--no-descriptions]`. By default -this command will print on the standard output (the shell window) the content of the completion file. To save to an -actual file use the `>` redirect symbol. +To generate the completion file you can use `arduino-cli completion [bash|zsh|fish|powershell] [--no-descriptions]`. By +default this command will print on the standard output (the shell window) the content of the completion file. To save to +an actual file use the `>` redirect symbol. ### Bash @@ -46,13 +46,13 @@ Remember to open a new shell to test the functionality. ### Powershell -Use `arduino-cli completion powershell > arduino-cli.ps1` to generate a temporary completion file. At this point you need to add -the content of the generated file to your PowerShell profile file. +Use `arduino-cli completion powershell > arduino-cli.ps1` to generate a temporary completion file. At this point you +need to add the content of the generated file to your PowerShell profile file. 1. `Get-Content -Path arduino-cli.ps1 | Add-Content -Path $profile` or add it by hand with your favourite text editor. 1. Move to first two "`using ...`" lines of `arduino-cli.ps1` on the top of the `$profile` file. -1. If not already done, add the line `Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete` to your `$profile` file: it is - needed to enable the TAB completion in PowerShell. +1. If not already done, add the line `Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete` to your `$profile` file: + it is needed to enable the TAB completion in PowerShell. 1. `del arduino-cli.ps1` to remove the temporary file. Remember to open a new shell to test the functionality. From 90717141393330085c50c00c6fb2b9c265ee1806 Mon Sep 17 00:00:00 2001 From: Giampaolo Mancini Date: Wed, 25 Aug 2021 16:45:34 +0200 Subject: [PATCH 6/7] Fix python lint for PS tests --- test/test_completion.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/test/test_completion.py b/test/test_completion.py index 7afd801595c..9a07af2be12 100644 --- a/test/test_completion.py +++ b/test/test_completion.py @@ -44,6 +44,7 @@ def test_completion_fish(run_command): assert "# fish completion for arduino-cli" in result.stdout assert "function __arduino_cli_perform_completion" in result.stdout + def test_completion_powershell(run_command): result = run_command("completion powershell") assert result.ok @@ -76,6 +77,7 @@ def test_completion_fish_no_desc(run_command): assert "function __arduino_cli_perform_completion" in result.stdout assert "__completeNoDesc" in result.stdout + def test_completion_powershell_no_desc(run_command): result = run_command("completion powershell --no-descriptions") assert not result.ok From 99595bf21df8e3fc369aafc37b59f4ee62236613 Mon Sep 17 00:00:00 2001 From: Giampaolo Mancini Date: Thu, 26 Aug 2021 10:10:19 +0200 Subject: [PATCH 7/7] Update docs/command-line-completion.md Co-authored-by: Silvano Cerza <3314350+silvanocerza@users.noreply.github.com> --- docs/command-line-completion.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/docs/command-line-completion.md b/docs/command-line-completion.md index e06d67ca50d..5d1433ccc89 100644 --- a/docs/command-line-completion.md +++ b/docs/command-line-completion.md @@ -50,7 +50,7 @@ Use `arduino-cli completion powershell > arduino-cli.ps1` to generate a temporar need to add the content of the generated file to your PowerShell profile file. 1. `Get-Content -Path arduino-cli.ps1 | Add-Content -Path $profile` or add it by hand with your favourite text editor. -1. Move to first two "`using ...`" lines of `arduino-cli.ps1` on the top of the `$profile` file. +1. The previous command added two `using namespace` lines, move them on top of the `$profile` file. 1. If not already done, add the line `Set-PSReadlineKeyHandler -Key Tab -Function MenuComplete` to your `$profile` file: it is needed to enable the TAB completion in PowerShell. 1. `del arduino-cli.ps1` to remove the temporary file.