Click¶
return value using click¶
https://stackoverflow.com/questions/26246824/how-do-i-return-a-value-when-click-option-is-used-to-pass-a-command-line-argume
Use standalone_mode=False:
Multiple Values from Environment Values¶
https://click.palletsprojects.com/en/7.x/options/
The default implementation for all types is to split on
whitespaceThe exceptions to this rule are the
FileandPathtypes: Unix systemscolon(:), and Windowssemicolon(;)
functions¶
click.add_commandRegisters anotherclass:Commandwith this groupclick.make_pass_decoratorGiven an object type this creates a decorator with innermost context of typeclick.argumentAttaches an argument to the commandclick.optionAttaches an option to the command
click.ParamType¶
Represents the type of a parameter. Validates and converts values from the command line or Python into the correct type.
subcommands¶
organize the commands as subcommands within the task group, providing a more modular and structured CLI application.
main.py
import click
from task import task
@click.group()
def cli():
"""A simple todo application."""
pass
# Add the 'task' subcommand group to the main CLI
cli.add_command(task)
if __name__ == '__main__':
cli()
task.py
import click
from functools import partial
# Add some parameters to click.option
click.option = partial(click.option, show_default=True)
@click.group()
def task():
"""Manage tasks."""
pass
@task.command()
@click.argument('task')
def add(task):
"""Add a new task."""
click.echo(f'Added task: {task}')
@task.command('list')
@click.option('--show/--no-show', default=True)
def list_tasks(show: bool):
"""List all tasks."""
click.echo("List of tasks:")
if show:
click.echo('Number of tasks: ')
Here
We create a subcommand group task using
@click.group()in thetask.pyfile.Inside the
task.pyfile, we use@task.command()to define subcommands within the task group for add and list_tasks.In the
main.pyfile, we add the task subcommand group to the main CLI usingcli.add_command(task).
Now, you can use the task subcommand group with its subcommands: