def parseOptions(self, options=None): """ The guts of the command-line parser. """ if options is None: options = sys.argv[1:] # we really do need to place the shell completion check here, because # if we used an opt_shell_completion method then it would be possible # for other opt_* methods to be run first, and they could possibly # raise validation errors which would result in error output on the # terminal of the user performing shell completion. Validation errors # would occur quite frequently, in fact, because users often initiate # tab-completion while they are editing an unfinished command-line. if len(options) > 1 and options[-2] == "--_shell-completion": from twisted.python import _shellcomp cmdName = path.basename(sys.argv[0]) _shellcomp.shellComplete(self, cmdName, options, self._shellCompFile) sys.exit(0) try: opts, args = getopt.getopt(options, self.shortOpt, self.longOpt) except getopt.error, e: raise UsageError(str(e))
def parseOptions(self, options=None): """ The guts of the command-line parser. """ if options is None: options = sys.argv[1:] # we really do need to place the shell completion check here, because # if we used an opt_shell_completion method then it would be possible # for other opt_* methods to be run first, and they could possibly # raise validation errors which would result in error output on the # terminal of the user performing shell completion. Validation errors # would occur quite frequently, in fact, because users often initiate # tab-completion while they are editing an unfinished command-line. if len(options) > 1 and options[-2] == "--_shell-completion": from twisted.python import _shellcomp cmdName = path.basename(sys.argv[0]) _shellcomp.shellComplete(self, cmdName, options, self._shellCompFile) sys.exit(0) try: opts, args = getopt.getopt(options, self.shortOpt, self.longOpt) except getopt.error as e: raise UsageError(str(e)) for opt, arg in opts: if opt[1] == '-': opt = opt[2:] else: opt = opt[1:] optMangled = opt if optMangled not in self.synonyms: optMangled = opt.replace("-", "_") if optMangled not in self.synonyms: raise UsageError("No such option '%s'" % (opt, )) optMangled = self.synonyms[optMangled] if isinstance(self._dispatch[optMangled], CoerceParameter): self._dispatch[optMangled].dispatch(optMangled, arg) else: self._dispatch[optMangled](optMangled, arg) if (getattr(self, 'subCommands', None) and (args or self.defaultSubCommand is not None)): if not args: args = [self.defaultSubCommand] sub, rest = args[0], args[1:] for (cmd, short, parser, doc) in self.subCommands: if sub == cmd or sub == short: self.subCommand = cmd self.subOptions = parser() self.subOptions.parent = self self.subOptions.parseOptions(rest) break else: raise UsageError("Unknown command: %s" % sub) else: try: self.parseArgs(*args) except TypeError: raise UsageError("Wrong number of arguments.") self.postOptions()
def parseOptions(self, options=None): """ The guts of the command-line parser. """ if options is None: options = sys.argv[1:] # we really do need to place the shell completion check here, because # if we used an opt_shell_completion method then it would be possible # for other opt_* methods to be run first, and they could possibly # raise validation errors which would result in error output on the # terminal of the user performing shell completion. Validation errors # would occur quite frequently, in fact, because users often initiate # tab-completion while they are editing an unfinished command-line. if len(options) > 1 and options[-2] == "--_shell-completion": from twisted.python import _shellcomp cmdName = path.basename(sys.argv[0]) _shellcomp.shellComplete(self, cmdName, options, self._shellCompFile) sys.exit(0) try: opts, args = getopt.getopt(options, self.shortOpt, self.longOpt) except getopt.error as e: raise UsageError(str(e)) for opt, arg in opts: if opt[1] == '-': opt = opt[2:] else: opt = opt[1:] optMangled = opt if optMangled not in self.synonyms: optMangled = opt.replace("-", "_") if optMangled not in self.synonyms: raise UsageError("No such option '%s'" % (opt,)) optMangled = self.synonyms[optMangled] if isinstance(self._dispatch[optMangled], CoerceParameter): self._dispatch[optMangled].dispatch(optMangled, arg) else: self._dispatch[optMangled](optMangled, arg) if (getattr(self, 'subCommands', None) and (args or self.defaultSubCommand is not None)): if not args: args = [self.defaultSubCommand] sub, rest = args[0], args[1:] for (cmd, short, parser, doc) in self.subCommands: if sub == cmd or sub == short: self.subCommand = cmd self.subOptions = parser() self.subOptions.parent = self self.subOptions.parseOptions(rest) break else: raise UsageError("Unknown command: %s" % sub) else: try: self.parseArgs(*args) except TypeError: raise UsageError("Wrong number of arguments.") self.postOptions()