Passing all extra commandline arguments to python program, Optparseraises exception

S

sapsi

Hello,
Im using optparse and python 2.6 to parse some options, my commandline
looks like

prog [options] start|stop extra-args-i-will-pas-on

The options are --b --c --d

The extra options are varied are are passed onto another program e.g --
quiet --no-command , my program doesnt care what these are but instead
passes them onto another program.

I know these will always follow start|stop.

However optparse tries to process them and throws an exception - how
can i prevent this without placing all the extra-args in quotes.

Thank you
Saptarshi
 
D

Dave Angel

sapsi said:
Hello,
Im using optparse and python 2.6 to parse some options, my commandline
looks like

prog [options] start|stop extra-args-i-will-pas-on

The options are --b --c --d

The extra options are varied are are passed onto another program e.g --
quiet --no-command , my program doesnt care what these are but instead
passes them onto another program.

I know these will always follow start|stop.

However optparse tries to process them and throws an exception - how
can i prevent this without placing all the extra-args in quotes.

Thank you
Saptarshi
Preprocess the sys.args before calling optparse.
Simply search sys.args for the string "start" and the string "stop", and
note whichever comes first. Then use slice operators to peel the extra
arguments off of sys.args.
 
S

Saptarshi

Saptarshi
Preprocess the sys.args before calling optparse.
Simply search sys.args for the string "start" and the string "stop", and
note whichever comes first.  Then use slice operators to peel the extra
arguments off of sys.args.


Thanks, i implemented your logic. I thought there was a Optparse
feature to handle this.
 
D

David Stanek

Hello,
Im using optparse and python 2.6 to parse some options, my commandline
looks like

prog [options] start|stop extra-args-i-will-pas-on

The options are --b --c --d

The extra options are varied are are passed onto another program e.g --
quiet --no-command , my program doesnt care what these are but instead
passes them onto another program.

I know these will always follow start|stop.

However optparse tries to process them and throws an exception - how
can i prevent this without placing all the extra-args in quotes.


In Linux (maybe in Windows) you can tell an application to stop
processing args by using '--'. Given this code:
import sys
from optparse import OptionParser
op = OptionParser()
op.add_option('--a', dest='a', action='store_true', default=False)
op.add_option('--b', dest='b', action='store_true', default=False)
opts, args = op.parse_args(sys.argv)
print 'opts:', opts
print 'args:', args

Here is an example use:
eee0:~% python junk.py --a -- --c
{'a': True, 'b': False}
['junk.py', '--c']
 
R

Robert Kern

Thanks, i implemented your logic. I thought there was a Optparse
feature to handle this.

There is:

parser.allow_interspersed_args = False

This means that as soon as the parser hits start|stop, it assumes that
everything following it is an argument and not an option that it needs to try to
parse.

--
Robert Kern

"I have come to believe that the whole world is an enigma, a harmless enigma
that is made terrible by our own mad attempt to interpret it as though it had
an underlying truth."
-- Umberto Eco
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top