Problem with optparser or In

Discussion in 'Python' started by Florian Lindner, Feb 25, 2004.

  1. Hi,
    I'm not sure where the problems lies...
    I'm using optparse and I've a option defined:

    parser.add_option("-t", "--type", action="callback",
    callback=option_callback, type="string")

    def option_callback(option, opt, value, parser, *args, **kwargs):
    if (str(option) == "-t/--type") and (opt in ("mailbox", "ftp")):
    print "Error!"
    else:
    parser.values.type = opt

    This should print error if the commandline is "--type=ftp" or
    "--type=mailbox" (same for the short args).
    But it never gets till Error... Although opt contains "mailbox" or "ftp".
    What's wrong there?


    Thanks,
    Florian
     
    Florian Lindner, Feb 25, 2004
    #1
    1. Advertising

  2. Florian Lindner

    Peter Otten Guest

    Florian Lindner wrote:

    > Hi,
    > I'm not sure where the problems lies...
    > I'm using optparse and I've a option defined:
    >
    > parser.add_option("-t", "--type", action="callback",
    > callback=option_callback, type="string")
    >
    > def option_callback(option, opt, value, parser, *args, **kwargs):


    Change to
    if (str(option) == "-t/--type") and (value in ("mailbox", "ftp")):
    > if (str(option) == "-t/--type") and (opt in ("mailbox", "ftp")):
    > print "Error!"
    > else:
    > parser.values.type = opt
    >
    > This should print error if the commandline is "--type=ftp" or
    > "--type=mailbox" (same for the short args).
    > But it never gets till Error... Although opt contains "mailbox" or "ftp".
    > What's wrong there?


    In such cases the poor man's debugger aka print statement is your friend.

    print opt

    would reveal that opt contains the actual option and value - surprise,
    surprise -the value.


    Peter
     
    Peter Otten, Feb 25, 2004
    #2
    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. Michele Petrazzo

    optparser question

    Michele Petrazzo, Dec 22, 2006, in forum: Python
    Replies:
    3
    Views:
    531
    Steven Bethard
    Dec 22, 2006
  2. hiral
    Replies:
    3
    Views:
    255
    hiral
    Oct 15, 2010
  3. jimgardener

    using optparser

    jimgardener, Oct 17, 2010, in forum: Python
    Replies:
    3
    Views:
    344
    Michele Simionato
    Oct 17, 2010
  4. Mike

    Problem problem problem :( Need Help

    Mike, May 7, 2004, in forum: ASP General
    Replies:
    2
    Views:
    551
    Bullschmidt
    May 11, 2004
  5. Zhao Yi

    optparser question

    Zhao Yi, Jun 30, 2010, in forum: Ruby
    Replies:
    1
    Views:
    107
    Robert Klemme
    Jun 30, 2010
Loading...

Share This Page