Adding a Command-Line Interface


See Getting Started.

Activate the Conda workshop enviroment:

$ source activate workshop


We are going to write a command line interface (CLI) for our Python plot function.


  • You will learn how to write a CLI with the Python library argparse.

Run the plotter CLI

Go to the plotter tutorial source:

$ cd ~/birdhouse-workshop/tutorials/03_plotter_cli

See the command line options of our plotter:

$ python -h
usage: [-h] [-V [VARIABLE]] dataset

Plot our well-know image:

$ python --variable air ../../data/

Exercise 1

Play a little bit with the command-line options. Try some other options (-V), use invalid input (water) and skip some arguments.

Exercise 2

Use external data from a Thredds service, like NOAA:

See access methods for a dataset, note OpenDAP:


Use OpenDAP URLs directly as dataset input:

$ python --variable air \

Exercise 3

Extend the command line and with an optional parameter for the timestep to be plotted.

Open your editor on … and happy hacking.

Don’t forget to test often:

$ pytest

Exercise 4

The output name of the plot is always plot.png. Add an optional output parameter to set an output filename.