Subparsers.add_parser TypeError: __init__() 有一个意外的关键字参数“prog”

Fat*_*ana 5 python argparse python-decorators

我需要实现一个 ArgParse 构建器来生成解析器并考虑子解析器和参数。我创建了一个装饰器来将子解析器声明为操作。这是包含装饰器的类:

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
Run Code Online (Sandbox Code Playgroud)

这是我在课堂上的称呼:

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
Run Code Online (Sandbox Code Playgroud)

我在这里期望的是创建一些解析器作为命令,如下所示:

$~ network create [arguments]
Run Code Online (Sandbox Code Playgroud)

根据 ArgParse文档,我做的一切都是正确的,但我得到了如下例外:

  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'
Run Code Online (Sandbox Code Playgroud)

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

Tha*_*wda 6

我有同样的问题!

我创建了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

Run Code Online (Sandbox Code Playgroud)

所以,这导致TypeError: __init__() got an unexpected keyword argument 'prog'

怎么修?

明确设置parser_class=argparse.ArgumentParser

sub_ps = self.add_subparsers(dest='cmd', parser_class=argparse.ArgumentParser)
Run Code Online (Sandbox Code Playgroud)


hpa*_*ulj 1

问题可能出在ArgParser()班级上。我不知道那是什么,尽管它可能是定制的argparse.ArgumentParser(作为子类)。

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

所以我猜它ArgParser不接受prog关键字,尽管ArgumentParser接受。但我认为,如果不了解更多关于其来源的信息,我们就无法为您提供帮助。