(parser, commands, *, commonspecs=COMMON_CLI, subset=None)
| 412 | |
| 413 | |
| 414 | def add_commands_cli(parser, commands, *, commonspecs=COMMON_CLI, subset=None): |
| 415 | arg_processors = {} |
| 416 | if isinstance(subset, str): |
| 417 | cmdname = subset |
| 418 | try: |
| 419 | _, argspecs, _ = commands[cmdname] |
| 420 | except KeyError: |
| 421 | raise ValueError(f'unsupported subset {subset!r}') |
| 422 | parser.set_defaults(cmd=cmdname) |
| 423 | arg_processors[cmdname] = _add_cmd_cli(parser, commonspecs, argspecs) |
| 424 | else: |
| 425 | if subset is None: |
| 426 | cmdnames = subset = list(commands) |
| 427 | elif not subset: |
| 428 | raise NotImplementedError |
| 429 | elif isinstance(subset, set): |
| 430 | cmdnames = [k for k in commands if k in subset] |
| 431 | subset = sorted(subset) |
| 432 | else: |
| 433 | cmdnames = [n for n in subset if n in commands] |
| 434 | if len(cmdnames) < len(subset): |
| 435 | bad = tuple(n for n in subset if n not in commands) |
| 436 | raise ValueError(f'unsupported subset {bad}') |
| 437 | |
| 438 | common = argparse.ArgumentParser(add_help=False) |
| 439 | common_processors = apply_cli_argspecs(common, commonspecs) |
| 440 | subs = parser.add_subparsers(dest='cmd') |
| 441 | for cmdname in cmdnames: |
| 442 | description, argspecs, _ = commands[cmdname] |
| 443 | sub = subs.add_parser( |
| 444 | cmdname, |
| 445 | description=description, |
| 446 | parents=[common], |
| 447 | ) |
| 448 | cmd_processors = _add_cmd_cli(sub, (), argspecs) |
| 449 | arg_processors[cmdname] = common_processors + cmd_processors |
| 450 | return arg_processors |
| 451 | |
| 452 | |
| 453 | def _add_cmd_cli(parser, commonspecs, argspecs): |
no test coverage detected
searching dependent graphs…