The guts of the command-line parser.
(self, options=None)
| 211 | # opt_h = opt_help # this conflicted with existing 'host' options. |
| 212 | |
| 213 | def parseOptions(self, options=None): |
| 214 | """ |
| 215 | The guts of the command-line parser. |
| 216 | """ |
| 217 | |
| 218 | if options is None: |
| 219 | options = sys.argv[1:] |
| 220 | |
| 221 | # we really do need to place the shell completion check here, because |
| 222 | # if we used an opt_shell_completion method then it would be possible |
| 223 | # for other opt_* methods to be run first, and they could possibly |
| 224 | # raise validation errors which would result in error output on the |
| 225 | # terminal of the user performing shell completion. Validation errors |
| 226 | # would occur quite frequently, in fact, because users often initiate |
| 227 | # tab-completion while they are editing an unfinished command-line. |
| 228 | if len(options) > 1 and options[-2] == "--_shell-completion": |
| 229 | from twisted.python import _shellcomp |
| 230 | |
| 231 | cmdName = path.basename(sys.argv[0]) |
| 232 | _shellcomp.shellComplete(self, cmdName, options, self._shellCompFile) |
| 233 | sys.exit(0) |
| 234 | |
| 235 | try: |
| 236 | opts, args = getopt.getopt(options, self.shortOpt, self.longOpt) |
| 237 | except getopt.error as e: |
| 238 | raise UsageError(str(e)) |
| 239 | |
| 240 | for opt, arg in opts: |
| 241 | if opt[1] == "-": |
| 242 | opt = opt[2:] |
| 243 | else: |
| 244 | opt = opt[1:] |
| 245 | |
| 246 | optMangled = opt |
| 247 | if optMangled not in self.synonyms: |
| 248 | optMangled = opt.replace("-", "_") |
| 249 | if optMangled not in self.synonyms: |
| 250 | raise UsageError(f"No such option '{opt}'") |
| 251 | |
| 252 | optMangled = self.synonyms[optMangled] |
| 253 | if isinstance(self._dispatch[optMangled], CoerceParameter): |
| 254 | self._dispatch[optMangled].dispatch(optMangled, arg) |
| 255 | else: |
| 256 | self._dispatch[optMangled](optMangled, arg) |
| 257 | |
| 258 | if getattr(self, "subCommands", None) and ( |
| 259 | args or self.defaultSubCommand is not None |
| 260 | ): |
| 261 | if not args: |
| 262 | args = [self.defaultSubCommand] |
| 263 | sub, rest = args[0], args[1:] |
| 264 | for cmd, short, parser, doc in self.subCommands: |
| 265 | if sub == cmd or sub == short: |
| 266 | self.subCommand = cmd |
| 267 | self.subOptions = parser() |
| 268 | self.subOptions.parent = self |
| 269 | self.subOptions.parseOptions(rest) |
| 270 | break |