Package: cmdfns
cmdfns: make Python functions callable from the command-linecmdfnssearches your codebase for functions decorated withcmdfns.commandand makes them callable from the command-line. For a more general-purpose
command-line library check outclizeorargparse.Basic usageFirst, decorate any functions that you want to make callable withcmdfns.command, e.g.:from cmdfns import command

@command
def say_hello():
    print("Hello, world")

@command
def say_goodbye():
    print("Goodbye, world")Then define your program entrypoint (e.g.main.py) as follows:import cmdfns
import os

if __name__ == "__main__":
    cmdfns.main(search_path=__file__)You can then call the functions from the command-line as follows:$ python main.py say_hello
Hello, world
$ python main.py say_goodbye
Goodbye, worldcmdfns.main()searches for command functions in Python files in the same
directory as the givensearch_pathand recursively in all subdirectories.
Alternatively, you can pass in a custom path for it to search, e.g.:import cmdfns
import os

if __name__ == "__main__":
    # Only search the "commands" subdirectory:
    cmdfns.main(search_path=os.path.join(os.path.dirname(__file__), "commands"))You can also pass a dictionary of command functions directly tocmdfns.main()as follows:import cmdfns
from hello import say_hello, say_goodbye

if __name__ == "__main__":
    # Pass functions directory with no searching:
    cmdfns.main(functions={"say_hello": say_hello, "say_goodbye": say_goodbye})If the named command is not found, or the specialhelpcommand is used, then
usage information is printed to stdout:$ python main.py help
Usage: main.py COMMAND [ARGS]

Where COMMAND is one of:
  say_goodbye
  say_hello

Use 'main.py help COMMAND' for command-specific helpArgument parsingArguments can be passed to command functions either as positional arguments or
as keyword arguments of the formname=value, e.g.:from cmdfns import command

@command
def greet(first_name, last_name):
    print(f"Hello, {first_name} {last_name}")$ python main.py greet James Bond
Hello, James Bond
$ python main.py greet last_name=Bond first_name=James
Hello, James BondIf a function has type hints then arguments of typeint,floatandboolare automatically converted. Forboolarguments, the strings"True","true"and"1"are all converted toTrue, while"False","false"and"0"are converted toFalse.from cmdfns import command

@command
def print_types(a: int, b: float, c: bool):
    print(f"Argument types: {type(a)} {type(b)} {type(c)}")$ python main.py 4 3.2 True
Argument types: <class 'int'> <class 'float'> <class 'bool'>Interactive modecmdfnscan also be run in interactive mode, whereby it will continually read
and execute commands from stdin. To do this, usecmdfns.interactive_main()as follows:import cmdfns

if __name__ == "__main__":
    cmdfns.interactive_main()$ python main.py
> say_hello
Hello, world
> say_goodbye
Goodbye, world
> help
Usage: > COMMAND [ARGS]

Where COMMAND is one of:
  say_goodbye
  say_hello

Use 'quit' to quit or 'help COMMAND' for command-specific help
> quit
$Asynchronous functionsTo call asynchronous functions as command functions, usecmdfns.async_main()orcmdfns.async_interactive_main()instead ofcmdfns.main()as follows:import asyncio
from cmdfns import command

@command
async def say_hello_and_goodbye():
    print("Hello, world")
    await asyncio.sleep(1)
    print("Goodbye, world")import asyncio
import cmdfns

if __name__ == "__main__":
    asyncio.run(cmdfns.async_main(search_path=__file__))$ python main.py say_hello_and_goodbye
Hello, world
Goodbye, world