I am using argparse to specify some arguments as follows:
my_parser = argparse.ArgumentParser()
my_parser.add_argument("--script_path", nargs='?', type=str, const='', default='', help="Path of the script to pull.")
my_parser.add_argument("--script", nargs='?', type=str, const='', default='', help="Name of the script to get pulled, without script extension.")
my_parser.add_argument("--project", nargs='?', type=str, const='', default='', help="Project.")
my_args = my_parser.parse_args()
my_script_path = my_args.script_path
my_script = my_args.script
my_project = my_args.project
Now I am trying to do the same but instead to have the above arguments defined via a .json file that I would load. I chose .json because it seemed right, feel free to suggest something better.
What I have tried is having a .json file like this:
[
{
"name_or_flags": ["-sp", "--script_path"],
"nargs": "?",
"const": "",
"default": "",
"type": "str",
"help": "The absolute path of the script to run."
},
...
]
After I loaded the file, I tried and failed in the below:
my_parser.add_argument(<combination of all keys, values from .json as a dictionary>)
my_parser.add_argument(<*unnamed_tup, **named_dict>)
#unnamed tuple since name_or_flags isn't supposed to be used
#unnamed tuple is only made from name_or_flags
No matter what I do it doesn't work.
Has anyone done something similar?
I am not looking to add values via the external file, like in: Using Argparse and Json together
Just to define the arguments.
Thanks!