Subparsers.add_parser TypeError: __init__() got an unexpected keyword argument 'prog'

Subparsers.add_parser TypeError: __init__() got an unexpected keyword argument 'prog'

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

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

下面是我在 class:

中的调用方式
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 我做的一切都是正确的,但我得到了这样的例外:

  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'。

问题可能出在 ArgParser() class 上。我不知道那是什么,尽管它可能是自定义的 argparse.ArgumentParser(如 subclass)。

add_parser 确保 kwargs 中有一个 prog,然后将其传递给 parser = self._parser_class(**kwargs)_parser_class 是主解析器的 class 除非在 add_subparsers 行中指定了替代项。

所以我猜测 ArgParser 不接受 prog 关键字,尽管 ArgumentParser 接受。但我认为如果不进一步了解其来源,我们无法为您提供帮助。

我遇到了同样的问题!

我创建了扩展 argparse.ArgumentParser 的 class ArgParser,在其中,我有 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)