Add PowerShell tab completion support#212
Add PowerShell tab completion support#212HeyItsGilbert wants to merge 4 commits intoiterative:mainfrom
Conversation
* Add PowerShell tab completion support
Implement PowerShell as a fourth supported shell alongside bash, zsh, and
tcsh. The generated script uses Register-ArgumentCompleter -Native with a
ScriptBlock that parses the command AST, tracks parser state (subcommands,
options, positionals, nargs), and returns CompletionResult objects.
Key changes:
- Add complete_powershell() with @mark_completer("powershell") decorator
- Add get_powershell_commands() for recursive parser traversal
- Add PowerShell serialization helpers for hashtables/arrays
- Add CHOICE_FUNCTIONS entries for powershell file/dir completion
- Update examples/customcomplete.py with PowerShell preamble
- Update tests with PowerShell assertions (all 78 tests pass)
- Update README, docs/index.md, docs/use.md with PowerShell instructions
- Include PLAN-powershell-support.md documenting the architecture
Note: option-specific choices/compgens completion is stubbed with a TODO
marker for future implementation.
https://claude.ai/code/session_01F9QZN9wfoaU7gujvy9Kva9
* Implement option-specific choices/compgens in PowerShell completer
Track a $currentActionKey through the state machine that gets updated
on subparser reset, option match, and nargs exhaustion. Use it in the
completion generation section so that option-specific choices (e.g.
--shell bash/zsh/tcsh/powershell) and compgen functions are properly
completed, matching the bash implementation's behavior.
https://claude.ai/code/session_01F9QZN9wfoaU7gujvy9Kva9
* Add SHTAB_DEBUG env var for PowerShell completion debugging
When $env:SHTAB_DEBUG is set to 'true', the completer prints
the internal state (wordToComplete, prefix, actionKey, etc.)
to help diagnose completion issues.
https://claude.ai/code/session_01F9QZN9wfoaU7gujvy9Kva9
---------
Co-authored-by: Claude <noreply@anthropic.com>
for more information, see https://pre-commit.ci
pmhahn
left a comment
There was a problem hiding this comment.
I just had a short look. Looks okay mostly, but someone with PowerShell knowledge should have a look at the embedded PS1 code.
Found some minor nits.
Maybe split the large __init__.py into individual files in the future? Or at least move the embedded shell-scripts parts into individual files? See #199 ?
|
|
||
|
|
||
| def _powershell_escape(string: str) -> str: | ||
| """Escape a string for use inside a PowerShell single-quoted string.""" |
There was a problem hiding this comment.
Nit: I would prefer that _ps1_escape adds the surrounding single-quotes itself. I'm no PS1 expert myself, but at least with bash there are several ways to escape strings
- in single-quotes only single-quotes must be handles special
- in double-quotes several characters still need backslash escaping
- without quote many characters must be backslash escaped
With bash shlex.quote() could switch between those choices or even depend on the string to escape and we don't care which is uses as it is an internal details.
With your _ps1_escape here you always must use single quotes and we later can't change it if we find something better.
| return "@{\n" + "\n".join(entries) + "\n}" | ||
|
|
||
|
|
||
| def get_powershell_commands(root_parser, root_prefix, choice_functions=None): |
There was a problem hiding this comment.
Oh oh, I see lots of get_bash_commands() code here. There are many differences, but I don't have time to dig in right now to find out, why they differ.
Maybe someone™ should refactor this and to extract common code? 🤔
There was a problem hiding this comment.
Do you need this addressed in this PR?
There was a problem hiding this comment.
No, but a mental note to whoever to address this soon.
Thank you for the code suggestions! I'll get the rest fixed shortly. Gotta remember to keep things pythonic :P Co-authored-by: Philipp Hahn <pmhahn+github@pmhahn.de>
for more information, see https://pre-commit.ci
Summary
This PR adds PowerShell tab completion support to shtab, enabling native PowerShell argument completion for CLI applications built with argparse.
Key features:
Register-ArgumentCompleter -NativeChanges
shtab/init.py: Added PowerShell completer implementation with:
get_powershell_commands()for parser tree extractioncomplete_powershell()decorated completer functionCHOICE_FUNCTIONSinfrastructureDocumentation & Examples:
README.rstanddocs/index.mdto list PowerShell as supported shelldocs/use.mdexamples/customcomplete.pywith PowerShell preambleTests: Extended existing parametrized test suite to include PowerShell
Building & Testing Locally
Prerequisites
pip install -e .Run tests
Generate completion script manually
Test completion in PowerShell
Enable debug output
Set the
SHTAB_DEBUGenvironment variable to trace completion state: