I am using getopt to process a command line optional argument, which should accept a list. Something like this:
foo.py --my_list=[1, 2, 3, 4,5]
But this trims everything after “[1,”
My questions are:
A) Is there a way to specify a list without converting it into a string? (using getopt)
B) If I am to convert the list into a string, how to convert this list to a string? e.g. something like mylist.split(“?”) to get rid of square brackets ?? is there a better way?
Thank you
There are two options that I can think of:
appendaction to specify what you want to do as:foo.py --my_list=1 --my_list=2 ....foo.py --my_list='1,2,3,4,5', and then usex.split(',')to get your values in a list. You can usegetoptoroptparsefor this method.The advantage of the first method is that you can get integer values in the list directly, at the expense of the commandline being longer (but you can add a single-charecter option for
--my_listif you want). The advantage of the second is shorter command line, but after thesplit(), you need to convert the string values'1','2', etc., to integers (pretty easy as well).