Re: partial option parsing

Jack Moffitt <[email protected]> Mon, 25 Nov 2002 18:26:44 -0700
Newsgroups gmane.comp.python.optik.user
Message-ID <[email protected]>
> Or is it like CVS, with the common options before the subcommand, and
> the subcommand-specific options after? ::
> 
>     command [common opts] subcommand [subcommand-specific opts] [args]

It is like CVS.  The application in question is actually a python based
bk/cvs replacement.

> 1. Set up an OptionParser with the general options.
> 
> 2. Call ``option_parser.disable_interspersed_args()``.  This will
>    force the option parser to stop processing at the first non-option
>    argument, which should be the subcommand.
> 
> 3. Process the first part of the command line::
> 
>        values, rest = option_parser.parse_args(args=sys.argv[1:])
> 
> 4. ``rest[0]`` should have the subcommand.

Up to this part is easy, and I have done it already.

> 5. Create a new OptionParser for the subcommand-specific options.
>    These options are completely independent of the first (common) set,
>    and can even reuse option strings (although I'd advise against it).
> 
> 6. Process the rest of the command line::
> 
>        values, args = option_parser.parse_args(args=rest[1:])

Now the problem is that I want to factor out certain commands to a
common place, since there are common options to subcommands that most
subcommands will use.  Think of CVS's -D date spec command, which works
in almost all CVS subcommands.

I think I've figured out a way to do this.  Basically I think:

1) Create a parser.

2) Load in my command-specific options.

3) pass the parser to the parse_common_options() command.

4) parse_common_options() will add the common options, call the
parser.parse_args(), and then process the common options and pass back
the values opject.  Then the command can process the option values for
the command-specific options.

I think this will work as I want.  Do you see anything wrong or have any
further suggestions?

Optik definately seems to be miles ahead of everything else I've looked
at.  Thanks for the good work :)

jack.


-------------------------------------------------------
This SF.net email is sponsored by: Get the new Palm Tungsten T 
handheld. Power & Color in a compact size! 
http://ads.sourceforge.net/cgi-bin/redirect.pl?palm0002en