Passing all extra commandline arguments to python program, Optparseraises exception

Discussion in 'Python' started by sapsi, Apr 16, 2009.

  1. sapsi

    sapsi Guest

    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
     
    sapsi, Apr 16, 2009
    #1
    1. Advertising

  2. sapsi

    Dave Angel Guest

    Re: Passing all extra commandline arguments to python program,Optparse raises exception

    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.
     
    Dave Angel, Apr 16, 2009
    #2
    1. Advertising

  3. sapsi

    Saptarshi Guest

    Re: Passing all extra commandline arguments to python program,Optparse raises exception

    > > 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.
     
    Saptarshi, Apr 17, 2009
    #3
  4. sapsi

    David Stanek Guest

    Re: Passing all extra commandline arguments to python program,Optparse raises exception

    On Thu, Apr 16, 2009 at 10:05 AM, 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.
    >



    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
     
    David Stanek, Apr 19, 2009
    #4
  5. sapsi

    Robert Kern Guest

    Re: Passing all extra commandline arguments to python program,Optparse raises exception

    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
     
    Robert Kern, Apr 21, 2009
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?ISO-8859-1?Q?Martin_J=F8rgensen?=

    how to write a program that takes arguments from commandline?

    =?ISO-8859-1?Q?Martin_J=F8rgensen?=, Feb 11, 2006, in forum: C Programming
    Replies:
    26
    Views:
    637
    =?ISO-8859-1?Q?Martin_J=F8rgensen?=
    Feb 14, 2006
  2. mathieu
    Replies:
    3
    Views:
    600
    Bo Persson
    Sep 4, 2009
  3. Iñaki Baz Castillo
    Replies:
    2
    Views:
    508
    Iñaki Baz Castillo
    Dec 23, 2009
  4. reeddeer
    Replies:
    5
    Views:
    196
    Martin Kissner
    Mar 13, 2005
  5. Santosh Kumar
    Replies:
    2
    Views:
    189
    Steven D'Aprano
    Sep 16, 2012
Loading...

Share This Page