Jan 21 2015

Python’s argparse and lists

Category: ComputerTuxevara @ 14:30

While Python’s argparse allows to declare a command line parameter to be of any supported type, this does neither work nor is it recommended for the “list” type.  A workaround for passing a list of comma separated items is to use your own type, which for argparse means that the value of “type” must be callable as shown in the example below.

def csv_list(string):
   return string.split(',')

parser = argparse.ArgumentParser()
parser.add_argument('-l', type = csv_list)