Skip to main content

python package to generate shell-completion for your CLI

Project description

CodeQL Python Testing

shell-complete-py

python package to generate shell-completion for your CLI

Installation

PyPI - Version

pip3 install shell-complete

Quick-Start

Installation

pip3 install shell-complete

Testing if everything works

To test if the package was installed successfully

$ eval "$(shell-complete shell_complete.__main__:parser)"  # via CLI
$ eval "$(shell-complete --completion)"  # via ShellCompleteAction
$ shell-complete <tab><tab>
--completion  --help        --root        --version
$ shell-complete --root <tab><tab>
dir/ .git/ .venv/  # example. this part will look different on your machine

or pipe it into a file and source that

$ shell-complete --completion > ./shell-complete.completion.bash
$ source ./shell-complete.completion.bash

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, 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)

You can also execute the module itself

python3 -m shell_complete [--root ROOT] parser

Or run the console-script

shell-complete [--root ROOT] parser

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.4.2.tar.gz (13.0 kB view details)

Uploaded Source

Built Distribution

shell_complete-0.4.2-py3-none-any.whl (14.7 kB view details)

Uploaded Python 3

File details

Details for the file shell_complete-0.4.2.tar.gz.

File metadata

  • Download URL: shell_complete-0.4.2.tar.gz
  • Upload date:
  • Size: 13.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.10.12

File hashes

Hashes for shell_complete-0.4.2.tar.gz
Algorithm Hash digest
SHA256 c1ac5d4e9f96fa1c78a2694493c656e2e13e0fae59c77617dd5d23d35a700a51
MD5 42a374811c9d8ccdf384a81fe5173217
BLAKE2b-256 4404cc34cc7a5af887264f9dfcca1286d3fb201e887ff9d5d9b6e8a04602b3af

See more details on using hashes here.

File details

Details for the file shell_complete-0.4.2-py3-none-any.whl.

File metadata

File hashes

Hashes for shell_complete-0.4.2-py3-none-any.whl
Algorithm Hash digest
SHA256 0c4fb13c524ac5327ad495b29d4f08ef93164906fed0741175ed4bbb92135e5e
MD5 aa3f5daa152d036011ee99d6e1a8b633
BLAKE2b-256 b81813edec7995b4ededa087551e9b916a39108567bc1ac3952fcbb0081b559a

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page