autocommand

2.2.2 · active · verified Sat Apr 11

autocommand is a Python library (current version 2.2.2) designed to simplify the creation of command-line programs from standard Python functions. It automatically converts a function's parameter signature into `argparse`-compatible command-line arguments and handles execution when the module is run as `__main__`. It has an active development status with a mature feature set.

Warnings

Install

Imports

Quickstart

Define a function and decorate it with `@autocommand(__name__)`. When the script is run directly, `autocommand` parses command-line arguments based on the function's signature and executes it. Arguments with default values or type annotations are automatically handled, including boolean flags.

import os
from autocommand import autocommand

@autocommand(__name__)
def greet(name: str = 'World', excited: bool = False):
    """Greets the given name."""
    message = f"Hello, {name}"
    if excited:
        message += '!'
    print(message)

# To run from command line: python your_script.py John --excited
# To run as a function (e.g., in a test):
# if __name__ != '__main__':
#     # In 2.x, calling greet() directly without __name__ returns a callable function
#     # so this is how you'd explicitly call it if not running as __main__
#     # For library usage, you might get the parser and call it like:
#     # func = autocommand(False)(greet) # Pass False or omit argument to return wrapper
#     # func(['Alice', '--excited'])

view raw JSON →