gpt4 book ai didi

python - Subparsers.add_parser 类型错误 : __init__() got an unexpected keyword argument 'prog'

转载 作者:行者123 更新时间:2023-12-03 23:17:28 31 4
gpt4 key购买 nike

我需要实现一个 ArgParse 构建器来生成解析器以及关于子解析器和参数。我创建了一个装饰器来将子解析器声明为 Action 。这是包含装饰器的类:

class Controller(object):
endpoints = None
parser = None
subparsers = None

def __init__(self, endpoint=None):
from src.app.commons.ArgParser import ArgParser
if not self.parser:
self.parser = ArgParser()
# Get all
self.endpoints = utilities.conf_wrapper()
self.mod = endpoint
if not self.subparsers:
self.subparsers = self.parser.add_subparsers(help=gettext('%s_SUBPARSER_HELP' % str(self.mod).upper()))
self.parser.add_argument_group(self.mod, gettext('%s_GROUP_DESC' % str(self.mod).upper()))


def endpoint(self, endpoint, **kwargs):
""" Create an endpoint to define a method on behalf of subparser"""
ref = self
def decorator(f):
"""
Create subparser for controller object
@param f as a function to call
@return decorator method
"""
# TODO Set given function f as a subparser destination
new_sub_parser = ref.subparsers.add_parser(endpoint, help=gettext('%s_%s_HELP' % (ref.mod, endpoint)))
[new_sub_parser.add_argument("--%s" % arg, action='store') for arg in ref.endpoints[ref.mod][endpoint]["params"]]
setattr(ConsoleInterface, 'do_%s' % endpoint, f)

return decorator

这是我在类里面如何称呼它的:
from src.app.controller import Controller

network = Controller("network")


@network.endpoint('create')
def create(*args):
try:
print "I am here"
except Exception as err:
print err

我在这里期待的是创建一些解析器作为这样的命令:
$~ network create [arguments]

根据 ArgParse docs我做的一切都是正确的,但我得到了这样的异常(exception):
  File "/projectpath/src/app/controller/__init__.py", line 48, in decorator
new_sub_parser = ref.subparsers.add_parser(endpoint, help=gettext('%s_%s_HELP' % (ref.mod, endpoint)))
File "/usr/lib/python2.7/argparse.py", line 1066, in add_parser
parser = self._parser_class(**kwargs)
TypeError: __init__() got an unexpected keyword argument 'prog'

当我看着 ArgParse._SubParsersAction.__init()__它涵盖了 kwargs 中的“prog”。

最佳答案

我有同样的问题!

我创建了我的类(class) ArgParser扩展 argparse.ArgumentParser和它的内幕我有 self.add_subparsers()调用,导致此问题。

class ArgParser(argparse.ArgumentParser):
"""A class to group all the arg parse stuff.
You dont need to pay attention here unless you want to edit CLI args spec"""

def __init__(self):
p_args = dict(formatter_class=argparse.ArgumentDefaultsHelpFormatter)
super().__init__(**p_args)
self.add_argument('-v', '--version', action='version', version=f'%(prog)s {__version__}')
sub_ps = self.add_subparsers(dest='cmd')
sub_ps.required = True
# ... and lot other stuff


所以,这会导致 TypeError: __init__() got an unexpected keyword argument 'prog'
怎么修?

明确设置 parser_class=argparse.ArgumentParser

sub_ps = self.add_subparsers(dest='cmd', parser_class=argparse.ArgumentParser)

关于python - Subparsers.add_parser 类型错误 : __init__() got an unexpected keyword argument 'prog' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47833828/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com