Parse options and handle defaults.

        When using OptionParser, returns tuple (options,args) of
        options and list of non-option command line arguments. When
        using ArgumentParser, returns Namespace object containing both
        options and arguments. In other words, parse() returns the
        same kind of that is returned by the corresponding parser's
        parse_args() function.

def parse(self):
        """Parse options and handle defaults.

        When using OptionParser, returns tuple (options,args) of
        options and list of non-option command line arguments. When
        using ArgumentParser, returns Namespace object containing both
        options and arguments. In other words, parse() returns the
        same kind of that is returned by the corresponding parser's
        parse_args() function."""
        if self.parseTool == 'argparse':
            self.options = self.parser.parse_args()   # called options for backward compat.
        else:
            (self.options, self.args) = self.parser.parse_args()

        # If -i is given, make sure we go into interactive mode at the end
        try:
            if self.options.interactive:
                os.environ['PYTHONINSPECT'] = '1'
        except AttributeError:
            pass

        # Configure logging to file
        if getattr(self.options,'logfile',None):
            self.fileHandler = MyStreamHandler(open(self.options.logfile, 'a'))
            self.fileHandler.setFormatter(FileFormatter(self.options.logtimestampfmt))
            self.logger.addHandler(self.fileHandler)
            if self.options.loglevel is None:
                if self.options.debug:
                    self.options.loglevel = 'DEBUG'
                else:
                    self.options.loglevel = 'INFO'
            try:
                self.fileHandler.setLevel(getLogLevelNo(self.options.loglevel))
            except:
                self.fileHandler.setLevel(logging.INFO)
                error('illegal loglevel: %s',self.options.loglevel)

        # Log command being executed. This goes only to self.fileHandler, because
        # at this point self.emailHandler and consoleHandler are intentionally not yet
        # configured (consoleHandler is still set to level STDOUT).
        info('')
        if self.options.logseparator:
            info(self.options.logseparator)
        #info('%s %s' % (time.asctime(), cmdLine()))
        info(cmdLine())
        info('')

        # Configure logging to e-mail
        if getattr(self.options,'emailto',None):
            hostname = socket.gethostname()
            fromAddr = '%s@%s' % (getpass.getuser(),hostname)
            if self.options.emailsubject:
                subject = self.options.emailsubject
            else:
                subject = 'Report from %s (%s)' % (cmdLine(True),hostname)
            if self.options.emailtriglevel is not None:
                try:
                    triggerLevelNo = getLogLevelNo(self.options.emailtriglevel)
                except:
                    triggerLevelNo = None
                    error('illegal email trigger level %s' % self.options.emailtriglevel)
            else:
                triggerLevelNo = None
            self.emailHandler = BufferingSMTPHandler(fromAddr,self.options.emailto,subject,
                                                     triggerLevelNo=triggerLevelNo)
            self.emailHandler.setFormatter(ConsoleFormatter())
            self.emailHandler.setLevel(logging.WARNING)
            self.logger.addHandler(self.emailHandler)
            try:
                self.emailHandler.setLevel(getLogLevelNo(self.options.emaillevel))
            except:
                self.emailHandler.setLevel(logging.WARNING)
                error('illegal emaillevel %s', self.options.emaillevel)

        # Configure console logging level
        # NOTE: do this after logging command being executed, so that we don't get a logseparator
        #       or the command on the screen
        if self.options.verbose:
            self.consoleHandler.setLevel(logging.INFO)
        if self.options.debug:
            self.consoleHandler.setLevel(logging.DEBUG)
        if getattr(self.options, 'noscreen', False):
            self.consoleHandler.setLevel(9999)   # disable logging to consoleHandler
        if self.errorHandler:
            self.errorHandler.setLevel(logging.STDOUT)
            if self.options.verbose:
                self.errorHandler.setLevel(logging.INFO)
            if self.options.debug:
                self.errorHandler.setLevel(logging.DEBUG)
            if getattr(self.options, 'noscreen', False):
                self.errorHandler.setLevel(9999)   # disable logging to errorHandler

        if self.parseTool == 'argparse':
            return self.options
        else:
            return (self.options, self.args)