python package to generate shell-completion for your CLI
Project description
shell-complete-py
python package to generate shell-completion for your CLI
Installation
pip3 install shell-complete
Usage
Either import and use the generate(parser: ArgumentParser)
function directly or add the ShellCompleteAction
to your argparse.
import pathlib
from argparse import ArgumentParser
from shell_complete import ShellCompleteAction, generate, types, associate
parser = ArgumentParser(prog="myprog")
...
# creates the --completion argument that's similar to --help or --version
# --completion print : prints the script for the user to handle it himself or use with eval
# --completion install : creates script in ~/.bash_completion.d/ and `source <script>` in ~/.bashrc
# --completion uninstall : removes script in ~/.bash_completion.d/ and `source <script>` in ~/.bashrc
parser.add_argument('--completion', action=ShellCompleteAction,
help="Generate a bash-completion-script")
# add custom type converter that can still auto-complete
@associate(types.directory)
def dirtype(value: str) -> pathlib.Path:
return pathlib.Path(value)
# smart autocompletion
parser.add_argument('--dest', type=dirtype) # the custom type
parser.add_argument('--source', type=types.file) # or the builtins
parser.add_argument('--bind', type=types.ip_address)
parser.add_argument('--database', type=types.known_hosts)
# subparsers are also supported
subparsers = parser.add_subparsers()
subcommand = subparsers.add_parser("sub")
# Completion is only suggested after typing `myprog sub`
subcommand.add_argument('--hidden', types=types.ip_address)
Support completion types
Basic completion types can be imported via
from shell_complete import types
from shell_complete.argparse import types # both are the same
type | info |
---|---|
types.path |
any type of path (file and directories) |
types.file |
completion for files |
types.directory |
completion for directories |
types.mac_address |
mac addresses |
types.ip_address |
ip addresses |
types.ipv4_address |
ipv4 addresses |
types.ipv6_address |
ipv6 addresses |
types.network_interface |
names of network interfaces |
types.inet_services |
names of inet services |
types.uid |
available user id's |
types.gid |
available group id's |
types.service |
available systemctl services |
types.user_group |
user:group pair |
types.user_at_host |
user@host pair |
types.known_hosts |
name of known hosts |
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
shell_complete-0.1.0.tar.gz
(10.0 kB
view hashes)
Built Distribution
Close
Hashes for shell_complete-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | bb441403eabb43f1b483050ae6d23631db0a5a50d9701f8addd5908bccd8f581 |
|
MD5 | 902f28c27f2686e7f8be63a922f86e9d |
|
BLAKE2b-256 | f042f7d6ccb2555c15ded5e90cdd597baaa6ab8f47543ca7045becd0c86eb657 |