Adding command line options#
The next requirement for our Framework is to allow command line argument handling.
Ths can be added using the click package. It basically works by generating a main entry function, which will be called in the __main__ context. This function receives some decorators to change the way it is called. @click.command() makes it a click command, whereas @click.option can be used to add command line arguments. These are passed into the function using keyword arguments in kwargs or in expanded arguments, shall they be specified in the function definition.
import click
from click.testing import CliRunner
@click.command()
@click.option("-n", "--name", type=str, required=True, help="Select a name")
def run(name):
print(f"Running with option {name=}.")
runner = CliRunner()
result = runner.invoke(run)
print(result.output)
Usage: run [OPTIONS]
Try 'run --help' for help.
Error: Missing option '-n' / '--name'.
result = runner.invoke(run, ["--name", "Pete"])
print(result.output)
Running with option name='Pete'.