Python’s argparse and lists

Standard

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)
parser.parse_args()

One thought on “Python’s argparse and lists

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.