解析配置文件,环境和命令行参数,以获取单个选项集合 [英] 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:
-
具有级联的选项值:
Has a cascade of option values:
- 默认选项值,由
- 配置文件选项覆盖,由
-
- 环境变量,由
- 命令行选项覆盖。
允许在命令行上指定的一个或多个配置文件位置 - 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.
这篇关于解析配置文件,环境和命令行参数,以获取单个选项集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!