Registry / devops / autocommand

autocommand

JSON →
library2.2.2pypypi✓ verified 23d ago

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 autocommand
INSTALL
IMPORT
SIG · AUTOCOMMAND
A
autocommand
devopspythonv2.2.2
Install
1.5s avg
Import
216ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v2.2.2 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.228s · 17.9MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.204s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

autocommand
from autocommand import autocommand

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'])
autocommand --version
Debug
Known issues
breakingIn autocommand 2.x, the behavior of calling an `@autocommand`-decorated function directly (i.e., outside of `if __name__ == '__main__'` without passing `__name__` or `True` to the decorator) changed. Instead of immediately executing the function, it now returns a callable wrapper function, typically `main(argv=None)`. If you were relying on direct execution for testing or embedding, you must update your calls.
fix
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.
affects: >=2.0.0
gotchaWhen an `autocommand`-decorated function is run as the main program, `autocommand` internally uses `sys.exit()` with the function's return value. This can bypass normal Python program flow and exception handling mechanisms, which might be unexpected in certain embedding or testing scenarios.
fix
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`.
affects: All
gotchaArgument parsing errors (e.g., missing required arguments, invalid types) or the use of `-h`/`--help` will cause `SystemExit` to be raised by the underlying `argparse` mechanism. This is a standard behavior of `argparse` but can halt programs that don't explicitly handle it.
fix
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.
affects: All
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'autocommand'
The 'autocommand' package is not installed in the current Python environment.
fix
Install the package using 'pip install autocommand'.
ImportError: cannot import name 'autocommand' from 'autocommand'
Attempting to import 'autocommand' from a local script named 'autocommand.py', causing a naming conflict.
fix
Rename the local script to avoid the naming conflict.
TypeError: autocommand() missing 1 required positional argument: 'func'
The 'autocommand' decorator is used without specifying the function to decorate.
fix
Ensure the 'autocommand' decorator is applied directly to a function definition.
autocommand.AutocommandError: Invalid annotation for parameter 'arg'
An invalid type annotation is provided for a function parameter.
fix
Use a valid type annotation such as 'int', 'str', or a tuple like '(type, str)'.
autocommand.KWargError: Function cannot have **kwargs parameter
The decorated function includes a '**kwargs' parameter, which is not supported by 'autocommand'.
fix
Remove the '**kwargs' parameter from the function definition.
Upgrade
Version history
2.2.2latest on PyPI · released Nov 18, 2022
Audit
Dependencies
pythonrequiredRequired Python version
Agent activity
12 hits · last 30 days
node
8
OpenAI (training)
1
Resources
autocommand — pip install autocommand · libregistry