解析配置文件、环境变量和命令行参数,以获得单一的选项集合

140 投票
12 回答
41803 浏览
提问于 2025-04-16 18:22

Python的标准库里有一些模块可以帮助我们处理不同的配置方式,比如解析配置文件configparser)、读取环境变量os.environ)和解析命令行参数argparse)。我想写一个程序,能够同时做到这些,并且还要:

  • 有一个选项值的级联

    • 默认的选项值,可以被
    • 配置文件中的选项覆盖,再被
    • 环境变量覆盖,最后被
    • 命令行选项覆盖。
  • 允许在命令行中指定一个或多个配置文件的位置,比如使用 --config-file foo.conf,并读取这个文件(可以替代或者额外读取通常的配置文件)。这仍然要遵循上面的级联规则。

  • 允许在一个地方定义选项,以决定如何解析配置文件和命令行。

  • 把解析后的选项统一成一个单一的选项值集合,让程序的其他部分可以访问,而不需要关心这些值是从哪里来的。

看起来我需要的功能都在Python的标准库里,但它们之间的配合并不是很顺畅。

我该如何在尽量不偏离Python标准库的情况下实现这些功能呢?

12 个回答

36

argparse模块让这件事变得简单,只要你能接受一个看起来像命令行的配置文件。(我觉得这是个优点,因为用户只需要学习一种语法。)将 fromfile_prefix_chars 设置为,比如说 @,这样就可以让

my_prog --foo=bar

等同于

my_prog @baz.conf

如果 @baz.conf 是,

--foo
bar

你甚至可以通过修改 argv 来让你的代码自动查找 foo.conf

if os.path.exists('foo.conf'):
    argv = ['@foo.conf'] + argv
args = argparser.parse_args(argv)

这些配置文件的格式可以通过创建ArgumentParser的子类,并添加一个 convert_arg_line_to_args 方法来修改。

49

更新: 我终于把这个放到了pypi上。你可以通过以下方式安装最新版本:

   pip install configargparser

完整的帮助和说明在这里

原始帖子

这是我自己拼凑出来的小东西。欢迎在评论中提出改进建议或报告bug:

import argparse
import ConfigParser
import os

def _identity(x):
    return x

_SENTINEL = object()


class AddConfigFile(argparse.Action):
    def __call__(self,parser,namespace,values,option_string=None):
        # I can never remember if `values` is a list all the time or if it
        # can be a scalar string; this takes care of both.
        if isinstance(values,basestring):
            parser.config_files.append(values)
        else:
            parser.config_files.extend(values)


class ArgumentConfigEnvParser(argparse.ArgumentParser):
    def __init__(self,*args,**kwargs):
        """
        Added 2 new keyword arguments to the ArgumentParser constructor:

           config --> List of filenames to parse for config goodness
           default_section --> name of the default section in the config file
        """
        self.config_files = kwargs.pop('config',[])  #Must be a list
        self.default_section = kwargs.pop('default_section','MAIN')
        self._action_defaults = {}
        argparse.ArgumentParser.__init__(self,*args,**kwargs)


    def add_argument(self,*args,**kwargs):
        """
        Works like `ArgumentParser.add_argument`, except that we've added an action:

           config: add a config file to the parser

        This also adds the ability to specify which section of the config file to pull the 
        data from, via the `section` keyword.  This relies on the (undocumented) fact that
        `ArgumentParser.add_argument` actually returns the `Action` object that it creates.
        We need this to reliably get `dest` (although we could probably write a simple
        function to do this for us).
        """

        if 'action' in kwargs and kwargs['action'] == 'config':
            kwargs['action'] = AddConfigFile
            kwargs['default'] = argparse.SUPPRESS

        # argparse won't know what to do with the section, so 
        # we'll pop it out and add it back in later.
        #
        # We also have to prevent argparse from doing any type conversion,
        # which is done explicitly in parse_known_args.  
        #
        # This way, we can reliably check whether argparse has replaced the default.
        #
        section = kwargs.pop('section', self.default_section)
        type = kwargs.pop('type', _identity)
        default = kwargs.pop('default', _SENTINEL)

        if default is not argparse.SUPPRESS:
            kwargs.update(default=_SENTINEL)
        else:  
            kwargs.update(default=argparse.SUPPRESS)

        action = argparse.ArgumentParser.add_argument(self,*args,**kwargs)
        kwargs.update(section=section, type=type, default=default)
        self._action_defaults[action.dest] = (args,kwargs)
        return action

    def parse_known_args(self,args=None, namespace=None):
        # `parse_args` calls `parse_known_args`, so we should be okay with this...
        ns, argv = argparse.ArgumentParser.parse_known_args(self, args=args, namespace=namespace)
        config_parser = ConfigParser.SafeConfigParser()
        config_files = [os.path.expanduser(os.path.expandvars(x)) for x in self.config_files]
        config_parser.read(config_files)

        for dest,(args,init_dict) in self._action_defaults.items():
            type_converter = init_dict['type']
            default = init_dict['default']
            obj = default

            if getattr(ns,dest,_SENTINEL) is not _SENTINEL: # found on command line
                obj = getattr(ns,dest)
            else: # not found on commandline
                try:  # get from config file
                    obj = config_parser.get(init_dict['section'],dest)
                except (ConfigParser.NoSectionError, ConfigParser.NoOptionError): # Nope, not in config file
                    try: # get from environment
                        obj = os.environ[dest.upper()]
                    except KeyError:
                        pass

            if obj is _SENTINEL:
                setattr(ns,dest,None)
            elif obj is argparse.SUPPRESS:
                pass
            else:
                setattr(ns,dest,type_converter(obj))

        return ns, argv


if __name__ == '__main__':
    fake_config = """
[MAIN]
foo:bar
bar:1
"""
    with open('_config.file','w') as fout:
        fout.write(fake_config)

    parser = ArgumentConfigEnvParser()
    parser.add_argument('--config-file', action='config', help="location of config file")
    parser.add_argument('--foo', type=str, action='store', default="grape", help="don't know what foo does ...")
    parser.add_argument('--bar', type=int, default=7, action='store', help="This is an integer (I hope)")
    parser.add_argument('--baz', type=float, action='store', help="This is an float(I hope)")
    parser.add_argument('--qux', type=int, default='6', action='store', help="this is another int")
    ns = parser.parse_args([])

    parser_defaults = {'foo':"grape",'bar':7,'baz':None,'qux':6}
    config_defaults = {'foo':'bar','bar':1}
    env_defaults = {"baz":3.14159}

    # This should be the defaults we gave the parser
    print ns
    assert ns.__dict__ == parser_defaults

    # This should be the defaults we gave the parser + config defaults
    d = parser_defaults.copy()
    d.update(config_defaults)
    ns = parser.parse_args(['--config-file','_config.file'])
    print ns
    assert ns.__dict__ == d

    os.environ['BAZ'] = "3.14159"

    # This should be the parser defaults + config defaults + env_defaults
    d = parser_defaults.copy()
    d.update(config_defaults)
    d.update(env_defaults)
    ns = parser.parse_args(['--config-file','_config.file'])
    print ns
    assert ns.__dict__ == d

    # This should be the parser defaults + config defaults + env_defaults + commandline
    commandline = {'foo':'3','qux':4} 
    d = parser_defaults.copy()
    d.update(config_defaults)
    d.update(env_defaults)
    d.update(commandline)
    ns = parser.parse_args(['--config-file','_config.file','--foo=3','--qux=4'])
    print ns
    assert ns.__dict__ == d

    os.remove('_config.file')

待办事项

这个实现还不完整。以下是部分待办事项列表:

符合文档行为

  • (简单) 写一个函数,从 add_argument 中的 args 找出 dest,而不是依赖于 Action 对象
  • (微不足道) 写一个 parse_args 函数,使用 parse_known_args。比如,可以把 cpython 实现中的 parse_args 复制过来,以确保它调用 parse_known_args

更难的事情……

我还没有尝试这些。虽然不太可能,但还是有可能它能正常工作……

  • (难?) 互斥
  • (难?) 参数组(如果实现了,这些组应该在配置文件中有一个 section。)
  • (难?) 子命令(子命令也应该在配置文件中有一个 section。)
7

看起来标准库并没有解决这个问题,所以每个程序员都得把 configparserargparseos.environ 这些东西拼凑在一起,方法还挺笨拙的。

撰写回答