How to handle two-level option processing with optparse

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

    #1

    How to handle two-level option processing with optparse

    optparse is way cool, far superior and cleaner than other options
    processing libraries I've used.

    In the next release of the Python debugger revision, I'd like to add
    debugger options: --help and POSIX-shell style line trace (similar to
    "set -x") being two of the obvious ones.

    So I'm wondering how to arrange optparse to handle its options, but
    not touch the script's options.

    For example the invocation may be something like:
    pdb --debugger-opt1 --debugger-opt2 ... debugged-script -opt1 opt2 ...

    If a --help option is given to the script to be debugged, I want to
    make sure it is not confused for the debugger's help option.

    One simple rule for determining who gets whoit is that options that
    come after the script name don't get touched in debugger's option
    processing.

    Another convention that has been used such as in the X11 "startx"
    command is to use "--" to separate the two sets of options. However
    this isn't as desirable as the simple rule mentioned above; it would
    make entering "--" *all* the time when perhaps most of the time there
    are no debugger options (as is the case now).

    In other systems you can back an indication of the first option that
    hasn't been processed and the remaining options are not touched.

    It seems that with all of the flexibility of optparse it should handle
    this. I'm not sure right now what the best way to do so would be
    though. Suggestions?
  • Giovanni Bajo

    #2
    Re: How to handle two-level option processing with optparse

    R. Bernstein wrote:
    [color=blue]
    > It seems that with all of the flexibility of optparse it should handle
    > this. I'm not sure right now what the best way to do so would be
    > though. Suggestions?[/color]


    If you call OptionParser.di sable_intersper sed_args() on your parser, it will
    stop parsing at the first positional argument, leaving other options unparsed.
    --
    Giovanni Bajo


    Comment

    • R. Bernstein

      #3
      Re: How to handle two-level option processing with optparse

      Giovanni Bajo suggests:
      [color=blue]
      > If you call OptionParser.di sable_intersper sed_args() on your parser,
      > it will stop parsing at the first positional argument, leaving other
      > options unparsed.[/color]

      Wow - that was a quick answer! Thanks - it works great!

      I see how I missed this. Neither disable_.. or enable_.. have document
      strings. And neither seem to described in the optparser section (6.21)
      of the Python Library (http://docs.python.org/lib/module-optparse.html).

      I wonder if something like this might be added to the Python Cookbook.

      Thanks again.

      Comment

      • Steve Holden

        #4
        Re: How to handle two-level option processing with optparse

        R. Bernstein wrote:[color=blue]
        > Giovanni Bajo suggests:
        >
        >[color=green]
        >>If you call OptionParser.di sable_intersper sed_args() on your parser,
        >>it will stop parsing at the first positional argument, leaving other
        >>options unparsed.[/color]
        >
        >
        > Wow - that was a quick answer! Thanks - it works great!
        >
        > I see how I missed this. Neither disable_.. or enable_.. have document
        > strings. And neither seem to described in the optparser section (6.21)
        > of the Python Library (http://docs.python.org/lib/module-optparse.html).
        >
        > I wonder if something like this might be added to the Python Cookbook.
        >[/color]
        Well you are just as capable of adding it as anyone else, so knock
        yourself out!

        regards
        Steve
        --
        Steve Holden +44 150 684 7255 +1 800 494 3119
        Holden Web LLC www.holdenweb.com
        PyCon TX 2006 www.python.org/pycon/

        Comment

        • Magnus Lycka

          #5
          Re: How to handle two-level option processing with optparse

          R. Bernstein wrote:[color=blue]
          > I see how I missed this. Neither disable_.. or enable_.. have document
          > strings. And neither seem to described in the optparser section (6.21)
          > of the Python Library (http://docs.python.org/lib/module-optparse.html).[/color]


          Comment

          • R. Bernstein

            #6
            Re: How to handle two-level option processing with optparse

            Steve Holden <steve@holdenwe b.com> writes:
            [color=blue]
            > Well you are just as capable ...[/color]

            Yes, I guess you are right. Done.

            Couldn't find how to suggest an addition to the Python Cookbook (other
            than some generic O'Reilly email), so I've put a submission to:

            Comment

            • R. Bernstein

              #7
              Re: How to handle two-level option processing with optparse

              Magnus Lycka informs:[color=blue]
              > [in response to my comment]:[color=green]
              > > I see how I missed this. Neither disable_.. or enable_.. have document
              > > strings. And neither seem to described in the optparser section (6.21)
              > > of the Python Library (http://docs.python.org/lib/module-optparse.html).[/color]
              >
              > http://docs.python.org/lib/optparse-other-methods.html[/color]

              Hmmm. A couple things are a little odd about this. First "Other
              methods" seems to be a grab-bag category. A place to put something
              when you don't know where-else to put it. The section called:
              "Querying and manipulating your option parser" seems closer. Better I
              think if the title were changed slightly to

              Querying, manipulating, and changing the default behavior of your
              option parse

              Second, oddly I can't find this section "Other Methods" in the current
              Python SVN source Doc/lib/liboptparse.tex . Best as I can tell, that
              file does seem to be the section documenting optparse.

              Comment

              • Steven Bethard

                #8
                Re: How to handle two-level option processing with optparse

                R. Bernstein wrote:[color=blue]
                > Magnus Lycka informs:[color=green]
                >> [in response to my comment]:[color=darkred]
                >>> I see how I missed this. Neither disable_.. or enable_.. have document
                >>> strings. And neither seem to described in the optparser section (6.21)
                >>> of the Python Library (http://docs.python.org/lib/module-optparse.html).[/color]
                >> http://docs.python.org/lib/optparse-other-methods.html[/color]
                >
                > Hmmm. A couple things are a little odd about this. First "Other
                > methods" seems to be a grab-bag category. A place to put something
                > when you don't know where-else to put it. The section called:
                > "Querying and manipulating your option parser" seems closer. Better I
                > think if the title were changed slightly to
                >
                > Querying, manipulating, and changing the default behavior of your
                > option parse
                >
                > Second, oddly I can't find this section "Other Methods" in the current
                > Python SVN source Doc/lib/liboptparse.tex . Best as I can tell, that
                > file does seem to be the section documenting optparse.[/color]

                Sounds like you should submit a documentation bug:
                http://sourceforge.net/tracker/?grou...70&atid=105470
                Plain text is fine -- just explain what needs to be changed.

                Thanks!

                STeVe

                Comment

                Working...