From 7bd3b18177eb60728d64c087b9edec903607ee7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=A9r=C3=A9mie=20Gince?= <50332514+JeremieGince@users.noreply.github.com> Date: Fri, 3 Nov 2023 16:41:28 -0400 Subject: [PATCH] Create cmds.py --- src/pythonbasictools/cmds.py | 24 ++++++++++++++++++++++++ 1 file changed, 24 insertions(+) create mode 100644 src/pythonbasictools/cmds.py diff --git a/src/pythonbasictools/cmds.py b/src/pythonbasictools/cmds.py new file mode 100644 index 0000000..2d781b3 --- /dev/null +++ b/src/pythonbasictools/cmds.py @@ -0,0 +1,24 @@ +from typing import Dict + + +def get_cmd_kwargs(defaults: Dict = None): + import sys + import argparse + + if defaults is None: + defaults = {} + cmd_kwargs = {i: v for i, v in enumerate(sys.argv)} + parser = argparse.ArgumentParser() + for pos_arg in sorted([k for k in defaults.keys() if isinstance(k, int)]): + if pos_arg in cmd_kwargs: + parser.add_argument(f"argv_{pos_arg}", type=str, default=defaults[pos_arg]) + for k, v in defaults.items(): + if isinstance(k, int): + cmd_kwargs[k] = cmd_kwargs.get(k, v) + else: + parser.add_argument(f"--{k}", type=type(v), default=v) + args = parser.parse_args() + cmd_kwargs.update(vars(args)) + return cmd_kwargs + +