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.
pip install autocommandVerified import paths — ran on the pinned version, not inferred.
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.
If integrating or testing, retrieve the callable wrapper and invoke it with an argument list: `wrapper_func = autocommand(False)(your_function); wrapper_func(['arg1', '--flag'])`. Alternatively, ensure `autocommand` is passed `True` or `__name__` if direct execution is desired within a `__main__` block. The change was noted around 2.0.1 for README updates.
Be aware that `sys.exit` is called. For testing, consider catching `SystemExit` or refactoring logic to be testable without full command-line execution. For library use, call the decorated function explicitly with `autocommand(False)` to get the wrapper, which returns the result instead of calling `sys.exit`.
If you need to gracefully handle parsing errors or help requests without exiting the interpreter, wrap the call to the autocommand-generated function in a `try...except SystemExit:` block.
Install the package using 'pip install autocommand'.
Rename the local script to avoid the naming conflict.
Ensure the 'autocommand' decorator is applied directly to a function definition.
Use a valid type annotation such as 'int', 'str', or a tuple like '(type, str)'.
Remove the '**kwargs' parameter from the function definition.