Parses all options given on the command line (defaults to `sys.argv`). Options look like ``--option=value`` and are parsed according to their ``type``. For boolean options, ``--option`` is equivalent to ``--option=true`` If the option has ``multiple=True``,
(
self, args: Optional[List[str]] = None, final: bool = True
)
| 313 | self._options[normalized] = option |
| 314 | |
| 315 | def parse_command_line( |
| 316 | self, args: Optional[List[str]] = None, final: bool = True |
| 317 | ) -> List[str]: |
| 318 | """Parses all options given on the command line (defaults to |
| 319 | `sys.argv`). |
| 320 | |
| 321 | Options look like ``--option=value`` and are parsed according |
| 322 | to their ``type``. For boolean options, ``--option`` is |
| 323 | equivalent to ``--option=true`` |
| 324 | |
| 325 | If the option has ``multiple=True``, comma-separated values |
| 326 | are accepted. For multi-value integer options, the syntax |
| 327 | ``x:y`` is also accepted and equivalent to ``range(x, y)``. |
| 328 | |
| 329 | Note that ``args[0]`` is ignored since it is the program name |
| 330 | in `sys.argv`. |
| 331 | |
| 332 | We return a list of all arguments that are not parsed as options. |
| 333 | |
| 334 | If ``final`` is ``False``, parse callbacks will not be run. |
| 335 | This is useful for applications that wish to combine configurations |
| 336 | from multiple sources. |
| 337 | |
| 338 | """ |
| 339 | if args is None: |
| 340 | args = sys.argv |
| 341 | remaining = [] # type: List[str] |
| 342 | for i in range(1, len(args)): |
| 343 | # All things after the last option are command line arguments |
| 344 | if not args[i].startswith("-"): |
| 345 | remaining = args[i:] |
| 346 | break |
| 347 | if args[i] == "--": |
| 348 | remaining = args[i + 1 :] |
| 349 | break |
| 350 | arg = args[i].lstrip("-") |
| 351 | name, equals, value = arg.partition("=") |
| 352 | name = self._normalize_name(name) |
| 353 | if name not in self._options: |
| 354 | self.print_help() |
| 355 | raise Error("Unrecognized command line option: %r" % name) |
| 356 | option = self._options[name] |
| 357 | if not equals: |
| 358 | if option.type == bool: |
| 359 | value = "true" |
| 360 | else: |
| 361 | raise Error("Option %r requires a value" % name) |
| 362 | option.parse(value) |
| 363 | |
| 364 | if final: |
| 365 | self.run_parse_callbacks() |
| 366 | |
| 367 | return remaining |
| 368 | |
| 369 | def parse_config_file(self, path: str, final: bool = True) -> None: |
| 370 | """Parses and loads the config file at the given path. |