此包支持使用配置文件的click命令。
click-configfile的Python项目详细描述
click是一个框架,用于简化为 命令行工具。这个包扩展了click功能 通过添加对使用配置文件的命令的支持。
示例:
配置文件,如:
# -- FILE: foo.ini[foo]flag=yesname=Alice and Bobnumbers=1 4 9 16 25filenames=foo/xxx.txt bar/baz/zzz.txt[person.alice]name=Alicebirthyear=1995[person.bob]name=Bobbirthyear=2001
可使用:
# EXAMPLE:# -- FILE: example_command_with_configfile.py (ALL PARTS: simplified)fromclick_configfileimportConfigFileReader,Param,SectionSchemafromclick_configfileimportmatches_sectionimportclickclassConfigSectionSchema(object):"""Describes all config sections of this configuration file."""@matches_section("foo")classFoo(SectionSchema):name=Param(type=str)flag=Param(type=bool,default=True)numbers=Param(type=int,multiple=True)filenames=Param(type=click.Path(),multiple=True)@matches_section("person.*")# Matches multiple sectionsclassPerson(SectionSchema):name=Param(type=str)birthyear=Param(type=click.IntRange(1990,2100))classConfigFileProcessor(ConfigFileReader):config_files=["foo.ini","foo.cfg"]config_section_schemas=[ConfigSectionSchema.Foo,# PRIMARY SCHEMAConfigSectionSchema.Person,]# -- SIMPLIFIED STORAGE-SCHEMA:# section:person.* -> storage:person.*# section:person.alice -> storage:person.alice# section:person.bob -> storage:person.bob# -- ALTERNATIVES: Override ConfigFileReader methods:# * process_config_section(config_section, storage)# * get_storage_name_for(section_name)# * get_storage_for(section_name, storage)# -- COMMAND:CONTEXT_SETTINGS=dict(default_map=ConfigFileProcessor.read_config())@click.command(context_settings=CONTEXT_SETTINGS)@click.option("-n","--number","numbers",type=int,multiple=True)@click.pass_contextdefcommand_with_config(ctx,numbers):# -- ACCESS ADDITIONAL DATA FROM CONFIG FILES: Using ctx.default_mapforperson_data_keyinctx.default_map.keys():ifnotperson_data_key.startswith("person."):continueperson_data=ctx.default_map[person_data_key]process_person_data(person_data)# as dict.