Skip to main content

CLI Command Parser

Project description

downloads py_version coverage_badge build_status Ruff

CLI Command Parser is a class-based CLI argument parser that defines parameters with descriptors. It provides the tools to quickly and easily get started with basic CLIs, and it scales well to support even very large and complex CLIs while remaining readable and easy to maintain.

The primary goals of this project:
  • Make it easy to define subcommands and actions in an clean and organized manner

  • Allow for inheritance so that common parameters don’t need to be repeated

  • Make it easy to handle common initialization tasks for all actions / subcommands once

  • Reduce the amount of boilerplate code that is necessary for setting up parsing and handling argument values

Example Program

from cli_command_parser import Command, Option, main

class Hello(Command, description='Simple greeting example'):
    name = Option('-n', default='World', help='The person to say hello to')
    count: int = Option('-c', default=1, help='Number of times to repeat the message')

    def main(self):
        for _ in range(self.count):
            print(f'Hello {self.name}!')

if __name__ == '__main__':
    main()
$ hello_world.py --name Bob -c 3
Hello Bob!
Hello Bob!
Hello Bob!

$ hello_world.py -h
usage: hello_world.py [--name NAME] [--count COUNT] [--help]

Simple greeting example

Optional arguments:
  --name NAME, -n NAME        The person to say hello to (default: 'World')
  --count COUNT, -c COUNT     Number of times to repeat the message (default: 1)
  --help, -h                  Show this help message and exit

Installing CLI Command Parser

CLI Command Parser can be installed and updated via pip:

$ pip install -U cli-command-parser

There are no required dependencies. Support for formatting wide characters correctly in help text descriptions can be included by adding wcwidth to your project’s requirements, and/or by installing with optional dependencies:

$ pip install -U cli-command-parser[wcwidth]

Python Version Compatibility

Python versions 3.8 and above are currently supported. The last release of CLI Command Parser that supported 3.7 was 2023-04-30. Support for Python 3.7 officially ended on 2023-06-27.

When using Python 3.8, some additional packages that backport functionality that was added in later Python versions are required for compatibility.

To use the argparse to cli-command-parser conversion script with Python 3.8, there is a dependency on astunparse. If you are using Python 3.9 or above, then astunparse is not necessary because the relevant code was added to the stdlib ast module. If you’re unsure, you can install cli-command-parser with the following command to automatically handle whether that extra dependency is needed or not:

$ pip install -U cli-command-parser[conversion]

Project details


Release history Release notifications | RSS feed

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

cli_command_parser-2024.9.7.tar.gz (130.2 kB view details)

Uploaded Source

Built Distribution

cli_command_parser-2024.9.7-py3-none-any.whl (155.0 kB view details)

Uploaded Python 3

File details

Details for the file cli_command_parser-2024.9.7.tar.gz.

File metadata

  • Download URL: cli_command_parser-2024.9.7.tar.gz
  • Upload date:
  • Size: 130.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.10.11

File hashes

Hashes for cli_command_parser-2024.9.7.tar.gz
Algorithm Hash digest
SHA256 16dd78eb3380ccba1672feeb8c66a8016b391d4d0aa1ad0231243669866cf2b6
MD5 7a280111a840f68f0b6c00c13b037a81
BLAKE2b-256 d89a764d4ae3f1a0b5df9910ee3c09c9b1cffc4c047ce8e03cde2d43030d1246

See more details on using hashes here.

File details

Details for the file cli_command_parser-2024.9.7-py3-none-any.whl.

File metadata

File hashes

Hashes for cli_command_parser-2024.9.7-py3-none-any.whl
Algorithm Hash digest
SHA256 01a70da731a31a46387ccbd3ebbb151402cb60b9fca3b85440162960457a927e
MD5 88e93a1ffb81165f7c42e3b491c6ebd6
BLAKE2b-256 a7910e76de9c81554d1aab480fbf1f5896bce477031ad922b2d8babe2bfd31cd

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