将TOML文件和命令行参数转换为配置数据类
项目描述
xconf
- 命令配置的数据类和TOML
演示
在xconf_demo.py
中可以看到如何使用xconf.Command
、xconf.field
和xconf.config
的示例。运行它以查看其配置键。
$ python xconf_demo.py demo_command -h
demo_command: Demo command
usage: xconf_demo.py demo_command [-c CONFIG_FILE] [-h] [-v] [--dump-config] [vars ...]
positional arguments:
vars Config variables set with 'key.key.key=value' notation
optional arguments:
-c CONFIG_FILE, --config-file CONFIG_FILE
Path to config file (default: demo_command.conf)
-h, --help Print usage information
-v, --verbose Enable debug logging
--dump-config Dump final configuration state as TOML and exit
configuration keys:
collections
dict[str, ExtendedThingie]
collections.<str>.name
str
<ExtendedThingie>
collections.<str>.extended
bool
<ExtendedThingie>
either_one
[int, str]
should_bar
bool
(default: False)
should_foo
bool
Whether demo should foo
number_list
list[int]
List of favorite numbers
float_list
list[float]
List of favorite floating-point numbers
str_list
list[str]
List of favorite strings
sequence
list[ExtendedThingie]
(default: [])
sequence[#].name
str
<ExtendedThingie>
sequence[#].extended
bool
<ExtendedThingie>
默认配置文件
命令名称demo_command
是从类名生成的,并用于在当前目录中查找默认配置文件(demo_command.conf
)。
在命令行中提供参数
帮助输出中的任何配置键都可以以dotted.name=value
格式在命令行中提供。
对于原始类型列表(str
、int
、float
),您只需使用逗号分隔等于号右侧的值。例如:number_list=1,2,3
。
要覆盖列表中的单个条目,请使用some_name[#]
或dotted[#].name=value
,其中#
是一个整数索引。例如:number_list[0]=99
字符串值是裸的(即没有围绕value
的引号)。布尔值不区分大小写,为true
、t
或1表示真,为false
、f
或0表示假。
构建命令
请参阅xconf_demo.py
以获取示例。请注意,命令必须继承自xconf.Command
并应用@xconf.config
装饰器。选项由数据类的层次结构定义。(出于一些不重要的原因,它们不是严格意义上的import dataclass
数据类。)
许可证
除xconf/vendor/
之外的所有代码均根据MIT许可证提供,但xconf_demo.py
和demo_command.conf
除外,它们被发布到公有领域,以便您构建。
请注意,xconf/vendor/
下的代码根据那里列出的许可证使用。
项目详情
下载文件
下载适用于您平台文件。如果您不确定选择哪一个,请了解更多关于 安装包 的信息。
源分发
xconf-0.0.2.tar.gz (32.6 kB 查看散列)
构建分发
xconf-0.0.2-py3-none-any.whl (34.1 kB 查看散列)
关闭
xconf-0.0.2.tar.gz 的散列
算法 | 散列摘要 | |
---|---|---|
SHA256 | 591f9acc1690ba5b5915e5d49af00a9f5c0d200ce9b443bface0196827697d71 |
|
MD5 | bf4b4fe6bfc3413f3f3a55237fc3c2c4 |
|
BLAKE2b-256 | 6d670aa8ba63bbf7016187b9793c505286ca082bfc95dd63fd9d14c0c57beb2f |
关闭
xconf-0.0.2-py3-none-any.whl 的散列
算法 | 散列摘要 | |
---|---|---|
SHA256 | e11c8d61b1d1aea93bb32cfaaeb599bf08506f2bb31a60606bfed05e2cfd570a |
|
MD5 | cd19f7605280357e4b1034893691bb64 |
|
BLAKE2b-256 | 5d26be5d6394c3756a5383f2e76584d177a9e2506b655ccfeccca00fcf9c2b81 |