I want to allow arbitrary command line arguments. If the user provides me with a command line that looks like this
myscript.py --a valueofa --b valueofb posar
Sadly, you can't. If you have to support this, you'll need to write your own option parser =(.
Will argparse do what you want? It was recently added to the standard library. Specifically, you might want to look at this section of the documentation.
arbitrary_args.py
:
#!/usr/bin/env python3
import sys
def parse_args_any(args):
pos = []
named = {}
key = None
for arg in args:
if key:
if arg.startswith('--'):
named[key] = True
key = arg[2:]
else:
named[key] = arg
key = None
elif arg.startswith('--'):
key = arg[2:]
else:
pos.append(arg)
if key:
named[key] = True
return (pos, named)
def main(argv):
print(parse_args_any(argv))
if __name__ == '__main__':
sys.exit(main(sys.argv[1:]))
$./arbitrary_args.py cmd posarg1 posarg2 --foo --bar baz posarg3 --quux
:
(['cmd', 'posarg1', 'posarg2', 'posarg3'], {'foo': True, 'bar': 'baz', 'quux': True})
argparse_arbitrary.py
:
#!/usr/bin/env python3
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-D', action='append', )
D = {L[0]:L[1] for L in [s.split('=') for s in parser.parse_args().D]}
print(D)
$./argparse_arbitrary.py -Ddrink=coffee -Dsnack=peanut
{'snack': 'peanut', 'drink': 'coffee'}