我用argparse编写了一个如下所示的命令:
parser = argparse.ArgumentParser("MY_CMD")
group = parser.add_mutually_exclusive_group()
group.add_argument('-b', '--binary', default=None, type=bytes)
group.add_argument('-s', '--string', default=None, type=str)
在python 2.7中,它可以很好地工作,但是如果我尝试像这样运行它,现在可以迁移到Python 3.7:
parser.parse_args(['-b', b"hello"])
我收到以下错误:
File "C:\Python37\lib\argparse.py", line 1749, in parse_args
args, argv = self.parse_known_args(args, namespace)
File "C:\Python37\lib\argparse.py", line 1781, in parse_known_args
namespace, args = self._parse_known_args(args, namespace)
File "C:\Python37\lib\argparse.py", line 1822, in _parse_known_args
option_tuple = self._parse_optional(arg_string)
File "C:\Python37\lib\argparse.py", line 2108, in _parse_optional
if not arg_string[0] in self.prefix_chars:
TypeError: 'in <string>' requires string as left operand, not int
谁能指出我如何针对Python 3修复此问题? 预先感谢。