Skip to main content

Plain Python functions as CLI commands without boilerplate

Project description

https://github.com/neithere/argh/actions/workflows/lint-and-test.yml/badge.svg https://img.shields.io/pypi/format/argh.svg https://img.shields.io/pypi/status/argh.svg https://img.shields.io/pypi/v/argh.svg https://img.shields.io/pypi/pyversions/argh.svg https://img.shields.io/pypi/dd/argh.svg https://readthedocs.org/projects/argh/badge/?version=stable https://readthedocs.org/projects/argh/badge/?version=latest

The power of Argparse with plain Python functions!

Building a command-line interface? Found yourself uttering “argh!” while struggling with the API of argparse? Don’t like the complexity but need the power?

Argh builds on the power of argparse (which comes with Python) and makes it really easy to use. It eliminates the complex API and lets you “dispatch” ordinary Python functions as CLI commands.

Installation

$ pip install argh

Example

import argh

def verify_paths(paths: list[str], *, verbose: bool = False):
    """
    Verify that all given paths exist.
    """
    for path in paths:
        if verbose:
            print(f"Checking {path}...")
        assert os.path.exists(path)

argh.dispatch_command(verify_paths)

Now you can run the script like this:

$ python app.py foo.txt bar/quux.txt

$ python app.py foo.txt bar/quux.txt --verbose
Checking foo.txt...
Checking bar/quux.txt...

$ python app.py -h
usage: app.py [-h] [-v] [paths ...]

Verify that all given paths exist.

positional arguments:
  paths          -

options:
  -h, --help     show this help message and exit
  -v, --verbose  False

Please check the documentation for examples of multiple commands, modularity, help generation, advanced type annotations inspection, decorators and more:

Why Argh?

Argh-powered applications are simple but flexible:

Pythonic, KISS:

Commands are plain Python functions. Almost no CLI-specific API to learn.

Reusable:

Endpoint functions can be used directly outside of CLI context.

Static typing friendly:

100% of the code including endpoint functions can be type-checked. Argh is driven primarily by type annotations.

DRY:

Don’t Repeat Yourself. The amount of boilerplate code is minimal. Among other things, Argh will:

  • infer command name from function name;

  • infer arguments from function signature;

  • infer argument types, actions and much more from annotations.

Modular:

Declaration of commands can be decoupled from assembling and dispatching.

Layered:

The complexity of code raises with requirements.

Transparent:

You can directly access argparse.ArgumentParser if needed.

Subcommands:

Easily nested commands. Argh isolates the complexity of subparsers.

NIH free:

Argh supports completion, progress bars and everything else by being friendly to excellent 3rd-party libraries. No need to reinvent the wheel.

Compact:

No dependencies apart from Python’s standard library.

Author

Developed by Andrey Mikhaylenko since 2010.

See contributors for a list of contributors to this library.

Contribute

The fastest way to improve this project is to submit tested and documented patches or detailed bug reports.

Licensing

Argh is free software: you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation, either version 3 of the License, or (at your option) any later version.

Argh is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License along with Argh. If not, see <http://gnu.org/licenses/>.

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

argh-0.31.2.tar.gz (57.2 kB view details)

Uploaded Source

Built Distribution

argh-0.31.2-py3-none-any.whl (44.8 kB view details)

Uploaded Python 3

File details

Details for the file argh-0.31.2.tar.gz.

File metadata

  • Download URL: argh-0.31.2.tar.gz
  • Upload date:
  • Size: 57.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for argh-0.31.2.tar.gz
Algorithm Hash digest
SHA256 db1c34885804f7d4646c385dc2fb19b45298561322f4c15eae1b133993f9e323
MD5 af222a1097dac25f1e2958222f0a116c
BLAKE2b-256 4eae3c1896d2e8c7f74270686f339aa4a07dd8d4ffddde96488fcb1a726eb072

See more details on using hashes here.

File details

Details for the file argh-0.31.2-py3-none-any.whl.

File metadata

  • Download URL: argh-0.31.2-py3-none-any.whl
  • Upload date:
  • Size: 44.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.7

File hashes

Hashes for argh-0.31.2-py3-none-any.whl
Algorithm Hash digest
SHA256 81a9ab43dacfc0d8e08794a140cda7927908417c8f89a969e6554cfc2e872e14
MD5 295c9794ef7fc2b87baf85ac732a242f
BLAKE2b-256 2f79c619ba76e97e4932f110f8eb133e823057b1aef82c3dd6b9c6bcb6703dd8

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