CLI arguments and config with click

Basic usage of click

Command line arguments get passed to a function that you then call later on.

import click

@click.command()
@click.option('-f', '--first', default='first')
@click.option('-s', '--second', default=10)


def test(first, second):
    print('First', first)
    print('Second', second)


test()

Using click with config file

Using the module click_config_file we can load the parameter file similar to this:

import click
import click_config_file

@click.command()
@click.option('--name', default='World', help='Who to greet.')
@click_config_file.configuration_option()

def hello(name):
    click.echo('Hello {}!'.format(name))

hello()

the parameter file should read:

name='Othmane'

The command to use the config file is:

python main.py --config myconfig.txt