optparse multiple arguments

Discussion in 'Python' started by Ritesh Raj Sarraf, Jun 30, 2006.

  1. Hi,

    I'm having some minor problems with optparse. I'm just worried that
    someone shouldn't say that multiple argument feature isn't implemented
    in optpartse.

    How to tackle multiple arguments to an option ?
    As far as I dug, I've found that >1 arguments are being ignored.

    parser.add_option("", "--my-option", dest="my_option", action="store",
    type="string")

    Now if someone uses it as:
    ../foo --my-option a b c

    I want somehow to store all the three arguments but my_option stores
    only "a" while ignoring b and c.

    Any help?

    Ritesh
     
    Ritesh Raj Sarraf, Jun 30, 2006
    #1
    1. Advertising

  2. Ritesh Raj Sarraf wrote:
    > Hi,
    >
    > I'm having some minor problems with optparse. I'm just worried that
    > someone shouldn't say that multiple argument feature isn't implemented
    > in optpartse.
    >
    > How to tackle multiple arguments to an option ?
    > As far as I dug, I've found that >1 arguments are being ignored.
    >
    > parser.add_option("", "--my-option", dest="my_option", action="store",
    > type="string")
    >
    > Now if someone uses it as:
    > ./foo --my-option a b c
    >
    > I want somehow to store all the three arguments but my_option stores
    > only "a" while ignoring b and c.
    >


    I just noticed that the args variable is holding values b and c.
    the args variables comes from:
    (options, args) = parser.parse_args()

    I guess I only need to figure out now is why args isn't storing
    argument "a" also...

    Ritesh
     
    Ritesh Raj Sarraf, Jun 30, 2006
    #2
    1. Advertising

  3. FIXED: Re: optparse multiple arguments

    Ritesh Raj Sarraf wrote:
    > I just noticed that the args variable is holding values b and c.
    > the args variables comes from:
    > (options, args) = parser.parse_args()
    >
    > I guess I only need to figure out now is why args isn't storing
    > argument "a" also...
    >
    > Ritesh


    I fixed it, I guess.

    parser.add_option("", "--my-option", dest="my_option",
    action="store_true")

    sets my_option to True and the arguments are all stored in the list
    "args". :)

    Ritesh
     
    Ritesh Raj Sarraf, Jun 30, 2006
    #3
  4. Re: FIXED: Re: optparse multiple arguments

    Ritesh Raj Sarraf wrote:

    >> I guess I only need to figure out now is why args isn't storing
    >> argument "a" also...

    >
    > I fixed it, I guess.
    >
    > parser.add_option("", "--my-option", dest="my_option",
    > action="store_true")
    >
    > sets my_option to True and the arguments are all stored in the list
    > "args". :)


    note that whatever action you're using, args holds the *remaining* arguments, after
    option processing.

    </F>
     
    Fredrik Lundh, Jun 30, 2006
    #4
  5. Re: FIXED: Re: optparse multiple arguments

    Ritesh Raj Sarraf wrote:
    > Ritesh Raj Sarraf wrote:
    > > I just noticed that the args variable is holding values b and c.
    > > the args variables comes from:
    > > (options, args) = parser.parse_args()
    > >
    > > I guess I only need to figure out now is why args isn't storing
    > > argument "a" also...
    > >
    > > Ritesh

    >
    > I fixed it, I guess.
    >
    > parser.add_option("", "--my-option", dest="my_option",
    > action="store_true")
    >
    > sets my_option to True and the arguments are all stored in the list
    > "args". :)
    >
    > Ritesh


    It might do you good to read the documentation instead of blindly
    experimenting.

    Anyway,

    parser.add_option("", "--my-option", nargs=3)

    http://docs.python.org/lib/optparse-standard-option-actions.html
     
    Simon Percivall, Jun 30, 2006
    #5
  6. Re: FIXED: Re: optparse multiple arguments

    Simon Percivall wrote:
    >
    > It might do you good to read the documentation instead of blindly
    > experimenting.
    >
    > Anyway,
    >
    > parser.add_option("", "--my-option", nargs=3)
    >
    > http://docs.python.org/lib/optparse-standard-option-actions.html


    That won't help because by design of my program, I can't limit the
    number of arguments a user can pass to it.

    Ritesh
     
    Ritesh Raj Sarraf, Jun 30, 2006
    #6
  7. Re: FIXED: Re: optparse multiple arguments

    Ritesh Raj Sarraf wrote:

    >> http://docs.python.org/lib/optparse-standard-option-actions.html

    >
    > That won't help because by design of my program, I can't limit the
    > number of arguments a user can pass to it.


    do you want options, arguments, or are you just somewhat confused ?

    </F>
     
    Fredrik Lundh, Jun 30, 2006
    #7
  8. Re: FIXED: Re: optparse multiple arguments

    Fredrik Lundh wrote:
    > Ritesh Raj Sarraf wrote:
    >
    > >> http://docs.python.org/lib/optparse-standard-option-actions.html

    > >
    > > That won't help because by design of my program, I can't limit the
    > > number of arguments a user can pass to it.

    >
    > do you want options, arguments, or are you just somewhat confused ?
    >
    > </F>


    I'm not sure about that.

    My program will accept user input. The user can input multiple
    arguments. maybe 1 or maybe 100, that's not definite. I do need option
    because this particular (multiple) argument criteria is for one single
    option.

    Thanks,
    Ritesh
     
    Ritesh Raj Sarraf, Jun 30, 2006
    #8
  9. Ritesh Raj Sarraf wrote:
    > Now if someone uses it as:
    > ./foo --my-option a b c
    >
    > I want somehow to store all the three arguments


    Currently optparse doesn't support options with an arbitrary number of
    arguments. I'm currently working on a optparse-inspired replacement
    which does support this along the lines of:

    parser.add_option('--my-option', nargs='*')

    but it's not quite ready for prime-time yet. I'm hoping to put an
    announcement out in the next couple of weeks.

    STeVe
     
    Steven Bethard, Jul 1, 2006
    #9
    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. Tomi Silander
    Replies:
    3
    Views:
    979
    Grant Edwards
    Apr 6, 2005
  2. Alex Gittens
    Replies:
    1
    Views:
    485
    Peter Hansen
    Jul 10, 2005
  3. Steven Bethard
    Replies:
    0
    Views:
    421
    Steven Bethard
    May 10, 2006
  4. Rocky Zhou
    Replies:
    3
    Views:
    338
    Steven Bethard
    Mar 18, 2007
  5. Ben Woodcroft
    Replies:
    2
    Views:
    182
    Ben Woodcroft
    May 20, 2009
Loading...

Share This Page