args = dotdict(vars(build_parser(args, description=__doc__).parse_args()))
(d: dict, parser: argparse.ArgumentParser = None, **kwargs)
| 755 | |
| 756 | |
| 757 | def build_parser(d: dict, parser: argparse.ArgumentParser = None, **kwargs): |
| 758 | """ |
| 759 | args = dotdict(vars(build_parser(args, description=__doc__).parse_args())) |
| 760 | """ |
| 761 | if 'description' in kwargs: |
| 762 | kwargs['description'] = markup_to_ansi(green(kwargs['description'])) |
| 763 | |
| 764 | if parser is None: |
| 765 | parser = argparse.ArgumentParser(formatter_class=lambda prog: argparse.RawTextHelpFormatter(prog, max_help_position=140), **kwargs) |
| 766 | |
| 767 | help_pattern = f'default = {blue("{}")}' |
| 768 | |
| 769 | for k, v in d.items(): |
| 770 | if isinstance(v, dict): |
| 771 | if 'default' in v: |
| 772 | # Use other params as kwargs |
| 773 | d = v.pop('default') |
| 774 | t = v.pop('type', type(d)) |
| 775 | # h = v.pop('help', markup_to_ansi(help_pattern.format(d))) |
| 776 | h = (v.pop('help') + '; ' + markup_to_ansi(help_pattern.format(d))) if 'help' in v else markup_to_ansi(help_pattern.format(d)) |
| 777 | parser.add_argument(f'--{k}', default=d, type=t, help=h, **v) |
| 778 | else: |
| 779 | # TODO: Add argparse group here |
| 780 | pass |
| 781 | elif isinstance(v, list): |
| 782 | parser.add_argument(f'--{k}', type=type(v[0]) if len(v) else str, default=v, nargs='+', help=markup_to_ansi(help_pattern.format(v))) |
| 783 | elif isinstance(v, bool): |
| 784 | t = 'no_' + k if v else k |
| 785 | parser.add_argument(f'--{t}', action='store_false' if v else 'store_true', dest=k, help=markup_to_ansi(help_pattern.format(v))) |
| 786 | else: |
| 787 | parser.add_argument(f'--{k}', type=type(v), default=v, help=markup_to_ansi(help_pattern.format(v))) |
| 788 | |
| 789 | return parser |