Given a config_dict: key -> val, returns a list of ["--key", "val"] so that argparse can parse it. Args: config_dict: a dict of key -> val switches: a list containing the str that should be considered as switch when creating commandli
(
config_dict: T.Dict[str, T.Any],
switches: T.List[str] = None,
printout: bool = False,
)
| 56 | |
| 57 | |
| 58 | def compile_argparser_str( |
| 59 | config_dict: T.Dict[str, T.Any], |
| 60 | switches: T.List[str] = None, |
| 61 | printout: bool = False, |
| 62 | ) -> T.List[str]: |
| 63 | """ |
| 64 | Given a config_dict: key -> val, returns a list of |
| 65 | ["--key", "val"] so that argparse can parse it. |
| 66 | |
| 67 | Args: |
| 68 | config_dict: |
| 69 | a dict of key -> val |
| 70 | switches: |
| 71 | a list containing the str that should be considered as switch |
| 72 | when creating commandline parse string |
| 73 | printout: |
| 74 | whether to print the parsed results |
| 75 | |
| 76 | Returns: |
| 77 | a list containing the cmd that can be parsed by `argparse`. |
| 78 | """ |
| 79 | |
| 80 | if switches is None: |
| 81 | switches = {} |
| 82 | |
| 83 | cmd_str = "" |
| 84 | for arg_name, val in config_dict.items(): |
| 85 | if arg_name in switches: |
| 86 | if val: |
| 87 | cmd_str += f"--{arg_name} " |
| 88 | else: |
| 89 | cmd_str += f"--{arg_name} " |
| 90 | if isinstance(val, (list, tuple)): |
| 91 | for v in val: |
| 92 | cmd_str += f"{v} " |
| 93 | else: |
| 94 | cmd_str += f"{val} " |
| 95 | |
| 96 | cmd_list = shlex.split(cmd_str) |
| 97 | if printout: |
| 98 | pprint(f"cmd_str: {cmd_str}") |
| 99 | pprint(f"cmd_list: {cmd_list}") |
| 100 | return cmd_list |
| 101 | |
| 102 | |
| 103 | def print_options( |