With argparse
I would like to be able to mix optional parameters with multiple positional parameters, e.g., like svn
allows:
svn ls first/path -r 1000 second/path
At the moment, this is not officially supported by Python (c.f. http://bugs.python.org/issue14191). I wrote this workaround and I am now wondering, if a) there is a better/easier/more elegant way to do it, and b) if someone can see something in the code that might break it under certain cirumstances:
#!/usr/bin/env python3
import argparse as ap
p = ap.ArgumentParser()
p.add_argument('-v', action='store_true')
p.add_argument('-l', action='store_true')
p.add_argument('files', nargs='*', action='append')
p.add_argument('remainder', nargs=ap.REMAINDER, help=ap.SUPPRESS)
args = p.parse_args()
while args.remainder != []:
args = p.parse_args(args.remainder, args)
print(args)
Usage example:
./test.py a b -v c d
Output:
Namespace(files=[['a', 'b'], ['c', 'd']], l=False, remainder=[], v=True)