Last active
September 6, 2024 03:03
-
-
Save csghone/d42aa6e2b2fa81f12dd921e792662386 to your computer and use it in GitHub Desktop.
Create cli args using Dict and ArgumentParser
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import argparse | |
from typing import Any, Dict | |
def create_cli_args_from_dict(parser: argparse.ArgumentParser, inp_args_dict: Dict[str, Any]): | |
ret_val_list = [] | |
for action in parser._actions: | |
if action.dest not in inp_args_dict: continue | |
val = inp_args_dict[action.dest] | |
if isinstance(action, argparse._StoreTrueAction): | |
assert isinstance(val, bool) | |
if val: | |
ret_val_list.append(f"{action.option_strings[-1]}") | |
elif isinstance(action, argparse._StoreFalseAction): | |
assert isinstance(val, bool) | |
if not val: | |
ret_val_list.append(f"{action.option_strings[-1]}") | |
elif isinstance(action, argparse._StoreAction): | |
ret_val_list.append(f"{action.option_strings[-1]} {val}") | |
else: | |
assert False | |
return " ".join(ret_val_list) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment