Skip to main content

Extension of `argparse` to provide fast and customizable argument parsing.

Project description

slap.core.cli

Extension of argparse to provide fast and customizable argument parsing.

Features

  • Minimal API; interact mostly with argparse or type hints
  • Fast; because argparse is fast
  • Completion; built-in support

Usage (Command API)

import argparse
from typing import Any, Optional

from slap.core.cli import CliApp, Command
from slap.core.cli.completion import CompletionCommand

class HelloCommand(Command):
    """ say hello to someone """

    def init_parser(self, parser: argparse.ArgumentParser) -> None:
        parser.add_argument("name")

    def execute(self, args: Any) -> Optional[int]:
        print(f"Hello, {args.name}!")

app = CliApp("minimal", "0.1.0")
app.add_command("hello", HelloCommand())
app.add_command("completion", CompletionCommand())
app.run()

Gives you the following CLI:

$ python examples/minimal.py
usage: minimal [-h] [-v] [--version] [{hello,completion}] ...

positional arguments:
  {hello,completion}  the subcommand to execute
  ...                 arguments for the subcommand

options:
  -h, --help          show this help message and exit
  -v, --verbose       increase the verbosity level
  --version           show program's version number and exit

subcommands:
  hello               say hello to someone
  completion          completion backend for bash

On Completion

You can run python examples/minimal.py completion --bash to get the code that should be run in your shell to enable completion features. However, you will need to make command available as a first-order command in your shell for completion to work (e.g. minimal instead of python examples/minimal.py).

Usage (Typed API)

from slap.core.cli.typed import Argument, Option, TypedArgsBase, subcommand
from typing import Annotated

class SayHelloArgs(TypedArgsBase):
    name: Annotated[str, Argument()]
    greeting: str
    version: Annotated[None, Option(version="1.0.0", short_name="V")]

args = SayHelloArgs.parse()
print(args)

Gives you the following CLI:

usage: test.py [-h] [--greeting GREETING] [--version] name

positional arguments:
  name

options:
  -h, --help           show this help message and exit
  --greeting GREETING
  --version, -V        show program's version number and exit

And running it gives:

$ python test.py John --greeting Hello
SayHelloArgs(name='John', greeting='Hello', version=None)

Note: Completion is currently not supported for the typed API.

Compatibility

Requires Python 3.7 or higher.

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

slap.core.cli-0.3.0.tar.gz (12.0 kB view details)

Uploaded Source

Built Distribution

slap.core.cli-0.3.0-py3-none-any.whl (13.3 kB view details)

Uploaded Python 3

File details

Details for the file slap.core.cli-0.3.0.tar.gz.

File metadata

  • Download URL: slap.core.cli-0.3.0.tar.gz
  • Upload date:
  • Size: 12.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.64.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for slap.core.cli-0.3.0.tar.gz
Algorithm Hash digest
SHA256 c6690ed51e52c2079452dc8e7c045a1168ab972c61141c0f2fd1a3bb88121e54
MD5 f166a4cdc41f74c76ad1d7663eee514f
BLAKE2b-256 271096b258828c630fa1dd53aa7058e1f4425ab0c95bd523765b4369b38b6d15

See more details on using hashes here.

File details

Details for the file slap.core.cli-0.3.0-py3-none-any.whl.

File metadata

  • Download URL: slap.core.cli-0.3.0-py3-none-any.whl
  • Upload date:
  • Size: 13.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/34.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.9 tqdm/4.64.0 importlib-metadata/4.11.3 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.10.2

File hashes

Hashes for slap.core.cli-0.3.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e67574c3e8eff1fcd800526a3b6373027f8301327a65054383d71624e0101130
MD5 161c53c28952570e7af3e018c4143ca4
BLAKE2b-256 3c9bda64271ea0318eae7dfee3eb730a76bc3f1d1f38f97c75ad6415a74662e4

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