Dynamic Command Line Arguments with Python argparse

In our previous example, we used argparse with a static set of arguments, which is a very useful place to start. In some cases, you may wish to data-drive the arguments, to either support user customization or just a fast moving development environment. Just a note that this mechanism does make it more difficult to provide backwards compatibility between different versions of your application.

Python and laptop

In this example, we shall tie in with our use of pandas as a database example. We shall have a fixed set of parameters, but will also dynamically define parameters based on the fields in our database, allowing users to query or set based on these fields.

The following csv file is our little database. The top line defines the fields, which we shall use to generate the command line arguments.

id,colour,weight,type,quantity
1,blue,finger,wool,22.0
2,green,finger,wool,4.0
3,blue,sock,wool-blend,20.0
6,black,chunky,acrylic,35.0
7white,chunky,acrylic,11.0

And the here is our code, which reads the header fields from our database, and then creates attributes for each.

import pandas as pd
import argparse

database = "database.csv"

def getDBFields():
   # read the file
  df = pd.read_csv(database)
  return df.head()


def defineParser():
  valid_actions = ["list","get", "add", "modify", "delete"]
  parser = argparse.ArgumentParser( prog='exampleParser',
              description='Dynamic Argparse Demo',
                    epilog='Use -h for help.')
  # positional argument
  parser.add_argument('action', choices=valid_actions)
  for field in getDBFields():
    # dynamic, optional fields
    parser.add_argument('-' + field, '--' + field) 
  parser.add_argument('-v', '--verbose',
                    action='store_true')  
  return parser


parser = defineParser()
args = parser.parse_args()

And if we run with a -h, we ca see the dynamically generated arguments

python exampleParser.py -h                                    
usage: exampleParser [-h] [-id ID] [-colour COLOUR] [-weight WEIGHT] [-type TYPE] [-quantity QUANTITY] [-v] {list,get,add,modfify,delete}

Dynamic Argparse Demo

positional arguments:
  {list,get,add,modify,delete}

options:
  -h, --help            show this help message and exit
  -id ID, --id ID
  -colour COLOUR, --colour COLOUR
  -weight WEIGHT, --weight WEIGHT
  -type TYPE, --type TYPE
  -quantity QUANTITY, --quantity QUANTITY
  -v, --verbose

Use -h for help.