Open katharinahafner opened 2 years ago
Minimal example:
# config.yml
vars:
a: [1, 2, 3]
b: "foo"
c: "bar"
Add an action to parse dict-string to dict
import argparse
from configargparse import ArgParser, YAMLConfigFileParser
class StoreDict(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
import json
try:
my_dict = json.loads(values)
except json.JSONDecodeError:
my_dict = json.loads(values.replace("'",'"'))
setattr(namespace, self.dest, my_dict)
p = ArgParser(config_file_parser_class=YAMLConfigFileParser)
p.add('-c', '--config_file', default='config.yml', is_config_file=True)
p.add('--vars', dest="vars", action=StoreDict)
args = p.parse_args()
print(args.vars)
Output: {'a': [1, 2, 3], 'b': 'foo', 'c': 'bar'}
If other users would like to see support for dictionaries, please post your views here or in https://github.com/bw2/ConfigArgParse/issues/258
Fix #258