Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support PowerShell #405

Merged
merged 5 commits into from
Mar 5, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ test_deps:

lint: test_deps
flake8 $$(python setup.py --name)
for script in scripts/*; do if grep -q python $$script; then flake8 $$script; fi; done
for script in scripts/*[^cmd]; do if grep -q python $$script; then flake8 $$script; fi; done

test: lint test_deps
coverage run --source=argcomplete --omit=argcomplete/my_shlex.py ./test/test.py -v
Expand Down
11 changes: 11 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,17 @@ The command line interface of this program must be additionally implemented in a

This option can also be used in combination with the other supported shells.

PowerShell Support
------------------
To create new completion file, e.g::

register-python-argcomplete --shell powershell my-awesome-script > ~/my-awesome-script.psm1

To activate completions for PowerShell, add the below line in ``$PROFILE``. For more information, see `How to create your profile <https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles?view=powershell-7.3#how-to-create-a-profile>`_ and `Profiles and execution policy <https://learn.microsoft.com/en-us/powershell/module/microsoft.powershell.core/about/about_profiles?view=powershell-7.3#profiles-and-execution-policy>`_. ::

Import-Module "~/my-awesome-script.psm1"


Python Support
--------------
Argcomplete requires Python 3.6+.
Expand Down
29 changes: 28 additions & 1 deletion argcomplete/shell_integration.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,28 @@
complete %(completion_arg)s %(executable)s -f -a '(__fish_%(function_name)s_complete)'
"""

shell_codes = {"bash": bashcode, "tcsh": tcshcode, "fish": fishcode}
powershell_code = r"""
Register-ArgumentCompleter -Native -CommandName %(executable)s -ScriptBlock {
param($commandName, $wordToComplete, $cursorPosition)
$completion_file = New-TemporaryFile
$env:ARGCOMPLETE_USE_TEMPFILES = 1
$env:_ARGCOMPLETE_STDOUT_FILENAME = $completion_file
$env:COMP_LINE = $wordToComplete
$env:COMP_POINT = $cursorPosition
$env:_ARGCOMPLETE = 1
$env:_ARGCOMPLETE_SUPPRESS_SPACE = 0
$env:_ARGCOMPLETE_IFS = "`n"
%(argcomplete_script)s 2>&1 | Out-Null

Get-Content $completion_file | ForEach-Object {
[System.Management.Automation.CompletionResult]::new($_, $_, "ParameterValue", $_)
}
rm $completion_file
Remove-Item Env:\_ARGCOMPLETE_STDOUT_FILENAME, Env:\ARGCOMPLETE_USE_TEMPFILES, Env:\COMP_LINE, Env:\COMP_POINT, Env:\_ARGCOMPLETE, Env:\_ARGCOMPLETE_SUPPRESS_SPACE, Env:\_ARGCOMPLETE_IFS
}
""" # noqa: E501

shell_codes = {"bash": bashcode, "tcsh": tcshcode, "fish": fishcode, "powershell": powershell_code}


def shellcode(executables, use_defaults=True, shell="bash", complete_arguments=None, argcomplete_script=None):
Expand Down Expand Up @@ -126,6 +147,12 @@ def shellcode(executables, use_defaults=True, shell="bash", complete_arguments=N
completion_arg=completion_arg,
function_name=function_name,
)
elif shell == "powershell":
code = ""
for executable in executables:
script = argcomplete_script or executable
code += powershell_code % dict(executable=executable, argcomplete_script=script)

else:
code = ""
for executable in executables:
Expand Down
3 changes: 2 additions & 1 deletion scripts/register-python-argcomplete
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ parser.add_argument(
help="arguments to call complete with; use of this option discards default options",
)
parser.add_argument(
"-s", "--shell", choices=("bash", "tcsh", "fish"), default="bash", help="output code for the specified shell"
"-s", "--shell", choices=("bash", "tcsh", "fish", "powershell"), default="bash",
help="output code for the specified shell"
)
parser.add_argument(
"-e", "--external-argcomplete-script", help="external argcomplete script for auto completion of the executable"
Expand Down
1 change: 1 addition & 0 deletions scripts/register-python-argcomplete.cmd
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@python "%~dp0\register-python-argcomplete" %*