I am trying to find the most Pythonic way to take a string containing command line options:
"-t 500 -x -c 3 -d"
And turn it into a dictionary
{"-t":"500", "-x":True, "-c":"3", "-d": True}
UPDATE: The string should also be able to contain --long options, and words with dashes in the middle:
"-t 500 -x -c 3 -d --long-option 456 -testing weird-behaviour"
Before suggesting that I look into OptionParse module, keep in mind I don't know what the valid options are or anything like that, I am just trying to put the string into a dictionary to allow modifying it based on a different dictionary of options.
The approach I am considering is using split() to get the items into a list and then walking the list and looking for items that begin with a dash "-" and use them as the key, and then somehow getting to the next item on the list for the value. The problem I have is with options that don't have values. I thought of doing something like:
for i in range(0, len(opt_list)):
if opt_list[i][0] == "-":
if len(opt_list) > i+1 and not opt_list[i+1][0] == "-":
opt_dict[opt_list[i]] = opt_list[i+1]
else:
opt_dict[opt_list[i]] = True
But it seems like I am programming in C not Python when I do that...