Re: --help option: sys.exit or return?
Miguel A Figueroa-Villanneva <[email protected]> Tue, 19 Oct 2004 04:17:00 -0400 (EDT)
| Newsgroups | gmane.comp.python.optik.user |
|---|---|
| Message-ID | <Pine.GSO.4.58.0410190401090.1547@scully> |
> > Is there a better solution to this? For example, a way to 'exit' only if > > the script was run from outside the python interpreter (i.e. if __name__ > > == "__main__") and only 'return' from the main function if it was run from > > the interpreter. > > Nope, in Optik __name__ == "optik.option_parser" or similar. And there > is *always* some namespace up the callstack where __name__ == __main__, > interactive session or not. Offhand, I don't know of a good way to > detect interactive sessions. > > In any version of Optik, you can do this: > > try: > parser.parse_args(...) > except SystemExit: > print "caught SystemExit" > I guess this works fine for the simple things I'm doing with python anyway. > In Optik 1.5, help/version options always go via OptionParser.exit(), so > you can override that method if you like. > > Both of these are a bit clumsy for interactive use, though. Hmmmm. > Both methods provide a way to get around this, although not optimal as you mention. I was looking at the 'getargs' package for reference (I'll stick with Optik though) and noticed that they implement an exception system; where they propagate exceptions: HelpQuery, VersionQuery, UsageError, KeyError, and ValueError. This of course, means that it is up to the user to catch these exceptions to exit from the system. I'm thinking this is a nice solution, although I can think of reasons why not to go that direction as well. For example, the user has to write more code and always implement these exceptions... Are there any more fundamental reasons why this approach wouldn't be a good one to follow? Well, I appreciate your response Greg and thanks again for the package! --Miguel ------------------------------------------------------- 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