Argh: Effortless CLI

0.31.3 · active · verified Sat Apr 11

Argh is a lightweight Python library that simplifies the creation of command-line interfaces (CLIs) by building on top of the `argparse` module. It allows developers to define CLI commands using plain Python functions, reducing boilerplate code and inferring arguments from function signatures and type annotations. The library is actively maintained, with regular releases, and is currently at version 0.31.3.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a simple CLI application with a single command (`verify_paths`) that accepts a list of paths and an optional verbose flag. Argh automatically infers arguments and types from the function signature and annotations. Run the script with `python your_script.py path1.txt path2.csv --verbose`.

import argh
import os

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}...")
        if not os.path.exists(path):
            raise FileNotFoundError(f"Path does not exist: {path}")
    print("All paths verified successfully.")

if __name__ == "__main__":
    # For a single command application, use dispatch_command.
    # For multiple commands, use argh.dispatch_commands([cmd1, cmd2]).
    # Note: old_name_mapping_policy=False is often recommended during the transition
    # for explicit argument mapping, especially with positional arguments having defaults.
    argh.dispatch_command(verify_paths, old_name_mapping_policy=False)

view raw JSON →