I am calling a python script with the following command line:
myscript.py --myopt="[(5.,5.),(-5.,-5.)]"
The question is -- how to convert myopt to a list variable. My solution was to use optparse, treating myopt as a string, and using
(options, args) = parser.parse_args()
myopt = eval(options.myopt)
Now, because I used eval()
I feel a bit like Dobby the house elf, having knowingly transgressed the commandments of great (coding) wizards, and wanting to self-flagellate myself in punishment.
But are there better options for parsing lists or tuples or lists of tuples from the command line?.. I've seen solutions that use split()
, but this won't work here since this isn't a simple list. Keep in mind, also, that this is being done in the context of mostly one-off scientific computing with no security concerns -- so perhaps eval()
isn't as evil here?..