Python3中ConfigArgParse模块的使用

      ConfigArgParse是一个可以替换argparse的插件,可通过"pip install configargparse"直接安装,允许通过配置文件或环境变量设置选项.最新版本为1.5.3,源码地址为:https://github.com/bw2/ConfigArgParse,  License为MIT.
      Python中的命令行解析模块如argparse对配置文件和环境变量的支持非常有限,因此ConfigArgParse模块扩展了argparse来添加这些特性.
      Features:
      (1).command-line, config file, env var,和默认设置现在可以使用单个API一次性定义、记录和解析.如果以多种方式指定值,则: command line > environment variables > config file values > defaults;
      (2).配置文件可以具有.ini或.yaml样式语法(例如:key=value or key: value);
      (3).用户可以通过普通(normal-looking)的命令行参数(例如:-c path/to/config.txt)而不是argparse-style @config.txt提供配置文件;
      (4).可以指定一个或多个默认配置文件路径(例如:['/etc/bla.conf', '~/.my_config']);
      (5).完全支持所有argparse功能;
      (6).env vars and config file keys & syntax会自动记录在-h帮助信息中;
      (7).新方法print_values()可以报告keys & values以及它们的设置位置(例如:command line, env var, config file, or default);
      (8).轻量级(除了(可选)PyYAML之外,没有第三方库依赖项);
      (9).可扩展(ConfigFileParser可以子类化(subclassed)以定义新的配置文件格式).

     示例代码如下所示:

import configargparse # pip install configargparse

def config_file_parse():
    #parser = configargparse.ArgumentParser(default_config_files=["test.conf"])
    parser = configargparse.ArgumentParser() # Aliases: ArgParser
    parser.add_argument("-c", "--config", required=True, is_config_file=True, help="config file path") # Aliases: add, add_arg
    parser.add_argument("--csdn_addr", type=str, default="csdn", help="csdn addr")
    parser.add_argument("--github_addr", type=str, default="github", help="github addr") # Aliases: add, add_arg
    parser.add_argument("--beijing", type=bool, default=False, help="work addr1")
    parser.add_argument("--jinan", type=bool, default=True, help="work addr2")
    parser.add_argument("--tianjin", type=bool, default=True, help="work addr3")
    parser.add_argument("--age", type=int, default=2, help="age")
    parser.add_argument("--height", type=float, default=1.7, help="height")
    parser.add_argument("--package", type=str, help="package pos")

    options = parser.parse_args() # Aliases: parse

    #print("======================================")
    #print(parser.format_help())
    #print("======================================")
    print(parser.format_values())
    #print("======================================")

    print("csdn addr:", options.csdn_addr)
    print("github addr:", options.github_addr)

    if options.beijing:
        print("Your work address: BeiJing")

    if options.age > 18:
        print("You are an adult, age:", options.age)

    if options.height:
        print("Your height:", options.height)

    if options.package == "./test_package":
        print("package pos:", options.package)

if __name__=='__main__':
    config_file_parse()
    print("test finish")

      test.conf内容如下:

csdn_addr = https://blog.csdn.net/fengbingchun
github_addr = https://github.com/fengbingchun

beijing = True
tianjin = False

age = 28
height = 1.65

package = ./test_package

      执行结果如下所示:

      GitHub: https://github.com/fengbingchun/Python_Test

猜你喜欢

转载自blog.csdn.net/fengbingchun/article/details/129333101