How to move optparse from main to function?

Discussion in 'Python' started by Bob, Feb 23, 2006.

  1. Bob

    Bob Guest

    I'm playing around with optparse and created the code below. How do I
    move that to a function and what variable do I pass?

    >From the documentation it seems like "options.optparse_test" would have

    the value zero if its option, either "-t" or "--test", is not detected
    at the command line. When I issue "print options.optparse_test" when no
    command options are used, "None" is sent to the screen (IIRC)... Is a
    proper check if a command line argument is not used to use the
    following:

    # being check
    if options.optparse_test <> 1:
    print "test parameter NOT detected"
    # end check

    Thanks.

    # begin program
    #!/usr/bin/python

    from optparse import OptionParser
    import string

    parser = OptionParser()
    parser.add_option("-t", "--test", action="count", dest="optparse_test",
    help="testing optparse")

    (options, args) = parser.parse_args()
    print options.optparse_test
    if options.optparse_test == 1:
    print "test parameter detected"
    if options.optparse_test <> 1:
    print "test parameter NOT detected"
    #parser.print_help()

    # end program
    Bob, Feb 23, 2006
    #1
    1. Advertising

  2. Bob

    Giles Brown Guest

    Giles Brown, Feb 23, 2006
    #2
    1. Advertising

  3. Bob

    Jason Drew Guest

    As pointed out, the module documentation is helpful.

    For your 'test' option, I don't think 'action="count"' is the best
    action. 'Test' is basically an on/off option, so why count it? I would
    use:

    parser.add_option("-t", "--test", action="store_true",
    dest="optparse_test", default=False, help="testing optparse")

    Then your code can use
    if options.optparse_test == True: ...
    or briefer:
    if options.optparse_test: ...


    As for putting the optparse code into a function, I sometimes use:

    def parserSetup():
    """Return a configured option parser for this program."""
    parser = OptionParser()
    parser.add_option( ... your option stuff ... )
    parser.add_option( ... )
    return parser

    if __name__=="__main__":
    parser = parserSetup()
    (options, args) = parser.parse_args()
    # Then in your case:
    if options.optparse_test: ...
    Jason Drew, Feb 23, 2006
    #3
  4. Bob

    Bob Guest

    The module documentation helped me construct the meat of my code bu it
    didn't lend a hand on how to build the real meal deal the way Jason's
    explanation did.
    Bob, Feb 23, 2006
    #4
  5. Bob

    Bob Guest

    Yes the documentation is helpful, but I wouldn't have been able to do
    what you did in your code by just looking at section 6.21.2.9. I
    thought I could put "parser = parserSetup()" and "(options, args) =
    parser.parse_args()" in the function. Thanks for helping out with that!
    Bob, Feb 23, 2006
    #5
  6. Bob

    Jason Drew Guest

    You're welcome!

    As usual, each of us is free to write the code whichever way works best
    for the particular problem at hand. That's why the module documentation
    often avoids advocating here-is-the-one-best-way-to-do-it. I just like
    sticking all the option setup stuff in a single function because it's
    conceptually all the same and it makes the main() function or whatever
    read shorter. It's partly down to experience, what little of it I can
    claim.

    Good luck!
    Jason Drew, Feb 25, 2006
    #6
    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. Replies:
    0
    Views:
    524
  2. Ravi
    Replies:
    17
    Views:
    901
    Kenneth Brody
    Apr 1, 2006
  3. ravi
    Replies:
    28
    Views:
    937
    Richard Heathfield
    Sep 26, 2007
  4. Eduardo78
    Replies:
    0
    Views:
    233
    Eduardo78
    Nov 3, 2005
  5. Andrew Tomazos
    Replies:
    2
    Views:
    584
    Nobody
    Dec 12, 2011
Loading...

Share This Page