Skip to content

Commit a40e516

Browse files
authored
Merge pull request #212 from HeyItsGilbert/main
2 parents c2d1527 + c813587 commit a40e516

9 files changed

Lines changed: 503 additions & 25 deletions

File tree

‎.github/workflows/test.yml‎

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,21 +9,24 @@ jobs:
99
if: github.event_name != 'pull_request' || !contains('OWNER,MEMBER,COLLABORATOR', github.event.pull_request.author_association)
1010
strategy:
1111
matrix:
12-
python: [3.9, 3.14]
13-
runs-on: ubuntu-latest
12+
python: ['3.x']
13+
os: [ubuntu, macos, windows]
14+
include: [{python: 3.9, os: ubuntu}] # min supported (see pyproject.toml)
15+
runs-on: ${{ matrix.os }}-latest
1416
steps:
1517
- uses: actions/checkout@v7
1618
with: {fetch-depth: 0}
1719
- uses: actions/setup-python@v7
1820
with:
1921
python-version: ${{ matrix.python }}
20-
- run: sudo apt-get update && sudo apt-get install -y zsh fish tcsh # generated-script tests
22+
- if: matrix.os == 'ubuntu'
23+
run: sudo apt-get update && sudo apt-get install -y zsh fish tcsh # generated-script tests
2124
- run: pip install -e .[dev]
22-
- run: env PYTHONPATH=. pytest --cov-report=xml --cov-report=term
25+
- run: env PYTHONPATH=. pytest --cov-report=xml --cov-report=term ${{ matrix.os == 'windows' && '-k "not bash" --cov-fail-under=75' || '' }}
2326
- uses: codecov/codecov-action@v7
2427
with:
2528
token: ${{ secrets.CODECOV_TOKEN }}
26-
flags: ${{ matrix.python }}
29+
flags: ${{ matrix.python }}-${{ matrix.os }}
2730
diff:
2831
if: (github.event_name != 'pull_request' || !contains('OWNER,MEMBER,COLLABORATOR', github.event.pull_request.author_association)) && github.ref != 'refs/heads/main'
2932
runs-on: ubuntu-latest

‎CONTRIBUTING.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ Most of the magic lives in [`shtab/__init__.py`](./shtab/__init__.py).
4141
- `complete_zsh()`
4242
- `complete_tcsh()`
4343
- `complete_fish()`
44+
- `complete_powershell()`
4445
- ...
4546
- `add_argument_to()` - convenience function for library integration
4647
- `Optional()`, `Required()`, `Choice()` - legacy helpers for advanced completion (e.g. dirs, files, `*.txt`)

‎README.rst‎

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ Features
2020

2121
- Outputs tab completion scripts for multiple shells
2222

23-
- ``bash``, ``zsh``, ``fish``, ``tcsh``
23+
- ``bash``, ``zsh``, ``fish``, ``tcsh``, ``powershell``
2424

2525
- Supports
2626

@@ -61,8 +61,8 @@ There are two ways of using ``shtab``:
6161
- `Library Usage <https://tqdm.github.io/shtab/use/#library-usage>`_: as a library integrated into your CLI application
6262

6363
- adds a couple of lines to your application
64-
- argument mode: end-users execute ``your_cli_app --print-completion {bash,zsh,tcsh,fish}``
65-
- subparser mode: end-users execute ``your_cli_app completion {bash,zsh,tcsh,fish}``
64+
- argument mode: end-users execute ``your_cli_app --print-completion {bash,zsh,tcsh,fish,powershell}``
65+
- subparser mode: end-users execute ``your_cli_app completion {bash,zsh,tcsh,fish,powershell}``
6666

6767
Examples
6868
--------
@@ -88,8 +88,7 @@ Contributions
8888

8989
Please do open `issues <https://github.com/tqdm/shtab/issues>`_ & `pull requests <https://github.com/tqdm/shtab/pulls>`_! Some ideas:
9090

91-
- support ``powershell`` (#212)
92-
- support ``python -m`` prefix (#55)
91+
- support ``python -m`` prefix (`#55 <https://github.com/tqdm/shtab/issues/55>`_)
9392

9493
See
9594
`CONTRIBUTING.md <https://github.com/tqdm/shtab/blob/main/CONTRIBUTING.md>`_

‎docs/index.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
## Features
1818

1919
- Outputs tab completion scripts for multiple shells
20-
- `bash`, `zsh`, `fish`, `tcsh`
20+
- `bash`, `zsh`, `fish`, `tcsh`, `powershell`
2121
- Supports
2222
- [`argparse`](https://docs.python.org/library/argparse)
2323
- [`docopt`](https://pypi.org/project/docopt) (via [`argopt`](https://pypi.org/project/argopt))
@@ -42,6 +42,7 @@ shell | location
4242
[`zsh`](https://github.com/zsh-users/zsh-completions/blob/master/zsh-completions-howto.org) | `/usr/local/share/zsh/site-functions/_NAME`
4343
[`tcsh`](https://github.com/tcsh-org/tcsh/blob/master/complete.tcsh) | `/etc/profile.d/completion_NAME.csh`, source in `~/.cshrc` or `~/.tcshrc`
4444
[`fish`](https://fishshell.com/docs/current/completions.html#where-to-put-completions) | `~/.config/fish/completions/NAME.fish`
45+
[`powershell`](https://learn.microsoft.com/en-us/powershell/scripting/learn/shell/creating-profiles#adding-customizations-to-your-profile) | `~\.config\powershell\completions\NAME.ps1`, source in `$PROFILE`
4546

4647
For more information, click on the shells above, and/or see [CLI Usage](use.md#cli-usage).
4748

‎docs/use.md‎

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ There are two ways of using `shtab`:
77
- end-users execute `shtab your_cli_app.your_parser_object`
88
- [Library Usage](#library-usage): as a library integrated into your CLI application
99
- adds a couple of lines to your application
10-
- argument mode: end-users execute `your_cli_app --print-completion {bash,zsh,tcsh}`
11-
- subparser mode: end-users execute `your_cli_app completion {bash,zsh,tcsh}`
10+
- argument mode: end-users execute `your_cli_app --print-completion {bash,zsh,tcsh,fish,powershell}`
11+
- subparser mode: end-users execute `your_cli_app completion {bash,zsh,tcsh,fish,powershell}`
1212

1313
## CLI Usage
1414

@@ -52,8 +52,8 @@ Below are various examples of enabling `shtab`'s own tab completion scripts.
5252

5353
=== "zsh"
5454

55-
Note that `zsh` requires completion script files to be named `_{EXECUTABLE}`
56-
(with an underscore prefix).
55+
!!! info
56+
`zsh` requires completion script files to be named `_{EXECUTABLE}` (with an underscore prefix).
5757

5858
```sh
5959
# note the underscore `_` prefix
@@ -103,6 +103,39 @@ Below are various examples of enabling `shtab`'s own tab completion scripts.
103103
| sudo tee /usr/share/fish/vendor_completions.d/shtab.fish
104104
```
105105

106+
=== "powershell"
107+
108+
!!! info
109+
PowerShell 7+ (`pwsh`) is fully supported, while
110+
Windows PowerShell 5 (`powershell.exe`) is only partially supported.
111+
112+
```powershell
113+
New-Item -Path ~\.config\powershell\completions -ItemType Directory -Force
114+
shtab --shell=powershell shtab.main.get_main_parser --error-unimportable `
115+
| Out-File -FilePath ~\.config\powershell\completions\shtab.ps1
116+
# Add to $PROFILE:
117+
. ~\.config\powershell\completions\shtab.ps1
118+
```
119+
120+
Eager:
121+
122+
Add the following to your PowerShell profile (`$PROFILE`):
123+
124+
```powershell
125+
shtab --shell=powershell shtab.main.get_main_parser `
126+
| Out-String | Invoke-Expression
127+
```
128+
129+
Or save to a file and dot-source it from your profile:
130+
131+
```powershell
132+
New-Item -Path ~\.config\powershell\completions -ItemType Directory -Force
133+
shtab --shell=powershell shtab.main.get_main_parser `
134+
| Out-File -FilePath ~\.config\powershell\completions\shtab.ps1
135+
# Add to $PROFILE:
136+
. ~\.config\powershell\completions\shtab.ps1
137+
```
138+
106139
### argparse
107140

108141
Any existing `argparse`-based scripts should be supported with minimal effort.
@@ -155,6 +188,15 @@ Assuming this code example is installed in `MY_PROG.cli`, simply run:
155188
| sudo tee /usr/share/fish/vendor_completions.d/MY-PROG.fish
156189
```
157190

191+
=== "powershell"
192+
193+
```powershell
194+
New-Item -Path ~\.config\powershell\completions -ItemType Directory -Force
195+
shtab --shell=powershell -u MY_PROG.cli.get_main_parser `
196+
| Out-File -FilePath ~\.config\powershell\completions\MY-PROG.ps1
197+
. ~\.config\powershell\completions\MY-PROG.ps1
198+
```
199+
158200
### click
159201

160202
Speedup `click`'s completions (and get support for more shell types) by changing from e.g. `_MY_PROG_COMPLETE=bash_source MY-PROG` to `shtab MY_PROG.cli.main --prog MY-PROG -s bash`

‎examples/customcomplete.py‎

100755100644
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,13 @@
99

1010
import shtab # for completion magic
1111

12+
# WARNING: shtab.cmd is (re)run by your shell on each tab press, so could be slow
13+
COMPLETE_TOKEN = shtab.cmd("head -c5 /dev/random | base32")
14+
# override powershell function to a Windows-compatible approximate equivalent
15+
COMPLETE_TOKEN['preamble']['powershell'] = ( # type: ignore[index]
16+
f"function {COMPLETE_TOKEN['powershell']}"
17+
" { Get-Random -Count 1 -InputObject (10000..99999) }")
18+
1219

1320
def process(args):
1421
print(f"received <token>={args.token} [<suffix>={args.suffix}]"
@@ -25,8 +32,7 @@ def process(args):
2532
# mypy: disable-error-code="attr-defined"
2633
parser = subparsers.add_parser("process", help="parse files")
2734
# dynamic command tab completion builtin shortcut
28-
# WARNING: shtab.cmd is (re)run by your shell on each tab press, so could be slow
29-
parser.add_argument("token").complete = shtab.cmd("head -c5 /dev/random | base32")
35+
parser.add_argument("token").complete = COMPLETE_TOKEN
3036
# file tab completion builtin shortcut
3137
parser.add_argument("-i", "--input-file", dest="input_name").complete = shtab.FILE
3238
# directory tab completion builtin shortcut

‎pyproject.toml‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ maintainers = [{name = "tqdm developers", email = "devs@tqdm.ml"}]
2020
description = "Automagic shell tab completion for Python CLI applications"
2121
readme = "README.rst"
2222
requires-python = ">=3.9"
23-
keywords = ["tab", "completion", "shell", "bash", "zsh", "tcsh", "fish", "argparse"]
23+
keywords = ["tab", "completion", "argparse", "shell", "bash", "zsh", "tcsh", "fish", "powershell", "pwsh"]
2424
license = {text = "MPL-2.0"}
2525
# Trove classifiers (https://pypi.org/pypi?%3Aaction=list_classifiers)
2626
classifiers = [

0 commit comments

Comments
 (0)