From: rantingrick on
"""
I am pleased to announce optphart (alpha2)!

-------------------
What is optphart?
-------------------
optphart is the nemisis of the asinine interfaces and bulky libraries
you may be accustomed to in the stdlib. All of which clog your scripts
with wasted lines and your memory with complex interfaces far worse
than any colon clogging putrifaction of junk food can hold a candle
to. Do yourself a favor and give your scripts an enema by harnessing
the simplistic elegance of the "optphart" module instead!

----------------------------
Whats new in this release:
----------------------------
- This release is less likely to blow chunks than the last.
- Now you can pass single opts! Woo Hoo!
"""

def optphart(args):
d = {'args':[], 'opts':[]}
for arg in args:
if arg.startswith('-'):
try:
key, value = arg.split('=', 1)
except ValueError:
d['opts'].append(arg[1:])
options = value.split(',')
if len(options) > 1:
d[key[1:]] = filter(None, options)
else:
d[key[1:]] = value
else:
d['args'].append(arg)
return d


if __name__ == '__main__':
args = [
'nakedArgumentOne',
'nakedArgumentTwo',
'-key1=value1',
'-key2=value2,', #trailing comma converts to array
'-file=C:\\temp.txt',
'-options1=Op1,Op2,Op3,Op4',
'-options2=Op1,Op2,Op3,Op4,',
'-help',
'-regex',
]
opts = optphart(args)
#print opts.keys()
for k,v in opts.items():
print '%s -> %s' %(k, v)