Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Passing all extra commandline arguments to python program, Optparseraises exception

Reply
Thread Tools

Passing all extra commandline arguments to python program, Optparseraises exception

 
 
sapsi
Guest
Posts: n/a
 
      04-16-2009
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
 
Reply With Quote
 
 
 
 
Dave Angel
Guest
Posts: n/a
 
      04-16-2009
sapsi wrote:
> 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.

 
Reply With Quote
 
 
 
 
Saptarshi
Guest
Posts: n/a
 
      04-17-2009
> > 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.
 
Reply With Quote
 
David Stanek
Guest
Posts: n/a
 
      04-19-2009
On Thu, Apr 16, 2009 at 10:05 AM, sapsi <(E-Mail Removed)> wrote:
> 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']


--
David
blog: http://www.traceback.org
twitter: http://twitter.com/dstanek
 
Reply With Quote
 
Robert Kern
Guest
Posts: n/a
 
      04-21-2009
On 2009-04-16 19:32, Saptarshi wrote:
>>> 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.


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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
want to show list of available options and arguments in my commandline utility Santosh Kumar Python 2 09-16-2012 07:29 AM
How to pass commandline arguments to a file loaded with "irb -rfile" ? IƱaki Baz Castillo Ruby 2 12-23-2009 01:30 PM
functions and arguments.length; passing unknown number of arguments oldyork90 Javascript 10 09-27-2008 03:05 AM
how to write a program that takes arguments from commandline? =?ISO-8859-1?Q?Martin_J=F8rgensen?= C Programming 26 02-14-2006 10:23 AM
perl scripts fail to read commandline arguments reeddeer Perl Misc 5 03-13-2005 12:26 AM



Advertisments