I have a Python script that I run from the command line. The script, which uses sys.argv
, requires the following six (6) arguments:
argument 1: the script name (e.g. 'TimeSeries.py')
argument 2: a configuration file (e.g. 'Customer1.cfg')
argument 3: whether the resulting data is daily granular or hourly granular (e.g. -d or -h)
argument 4: which data to retrieve (e.g. -all or -limited)
argument 5: the number of days in the time series (e.g. '30')
argument 6: the 'as of' date
I run this script multiple times per day and have therefore memorized the sequence of arguments.
But, there are others who might run the script infrequently and not know the required arguments (and/or the sequence).
Is there a way for them to query the argument list (along with an example of each argument)? Perhaps the doc string?
Thanks!
There's multiple options:
Use the Click python library and reformat the script using this. This automatically creates a
--help
function that you can use. I have not personally used this one.Use
argparse
from the standard library. For example:This will generate an
-h
option to use with help text per argument. Can be combined with my last suggestion:docstring
to the top of the file with an explanation. Whenever no argument is given, print out the__doc__
. Example with argparse again:Now calling the script with the
-h
option, will print out the docstring at the top, with the rest of the argument help texts. This can ofcourse also be implemented in a simpleif
: