Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Don't parse options after the last positional argument

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 also be treated as positional arguments.

Under optparse, I believe this would be done with disable_interspersed_args.

Presently I have something like this:

parser = argparse.ArgumentParser()
parser.add_argument('--parallel', default=False, action='store_true')
# maybe allow no command? this would ssh interactively into each machine...
parser.add_argument('command', nargs='+')
args = parser.parse_args()

But if options are passed as part of the command (such as my_wrapper ls -l), they're instead interpreted by ArgumentParser as unknown options. error: unrecognized arguments: -l

If I use parse_known_args(), the options may be taken out of order.

p = argparse.ArgumentParser()
p.add_argument('-a', action='store_true')
p.add_argument('command', nargs='+')
print(p.parse_known_args())

$ python3 bah.py -b ls -l -a
(Namespace(a=True, command=['ls']), ['-b', '-l'])

Here you can see that -b's position before ls has been lost, and -a has been parsed out from the command, which is not desired.

How can I:

  • Prevent arguments from being parsed after a certain point?
  • Disable parsing of interspersed arguments?
  • Allow arguments with a prefix to be consumed as positional arguments?
like image 587
Matt Joiner Avatar asked Jun 27 '11 05:06

Matt Joiner


People also ask

How do you make a positional argument optional?

To specify optional positional parameters, use square [] brackets.

Can positional arguments be optional?

You can assign an optional argument using the assignment operator in a function definition or using the Python **kwargs statement. There are two types of arguments a Python function can accept: positional and optional. Optional arguments are values that do not need to be specified for a function to be called.

How do you add an optional argument in Argparse?

To add an optional argument, simply omit the required parameter in add_argument() . args = parser. parse_args()if args.

What does Metavar mean in Python?

Metavar: It provides a different name for optional argument in help messages. Provide a value for the metavar keyword argument within add_argument() .


2 Answers

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.

like image 110
John Zwinck Avatar answered Oct 19 '22 09:10

John Zwinck


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.

like image 23
dcolish Avatar answered Oct 19 '22 10:10

dcolish