Re: optik...what about subcommands, then? :) (fwd)

David Goodger <[email protected]> Sun, 04 May 2003 23:48:41 -0400
Newsgroups gmane.comp.python.optik.user
Message-ID <[email protected]>
leon j. breedt wrote:
> i have an application i'm currently writing, that uses
> the concept of 'subcommands'.

See the "partial option parsing" thread beginning 2002-11-24 for 
complete instructions.

> i was wondering how this could be best implemented using
> Optik. i tried to subclass OptionParser, but that quickly
> get messy. then i was wondering if a special subclass
> of OptionGroup could do the trick. but once again, that
> will require more changes to OptionParser than i want to
> do, i think. another approach i considered was preparsing
> the argument list, and creating a seperate OptionParser
> for the different slices i wanted to parse.

No need to pre-parse anything.  Just do it the simple way, the way you 
yourself parse the command when you see it.

> but then i lose the nice help output :)

If you put the help option before the subcommand (e.g. "myapp --help 
snapshot"), you'll get help on the main command (which should include a 
list of possible subcommands as well as general options).  If you put 
the help option after the subcommand (e.g. "myapp snapshot --help"), 
you'll get help for the subcommand, which is what I'd expect.  Add a 
line to each subcommand's help text saying "Use 'myapp --help' for 
general options and a list of subcommands" and you're covered.  You 
shouldn't want to get any more complicated than that.  If you do, write 
a man page or proper docs; it's more than Optik/optparse is set up to 
provide.

-- 
David Goodger    http://starship.python.net/~goodger

Programmer/sysadmin for hire: http://starship.python.net/~goodger/cv



-------------------------------------------------------
This sf.net email is sponsored by:ThinkGeek
Welcome to geek heaven.
http://thinkgeek.com/sf