How to move optparse from main to function?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bob

    How to move optparse from main to function?

    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?
    [color=blue]
    >From the documentation it seems like "options.optpar se_test" would have[/color]
    the value zero if its option, either "-t" or "--test", is not detected
    at the command line. When I issue "print options.optpars e_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.optpars e_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_opti on("-t", "--test", action="count", dest="optparse_ test",
    help="testing optparse")

    (options, args) = parser.parse_ar gs()
    print options.optpars e_test
    if options.optpars e_test == 1:
    print "test parameter detected"
    if options.optpars e_test <> 1:
    print "test parameter NOT detected"
    #parser.print_h elp()

    # end program

  • Giles Brown

    #2
    Re: How to move optparse from main to function?

    Doesn't the module documentation ...



    .... tell you what you need?

    Giles

    Comment

    • Jason Drew

      #3
      Re: How to move optparse from main to function?

      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_opti on("-t", "--test", action="store_t rue",
      dest="optparse_ test", default=False, help="testing optparse")

      Then your code can use
      if options.optpars e_test == True: ...
      or briefer:
      if options.optpars e_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_opti on( ... your option stuff ... )
      parser.add_opti on( ... )
      return parser

      if __name__=="__ma in__":
      parser = parserSetup()
      (options, args) = parser.parse_ar gs()
      # Then in your case:
      if options.optpars e_test: ...

      Comment

      • Bob

        #4
        Re: How to move optparse from main to function?

        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.

        Comment

        • Bob

          #5
          Re: How to move optparse from main to function?

          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_ar gs()" in the function. Thanks for helping out with that!

          Comment

          • Jason Drew

            #6
            Re: How to move optparse from main to function?

            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!

            Comment

            Working...