Re: optparse, allowing both --foo and foo=99?

Discussion in 'Python' started by Carl Banks, Oct 16, 2009.

  1. Carl Banks

    Carl Banks Guest

    On Oct 15, 10:29 pm, Mark Harrison <> wrote:
    > What's the magic to allow this?  If the value is not specified I
    > would like to use the default value of 1.
    >
    > import optparse
    > p=optparse.OptionParser()
    > p.add_option("--debug")
    >
    > (opts, args) = p.parse_args(['--debug=22']); print opts
    > (opts, args) = p.parse_args(['--debug']);    print opts


    Unless you need to avoid third-party dependencies, install argparse
    (http://code.google.com/p/argparse) and don't give optparse another
    look.

    With argparse (which has a similar but not compatible api) you can do
    it like this:

    p=optparse.ArgumentParser()
    p.add_argument("--debug",nargs='?')


    Carl Banks
     
    Carl Banks, Oct 16, 2009
    #1
    1. Advertising

  2. Carl Banks wrote:
    > On Oct 15, 10:29 pm, Mark Harrison <> wrote:
    >
    >> What's the magic to allow this? If the value is not specified I
    >> would like to use the default value of 1.
    >>
    >> import optparse
    >> p=optparse.OptionParser()
    >> p.add_option("--debug")
    >>
    >> (opts, args) = p.parse_args(['--debug=22']); print opts
    >> (opts, args) = p.parse_args(['--debug']); print opts
    >>

    >
    > Unless you need to avoid third-party dependencies, install argparse
    > (http://code.google.com/p/argparse) and don't give optparse another
    > look.
    >
    > With argparse (which has a similar but not compatible api) you can do
    > it like this:
    >
    > p=optparse.ArgumentParser()
    > p.add_argument("--debug",nargs='?')
    >
    >
    > Carl Banks
    >

    What is the problem with optparse ? AFAIK, it is pretty much satisfying.

    JM
     
    Jean-Michel Pichavant, Oct 16, 2009
    #2
    1. Advertising

  3. Jean-Michel Pichavant wrote:
    > Carl Banks wrote:
    >> On Oct 15, 10:29 pm, Mark Harrison <> wrote:
    >>
    >>> What's the magic to allow this? If the value is not specified I
    >>> would like to use the default value of 1.
    >>>
    >>> import optparse
    >>> p=optparse.OptionParser()
    >>> p.add_option("--debug")
    >>>
    >>> (opts, args) = p.parse_args(['--debug=22']); print opts
    >>> (opts, args) = p.parse_args(['--debug']); print opts
    >>>

    >>
    >> Unless you need to avoid third-party dependencies, install argparse
    >> (http://code.google.com/p/argparse) and don't give optparse another
    >> look.
    >>
    >> With argparse (which has a similar but not compatible api) you can do
    >> it like this:
    >>
    >> p=optparse.ArgumentParser()
    >> p.add_argument("--debug",nargs='?')
    >>
    >>
    >> Carl Banks
    >>

    > What is the problem with optparse ? AFAIK, it is pretty much satisfying.
    >
    > JM
    >

    Please, kindly ignore my question
    (http://argparse.googlecode.com/svn/tags/r101/doc/argparse-vs-optparse.html)
    JM
     
    Jean-Michel Pichavant, Oct 16, 2009
    #3
    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. Alex Vinokur
    Replies:
    7
    Views:
    453
    Jerry Coffin
    Sep 21, 2003
  2. Alex Vinokur
    Replies:
    4
    Views:
    489
    Ron Natalie
    Sep 24, 2003
  3. Replies:
    3
    Views:
    399
    John Roth
    Jul 29, 2005
  4. .rhavin grobert

    vector: Foo[5] == ((foo*)Foo) + 5 ?

    .rhavin grobert, Sep 23, 2008, in forum: C++
    Replies:
    4
    Views:
    414
    JaredGrubb
    Sep 24, 2008
  5. Replies:
    4
    Views:
    165
    Thomas 'PointedEars' Lahn
    Dec 23, 2007
Loading...

Share This Page