Re: --help option: sys.exit or return?

Greg Ward <[email protected]> Mon, 18 Oct 2004 22:22:35 -0400
Newsgroups gmane.comp.python.optik.user
Message-ID <[email protected]>
On 18 October 2004, Miguel A Figueroa-Villanneva said:
> When Optik calls sys.exit(), because it encounters --version, --help, or
> an error your interactive Python interpreter will exit.

Hmmm, good point.  I very rarely play with Optik in an interactive
session, so I've never been bitten by this.  But the interactive
interpreter is one of Python's best features, so I can see your point.

> 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"

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.

        Greg
-- 
Greg Ward <[email protected]>                         http://www.gerg.ca/
Clarke's Law, redux:
Any sufficiently advanced technology is indistinguishable from a rigged demo.


-------------------------------------------------------
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