Auto-Create a command-line parser entry for a named option. If the option does not exist, it is ignored.
(self, parser, optname, metavar=None, short=None)
| 410 | ) |
| 411 | |
| 412 | def make_parser(self, parser, optname, metavar=None, short=None): |
| 413 | """ |
| 414 | Auto-Create a command-line parser entry for a named option. If the |
| 415 | option does not exist, it is ignored. |
| 416 | """ |
| 417 | if optname not in self._options: |
| 418 | return |
| 419 | |
| 420 | o = self._options[optname] |
| 421 | |
| 422 | def mkf(x, s): |
| 423 | x = x.replace("_", "-") |
| 424 | f = ["--%s" % x] |
| 425 | if s: |
| 426 | f.append("-" + s) |
| 427 | return f |
| 428 | |
| 429 | flags = mkf(optname, short) |
| 430 | |
| 431 | if o.typespec is bool: |
| 432 | g = parser.add_mutually_exclusive_group(required=False) |
| 433 | onf = mkf(optname, None) |
| 434 | offf = mkf("no-" + optname, None) |
| 435 | # The short option for a bool goes to whatever is NOT the default |
| 436 | if short: |
| 437 | if o.default: |
| 438 | offf = mkf("no-" + optname, short) |
| 439 | else: |
| 440 | onf = mkf(optname, short) |
| 441 | g.add_argument( |
| 442 | *offf, |
| 443 | action="store_false", |
| 444 | dest=optname, |
| 445 | ) |
| 446 | g.add_argument(*onf, action="store_true", dest=optname, help=o.help) |
| 447 | parser.set_defaults(**{optname: None}) |
| 448 | elif o.typespec in (int, Optional[int]): |
| 449 | parser.add_argument( |
| 450 | *flags, |
| 451 | action="store", |
| 452 | type=int, |
| 453 | dest=optname, |
| 454 | help=o.help, |
| 455 | metavar=metavar, |
| 456 | ) |
| 457 | elif o.typespec in (str, Optional[str]): |
| 458 | parser.add_argument( |
| 459 | *flags, |
| 460 | action="store", |
| 461 | type=str, |
| 462 | dest=optname, |
| 463 | help=o.help, |
| 464 | metavar=metavar, |
| 465 | choices=o.choices, |
| 466 | ) |
| 467 | elif o.typespec == Sequence[str]: |
| 468 | parser.add_argument( |
| 469 | *flags, |
no outgoing calls