1

示例代码:

import argparse
import cmd2
import sys


def create_subparser(a_parser = None):
    if a_parser is None:
        new_parser = argparse.ArgumentParser()
    else:
        new_parser = a_parser.add_parser("single")
    new_parser.add_argument("--single-param", "-sp")
    return new_parser


def single_print(some_param):
    print("single operation " + str(some_param))


def other_print():
    print("other operation")


class TestCmd(cmd2.Cmd):
    single_parser = create_subparser()

    @cmd2.with_argparser(single_parser)
    def do_single(self, opts):
        print(opts)
        single_print(opts.single_param)

    def do_other(self, opts):
        other_print()


if __name__ == "__main__":
    parser = argparse.ArgumentParser()
    subparsers = parser.add_subparsers(help="commands", dest="mode")
    create_subparser(subparsers)
    cmd_parser = subparsers.add_parser("cmd", help="shell")

    args = parser.parse_args()
    if args.mode == "single":
        single_print(args.single_param)
    else:
        sys.argv = [sys.argv[0]]  # cmd2 complains about unknown parameters without
        cmd = TestCmd()
        cmd.cmdloop()

在示例中,我正在尝试制作一个可以在单模式program.py single -sp test(我尝试获取 cmd2 类中 do_single 中使用的 argparse 的所有值,我得到了。使用 cmd2 的调试,它会:program.py cmdsingle -sp testotherEXCEPTION of type 'RecursionError' occurred with message: 'maximum recursion depth exceeded in comparison'

Traceback (most recent call last):
File "/home/.../site-packages/cmd2/cmd2.py", line 2011, in onecmd_plus_hooks
    stop = self.onecmd(statement, add_to_history=add_to_history)
  File "/home/.../site-packages/cmd2/cmd2.py", line 2441, in onecmd
    stop = func(statement)
  File "/home/.../site-packages/cmd2/decorators.py", line 308, in cmd_wrapper
    return func(*args_list, **kwargs)
  File "test.py", line 28, in do_single
    print(opts)
  File "/usr/lib/python3.7/argparse.py", line 131, in __repr__
    arg_strings.append('%s=%r' % (name, value))
  File "/usr/lib/python3.7/argparse.py", line 131, in __repr__
    arg_strings.append('%s=%r' % (name, value))
  File "/usr/lib/python3.7/argparse.py", line 131, in __repr__
    arg_strings.append('%s=%r' % (name, value))
  [Previous line repeated 326 more times]
  File "/usr/lib/python3.7/argparse.py", line 129, in __repr__
    for name, value in self._get_kwargs():
  File "/usr/lib/python3.7/argparse.py", line 139, in _get_kwargs
    return sorted(self.__dict__.items())
RecursionError: maximum recursion depth exceeded in comparison

我使用该create_subparser函数的原因是因为我正在创建要由 cmd2.Cmd 子类使用的子解析器,但是我想在从主 shell 调用时对模式使用相同的解析器。这基本上是一个最小的工作示例,我的代码有更多的标志等等。

另一件事,问题似乎是因为print(opts). 似乎它试图这样做vars(opts),当它尝试枚举所有变量时,由于某处递归而耗尽内存。如果我不尝试列出对象内的所有变量(例如,如果我只使用opts.single_param),它会起作用。这似乎与我试图在 cmd2 和主程序中使用相同的 argparser 有关,但我不明白为什么会发生这种情况以及是否可以修复。

4

1 回答 1

0

没有cmd2我就无法运行你的代码。但是我可以创建一个显示您的问题的名称空间。

In [429]: ns = argparse.Namespace(foo='bar', baz=12)                                                 
In [430]: ns.ns = ns                                                                                 
In [431]: print(ns)                                                                                  
---------------------------------------------------------------------------
RecursionError                            Traceback (most recent call last)
<ipython-input-431-0769451eb86e> in <module>
----> 1 print(ns)

/usr/lib/python3.6/argparse.py in __repr__(self)
    133         for name, value in self._get_kwargs():
    134             if name.isidentifier():
--> 135                 arg_strings.append('%s=%r' % (name, value))
    136             else:
    137                 star_args[name] = value

... last 1 frames repeated, from the frame below ...

/usr/lib/python3.6/argparse.py in __repr__(self)
    133         for name, value in self._get_kwargs():
    134             if name.isidentifier():
--> 135                 arg_strings.append('%s=%r' % (name, value))
    136             else:
    137                 star_args[name] = value

RecursionError: maximum recursion depth exceeded while calling a Python object

您可以测试各个属性,直到找到问题所在:

In [432]: list(ns.__dict__.keys())                                                                   
Out[432]: ['foo', 'baz', 'ns']
于 2020-08-21T19:28:47.607 回答