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.
(self, args)
| 231 | return self.usage(msg) |
| 232 | |
| 233 | def parse(self, args): |
| 234 | """Parse a list of arguments and return (options, flags, extra). |
| 235 | |
| 236 | In the returned tuple, "options" is an OptDict with known options, |
| 237 | "flags" is a list of option flags that were used on the command-line, |
| 238 | and "extra" is a list of positional arguments. |
| 239 | """ |
| 240 | try: |
| 241 | (flags,extra) = self.optfunc(args, self._shortopts, self._longopts) |
| 242 | except getopt.GetoptError, e: |
| 243 | self.fatal(e) |
| 244 | |
| 245 | opt = OptDict() |
| 246 | |
| 247 | for k,v in self._defaults.iteritems(): |
| 248 | k = self._aliases[k] |
| 249 | opt[k] = v |
| 250 | |
| 251 | for (k,v) in flags: |
| 252 | k = k.lstrip('-') |
| 253 | if k in ('h', '?', 'help', 'usage'): |
| 254 | self.usage() |
| 255 | if k.startswith('no-'): |
| 256 | k = self._aliases[k[3:]] |
| 257 | v = 0 |
| 258 | elif (self._aliases.get('#') and |
| 259 | k in ('0','1','2','3','4','5','6','7','8','9')): |
| 260 | v = int(k) # guaranteed to be exactly one digit |
| 261 | k = self._aliases['#'] |
| 262 | opt['#'] = v |
| 263 | else: |
| 264 | k = self._aliases[k] |
| 265 | if not self._hasparms[k]: |
| 266 | assert(v == '') |
| 267 | v = (opt._opts.get(k) or 0) + 1 |
| 268 | else: |
| 269 | v = _intify(v) |
| 270 | opt[k] = v |
| 271 | for (f1,f2) in self._aliases.iteritems(): |
| 272 | opt[f1] = opt._opts.get(f2) |
| 273 | return (opt,flags,extra) |