我正在为我的Python应用程序编写灵活的配置模块。我想允许在Python的命令行覆盖配置选项。为此,我正在使用here模块提供argparse
提供的想法。
不幸的是,我在某个特定用例中遇到了意外的异常。请参阅以下(非常simplified)代码 - 让我们称之为/tmp/test.py
:
import argparse
import configparser
# App's default settings
defaults = {
'verbose': 3,
'foo': 'bar'
}
# Parser for reading configuration file path (if provided)
first_parser = argparse.ArgumentParser(
description=__doc__,
formatter_class=argparse.RawDescriptionHelpFormatter,
add_help=False)
first_parser.add_argument('-c', '--conf', metavar='FILE', help='Config file')
args, remaining_argv = first_parser.parse_known_args()
if args.conf:
parser = configparser.ConfigParser()
parser.read(args.conf)
options = dict(parser.items('defaults'))
defaults.update(options)
# Parser for reading app's verbosity level and value for --foo option
second_parser = argparse.ArgumentParser(parents=[first_parser], add_help=False)
second_parser.add_argument('-v', '--verbose', action='count', default=0)
second_parser.add_argument('--foo', default='xyz')
second_parser.set_defaults(**defaults)
config = second_parser.parse_args(remaining_argv)
print(config)
问题:
me@mycomp:/tmp$ python3 /tmp/test.py
Namespace(conf=None, foo='bar', verbose=3)
me@mycomp:/tmp$ python3 /tmp/test.py -v
Namespace(conf=None, foo='bar', verbose=4)
me@mycomp:/tmp$ python3 /tmp/test.py --conf /tmp/config
Namespace(conf=None, foo="aaa", verbose='6')
me@mycomp:/tmp$ python3 /tmp/test.py -v --conf /tmp/config
Traceback (most recent call last):
File "/tmp/test.py", line 32, in <module>
config = second_parser.parse_args(remaining_argv)
File "/usr/lib/python3.5/argparse.py", line 1735, in parse_args
args, argv = self.parse_known_args(args, namespace)
File "/usr/lib/python3.5/argparse.py", line 1767, in parse_known_args
namespace, args = self._parse_known_args(args, namespace)
File "/usr/libPotwor/python3.5/argparse.py", line 1973, in _parse_known_args
start_index = consume_optional(start_index)
File "/usr/lib/python3.5/argparse.py", line 1913, in consume_optional
take_action(action, args, option_string)
File "/usr/lib/python3.5/argparse.py", line 1841, in take_action
action(self, namespace, argument_values, option_string)
File "/usr/lib/python3.5/argparse.py", line 1006, in __call__
new_count = _ensure_value(namespace, self.dest, 0) + 1
TypeError: Can't convert 'int' object to str implicitly
/tmp/config
内容:
me@mycomp:/tmp$ cat /tmp/config
[defaults]
verbose = 6
foo = aaa
是什么原因?我该如何解决?
答案 0 :(得分:0)
我认为 options = dict(parser.items(&#39;默认&#39;)) - &gt;选项可以是[(&#39; your_key&#39;,int_your_value)]。 您应该修改:{&#39; your_key&#39;:int}
文档:https://www.tutorialspoint.com/python/dictionary_items.htm