I\'m writing a wrapper around the ssh
command line client. After the first positional argument that\'s part of command
, all further options should
Another option is to use parse_known_args, which stops parsing when an unknown argument is encountered.
I had the same problem. I found the solution on the argparse bug tracker: http://code.google.com/p/argparse/issues/detail?id=52
The solution is simple: replace nargs='+'
(or '*'
) with nargs=argparse.REMAINDER
. This special value is not documented, but it does what you want.
I think your best bet to start solving these issues is to try out --
after all your optional args. --
is a pseudo-arg that tells ArgumentParser that everything after is a positional argument. Docs are here
As for prevent arguments from being parsed after a certain point, you can pass part of argv to parse_args. That combined with some introspection can be used to limit what is parsed.