click is a python library to help you make command-line interfaces

Basic structure

A script should start with the following code:

import click

@click.group()
def manager():
    pass

if __name__ == '__main__':
    manager()

This manager function groups all commands.
A command can be added with:

@manager.command()
def do_something():
    """Does something."""
    my_code()

Arguments

Making a command with arguments and options can be done as below:

@logs.command()
@click.argument('my_argument', required=False)
@click.option("-o", "--option")
@click.option("-f", "--flag", is_flag=True)
def my_command(my_argument, option, flag):
    my_code()

This command can be called as: python3 myscript.py my_command <arg> -o <my option> -f, which would result in : my_argument=<arg>, option=<my option> and flag=True

Groups

It is possible to add command groups to your script. These can be used as seen below:

@manager.group()
def logs():
    """Subcommand: Manage logs."""
    pass

This group is used to extend the basic script. Commands can be added to the group, as below:

@logs.command()
def show():
    """Show logs. Optionally, specify a script and/or log-level"""
    print_logs()

You could call this command by running python3 myscript.py logs show.

Splitting Groups

Alternatively, you may want to split groups into separate files (e.g. main.py and logs.py)
If you chose to do this, you may want to split them like this:
logs.py:

import click

@click.group()
def logs():
    """Subcommand: Manage logs."""
    pass

@logs.command()
def show():
    """Show logs. Optionally, specify a script and/or log-level"""
    print_logs()

main.py:

import click
from logs import logs

@click.group()
def manager():
    pass

manager.add_command(logs)

if __name__ == '__main__':
    manager()

NOTE: The manager.group() from the previous example changes into a click.group()

Other features

"""Print text. This function handles some terminal checks"""
click.echo("my text")

"""Ask the user to confirm something, with a Y/N dialogue
        abort:   Quits the script if the user did not confirm
        returns: A boolean"""
click.confirm("my text", abort=False)

"""Ask the user for input.
        type:    The type of value you expect
        default: The default value"""
value = click.prompt("my text", type=int, default=42)