'Parse a list of arguments and return (options, flags, extra).
In the returned tuple, "options" is an OptDict with known options,
"flags" is a list of option flags that were used on the command-line,
and "extra" is a list of positional arguments.'

def parse(self, args):

    try:
        (flags, extra) = self.optfunc(args, self._shortopts, self._longopts)
    except getopt.GetoptError as e:
        self.fatal(e)
    opt = OptDict(aliases=self._aliases)
    for (k, v) in self._defaults.iteritems():
        opt[k] = v
    for (k, v) in flags:
        k = k.lstrip('-')
        if (k in ('h', '?', 'help', 'usage')):
            self.usage()
        if (self._aliases.get('#') and (k in ('0', '1', '2', '3', '4', '5', '6', '7', '8', '9'))):
            v = int(k)
            (k, invert) = self._aliases['#']
            opt['#'] = v
        else:
            (k, invert) = opt._unalias(k)
            if (not self._hasparms[k]):
                assert (v == '')
                v = ((opt._opts.get(k) or 0) + 1)
            else:
                v = _intify(v)
        opt[k] = _invert(v, invert)
    return (opt, flags, extra)
