Skip to content

Commit

Permalink
fix(args): support extend on python 3.7
Browse files Browse the repository at this point in the history
  • Loading branch information
lt-mayonesa committed Apr 1, 2023
1 parent d9a88a6 commit e4c0581
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions hexagon/support/args.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,13 @@ def parse_cli_args(args=None):


def init_arg_parser(model, fields=None, prog=None, description=None):
"""
Add Pydantic model to an ArgumentParser
"""
__p = argparse.ArgumentParser(
prog=prog or "hexagon", description=description or "Hexagon CLI", add_help=False
)

if sys.version_info < (3, 8):
__polyfill_extend_action(__p)

for field in model.__fields__.values():
if fields and field.name not in fields:
continue
Expand All @@ -52,6 +53,16 @@ def init_arg_parser(model, fields=None, prog=None, description=None):
return __p


def __polyfill_extend_action(__p):
class ExtendAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
items = getattr(namespace, self.dest) or []
items.extend(values)
setattr(namespace, self.dest, items)

__p.register("action", "extend", ExtendAction)


def __add_parser_argument(parser, field: ModelField):
reprs = field.type_.cli_repr(field)
nargs, action = __config_base_on_type(field)
Expand Down

0 comments on commit e4c0581

Please sign in to comment.