Command Line Interface#
import sys
from copy import copy
from pprint import pprint
from dicfg import ConfigReader
# copying sys.argv such that we can re-use it later
sys_argv = copy(sys.argv)
!cat ./configs/config.yml
config_reader = ConfigReader(name="myconfig")
default:
config_int: 1
config_string: "fire"
config_list: [1, 2, 3]
config_none: None
config_dict:
sub_config: "water"
The main config can be updated via command line interface arguments.#
# Settings can be changed via the command line interface via dotlist notation
sys.argv = sys_argv + ["myconfig.default.config_int=2"]
# The ConfigReader will automatically read the command line arguments
pprint(config_reader.read(), sort_dicts=False)
{'default': {'config_int': 2,
'config_string': 'fire',
'config_list': [1, 2, 3],
'config_none': 'None',
'config_dict': {'sub_config': 'water'}}}
All config types can be update via command line interface arguments.#
sys.argv = sys_argv + [
"myconfig.default.config_int=2",
"myconfig.default.config_list=[2,4,6]",
"myconfig.default.config_dict.sub_config='air'",
]
pprint(config_reader.read(), sort_dicts=False)
{'default': {'config_int': 2,
'config_string': 'fire',
'config_list': [2, 4, 6],
'config_none': 'None',
'config_dict': {'sub_config': 'air'}}}
Command line interface arguments will override corresponding user config values.#
sys.argv = sys_argv + ["myconfig.default.config_int=2"]
user_config = {"myconfig": {"default": {"config_int": -1}}}
pprint(config_reader.read(user_config), sort_dicts=False)
{'default': {'config_int': 2,
'config_string': 'fire',
'config_list': [1, 2, 3],
'config_none': 'None',
'config_dict': {'sub_config': 'water'}}}