Last active
September 24, 2020 08:20
-
-
Save sveitser/395cfa458167650a0b9b931b27e9a0e1 to your computer and use it in GitHub Desktop.
Python CLI arguments preference: CLI > Env Var > config file > default
This file contains hidden or 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
import click | |
import click_config_file | |
import yaml | |
def myprovider(file_path, cmd_name): | |
with open(file_path) as config_file: | |
return yaml.safe_load(config_file) | |
@click.command() | |
@click.option("--opt", default="from-default") | |
@click_config_file.configuration_option( | |
provider=myprovider, config_file_name="config.yaml", implicit=False | |
) | |
def main(opt): | |
print("opt", opt) | |
if __name__ == "__main__": | |
main(auto_envvar_prefix="FOO") |
This file contains hidden or 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
opt: from-file |
This file contains hidden or 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
import configargparse | |
import yaml | |
p = configargparse.ArgumentParser( | |
auto_env_var_prefix="FOO_", add_config_file_help=False, add_env_var_help=False, | |
) | |
p.add("-c", "--my-config", required=False, is_config_file=True, help="config file path") | |
p.add("--opt", required=False, help="path to genome file", default="from-default") | |
options = p.parse_args() | |
print("opt", options.opt) |
Author
sveitser
commented
Sep 24, 2020
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment