96. Passing Down Arguments with CLICK

You can use click when you want to add arguments when calling your python file.

Let’s say we want to specify the following when running your python code which will train your model.
– Data Directory
– Number of Epochs
– Batch Size
– Log File Name

Like bellow.

python main.py --data-directory /PATH/TO/DIR --epochs 25 --batch-size 4 --log log

Here is one way to implement using click

import click

#List all desired arguments here
@click.command()
@click.option("--data-directory",
              required=True, # You can specify whether it is mandatory or not
              help="Specify the data directory.")
@click.option(
    "--epochs",
    default=25,
    type=int,
    help="Specify the number of epochs you want to run the experiment for.")
@click.option("--batch-size",
              default=4,
              type=int,
              help="Specify the batch size for the dataloader.")
@click.option("--log",
              default="log",
              type=str,
              help="Specify the name of your log")


# Now you can use the arguments when calling your functions
def main(data_directory, epochs, batch_size, log):
    print(data_directory) #OUTPUT: /PATH/TO/DIR
    print(epochs) #OUTPUT: 25
    print(batch_size) #OUTPUT: 4
    print(log) #OUTPUT: log


if __name__ == "__main__":
    main()