Parse config files, environment, and command-line arguments, to get a single collection of options(解析配置文件、环境和命令行参数,以获取单个选项集合)
问题描述
Python 的标准库有用于配置文件解析的模块(configparser),环境变量读取(os.environ),以及命令行参数解析(argparse).我想编写一个程序来完成所有这些,并且:
Python's standard library has modules for configuration file parsing (configparser), environment variable reading (os.environ), and command-line argument parsing (argparse). I want to write a program that does all those, and also:
具有选项值的级联:
- 默认选项值,被覆盖
- 配置文件选项,被 覆盖
- 环境变量,被 覆盖
- 命令行选项.
允许在命令行中指定一个或多个配置文件位置,例如--config-file foo.conf,并读取它(代替或附加到通常的配置文件).这仍然必须遵守上述级联.
Allows one or more configuration file locations specified on the command line with e.g. --config-file foo.conf, and reads that (either instead of, or additional to, the usual configuration file). This must still obey the above cascade.
允许在一个地方定义选项来确定配置文件和命令行的解析行为.
Allows option definitions in a single place to determine the parsing behaviour for configuration files and the command line.
将已解析的选项统一为一个选项值的单一集合,以供程序的其余部分访问,而无需关心它们来自何处.
Unifies the parsed options into a single collection of option values for the rest of the program to access without caring where they came from.
我需要的一切显然都在 Python 标准库中,但它们不能顺利协同工作.
Everything I need is apparently in the Python standard library, but they don't work together smoothly.
如何在与 Python 标准库的偏差最小的情况下实现这一目标?
How can I achieve this with minimum deviation from the Python standard library?
推荐答案
似乎标准库没有解决这个问题,让每个程序员都在拼凑 configparser 和 argparse和 os.environ 以笨拙的方式组合在一起.
It seems the standard library doesn't address this, leaving each programmer to cobble configparser and argparse and os.environ all together in clunky ways.
这篇关于解析配置文件、环境和命令行参数,以获取单个选项集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:解析配置文件、环境和命令行参数,以获取单个选项集合
基础教程推荐
- 在同一图形上绘制Bokeh的烛台和音量条 2022-01-01
- 修改列表中的数据帧不起作用 2022-01-01
- PANDA VALUE_COUNTS包含GROUP BY之前的所有值 2022-01-01
- 包装空间模型 2022-01-01
- 使用大型矩阵时禁止 Pycharm 输出中的自动换行符 2022-01-01
- 无法导入 Pytorch [WinError 126] 找不到指定的模块 2022-01-01
- 求两个直方图的卷积 2022-01-01
- 在Python中从Azure BLOB存储中读取文件 2022-01-01
- PermissionError: pip 从 8.1.1 升级到 8.1.2 2022-01-01
- Plotly:如何设置绘图图形的样式,使其不显示缺失日期的间隙? 2022-01-01
