Re: --help option: sys.exit or return?
Greg Ward <[email protected]> Sun, 24 Oct 2004 20:22:14 -0400
| Newsgroups | gmane.comp.python.optik.user |
|---|---|
| Message-ID | <[email protected]> |
On 21 October 2004, Brian Dorsey said:
> This is exactly my sitation as well. 100% of my scripts which use
> optik (or really optparse) should die with a nice friendly message
> when they have trouble parsing options or really, any other error.
>
> There is one piece of duplicate logic I end up adding to every single
> script I write though:
> opt, args = parser.parse_args(sys.argv[1:])
> if len(args) == 1 and args[0].lower() in ('/?', '/h', '/help'):
> parser.print_help()
> sys.exit(0)
>
> It's clear from the Optik docs that the goal is unixy option parsing,
> and keeping the interface consistant. I completely agree with that.
> And yet I still find myself adding this snippet to nearly every script
> I write. Most of them are cross-platform, or windows only and I find
> that my technically oriented windows users (internal IT folks)
> universally expect to type something with a forward slash to get help.
> Without this check, any script which is OK with just one arg will
> happily try to work on "/help" or whatever.
>
> Would you be open to adding some way to deal with this to Optik?
Categorically, unequivocally, NO.
If you cannot achieve this by writing a simple subclass of OptionParser,
I am willing to entertain ideas for refactoring to support it. (If you
don't know how to subclass OptionParser, I recommend the fine book
*Learning Python* by Mark Lutz and David Ascher.)
> A couple of ideas I had were:
> * Magically use a modified STD_HELP_OPTION on windows machines.
Rule 1: no magic. (Oh wait, that's for Quixote. Never mind, same rule
applies in Optik, I just never wrote it down.)
> * Add another keyword parameter to OptionParser.__init__ with a list
> of additional strings to accept as ways to print help.
> * Allow a list of strings to be passed to the existing keyword
> parameter, something like:
> parser = optparse.OptionParser(add_help_option=['/?', '/h', '/help'])
Implement it yourself, preferably as a subclass of OptionParser, and let
us know how it worked.
Greg
--
Greg Ward <[email protected]> http://www.gerg.ca/
One man's theology is another man's belly laugh.
-------------------------------------------------------
This SF.net email is sponsored by: IT Product Guide on ITManagersJournal
Use IT products in your business? Tell us what you think of them. Give us
Your Opinions, Get Free ThinkGeek Gift Certificates! Click to find out more
http://productguide.itmanagersjournal.com/guidepromo.tmpl