-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a1dd362
commit 7bd3b18
Showing
1 changed file
with
24 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
|
||
|