Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

load config from command arguments #15

Open
dc3ea9f opened this issue Jul 28, 2023 · 6 comments
Open

load config from command arguments #15

dc3ea9f opened this issue Jul 28, 2023 · 6 comments

Comments

@dc3ea9f
Copy link

dc3ea9f commented Jul 28, 2023

Hi, I want to use --from_yaml to specify use which config file, then load it to Config, use Config.parse() to modify the specified config.

The current code is:

from chanfig import Config
from rich import print


if __name__ == '__main__':
    yaml_config = Config()
    yaml_config.add_argument('--from_yaml', type=str, required=True)
    yaml_config.parse()
    config = Config.from_yaml(yaml_config.from_yaml)
    config = config.parse()
    print(config)

While it is a little ugly, and when run with --help, it only shows help messages of yaml_config since the first parser is used.

Is there a more elegant way to do so?

@ZhiyuanChen
Copy link
Owner

config.parse has a parameter named default_config. This allow you to apply your CLI arguments on a config file.

from chanfig import Config
from rich import print


if __name__ == '__main__':
    config = config.parse(default_config="config")
    print(config)
python xxx.py --config default.yaml

@dc3ea9f
Copy link
Author

dc3ea9f commented Jul 29, 2023

Thank you. It fixed it.

@dc3ea9f dc3ea9f closed this as completed Jul 29, 2023
@dc3ea9f
Copy link
Author

dc3ea9f commented Aug 2, 2023

sorry for the late response, I have just tested the snippet, however, when run with --help, it won't show keys in the configuration file, even run with --config.

❯ python run.py --config configs/exps/test.yaml --help
usage: run.py [-h] [--config CONFIG]

options:
  -h, --help       show this help message and exit
  --config CONFIG

@dc3ea9f dc3ea9f reopened this Aug 2, 2023
@ZhiyuanChen
Copy link
Owner

sorry for the late response, I have just tested the snippet, however, when run with --help, it won't show keys in the configuration file, even run with --config.

❯ python run.py --config configs/exps/test.yaml --help

usage: run.py [-h] [--config CONFIG]



options:

  -h, --help       show this help message and exit

  --config CONFIG

Im not sure if it’s possible to achieve this.

Merging a config file happens after parsed the command line arguments.

--help is handled by argparse when parsing the command line arguments.

@dc3ea9f
Copy link
Author

dc3ea9f commented Aug 3, 2023

Yes, I know it is a little weird to load from config and then add to the argparse, but it could be better if we can support this. This may change a lot of logic when loading and parsing the config.

@ZhiyuanChen
Copy link
Owner

Yes, I'll need some time for this, hopefully by the end of August.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants