ap = argparse.ArgumentParser()
ap.add_argument("-i", "--image", help="Path to input image", required=True)
ap.add_argument("-p", "--pivot-point", help="Pivot point coordinates x, y separated by comma (,)", required=True)
ap.add_argument("-s", "--scale", help="Scale to zoom", type = int, required=True)
args = vars(ap.parse_args(['image.jpeg', '(144,72)', 3]))
您好,
我试图在参数中传递一个图像来旋转它并缩放它以通过argparse进行缩放。
但我收到以下错误:
请让我知道你做错了什么。
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-131-c7157827dce9> in <module>()
3 ap.add_argument("-p", "--pivot-point", help="Pivot point coordinates x, y separated by comma (,)", required=True)
4 ap.add_argument("-s", "--scale", help="Scale to zoom", type = int, required=True)
----> 5 args = vars(ap.parse_args(['image.jpeg', '(144,72)', 3]))
~/anaconda3/lib/python3.6/argparse.py in parse_args(self, args, namespace)
1728 # =====================================
1729 def parse_args(self, args=None, namespace=None):
-> 1730 args, argv = self.parse_known_args(args, namespace)
1731 if argv:
1732 msg = _('unrecognized arguments: %s')
~/anaconda3/lib/python3.6/argparse.py in parse_known_args(self, args, namespace)
1760 # parse the arguments and exit if there are any errors
1761 try:
-> 1762 namespace, args = self._parse_known_args(args, namespace)
1763 if hasattr(namespace, _UNRECOGNIZED_ARGS_ATTR):
1764 args.extend(getattr(namespace, _UNRECOGNIZED_ARGS_ATTR))
~/anaconda3/lib/python3.6/argparse.py in _parse_known_args(self, arg_strings, namespace)
1801 # and note the index if it was an option
1802 else:
-> 1803 option_tuple = self._parse_optional(arg_string)
1804 if option_tuple is None:
1805 pattern = 'A'
~/anaconda3/lib/python3.6/argparse.py in _parse_optional(self, arg_string)
2087
2088 # if it doesn't start with a prefix, it was meant to be positional
-> 2089 if not arg_string[0] in self.prefix_chars:
2090 return None
2091
TypeError: 'int' object is not subscriptable
答案 0 :(得分:1)
解析器通常处理sys.argv[1:]
,它是shell生成的字符串列表。使用您自己的列表进行测试也需要字符串。请注意使用&#39; 3&#39;而不是3
。
In [182]: ap.parse_args(['image.jpeg', '(144,72)', '3'])
usage: ipython3 [-h] -i IMAGE -p PIVOT_POINT -s SCALE
ipython3: error: the following arguments are required: -i/--image, -p/--pivot-point, -s/--scale
An exception has occurred, use %tb to see the full traceback.
SystemExit: 2
现在它提供标准化的argparse
错误,其中包含使用情况和有用的诊断信息。
如果我们提供您告诉它的短期权标志:
In [183]: ap.parse_args(['-i','image.jpeg', '-p','(144,72)', '-s' '3'])
Out[183]: Namespace(image='image.jpeg', pivot_point='(144,72)', scale=3)
In [184]: vars(_)
Out[184]: {'image': 'image.jpeg', 'pivot_point': '(144,72)', 'scale': 3}
或者我们可以使用split
更清楚地了解正确的命令行的外观:
In [186]: ap.parse_args("-i image.jpeg -p (144,72) -s 3".split())
Out[186]: Namespace(image='image.jpeg', pivot_point='(144,72)', scale=3)
由于您已将-s
定义为type=int
,因此它会转换字符串&#39; 3&#39;整数。
对于-p
,您可以尝试nargs=2
,允许您使用&#39; -p 144 72&#39;。
使用
ap.add_argument("-p", "--pivot-point", nargs=2, metavar=('x','y'),
type=int, help="Pivot point coordinates")
我明白了:
In [196]: ap.print_help()
usage: ipython3 [-h] -i IMAGE -p x y -s SCALE
optional arguments:
-h, --help show this help message and exit
-i IMAGE, --image IMAGE
Path to input image
-p x y, --pivot-point x y
Pivot point coordinates
-s SCALE, --scale SCALE
Scale to zoom
In [197]: ap.parse_args("-i image.jpeg -p 144 72 -s 3".split())
Out[197]: Namespace(image='image.jpeg', pivot_point=[144, 72], scale=3)
答案 1 :(得分:0)
替换
args = vars(ap.parse_args(['image.jpeg', '(144,72)', 3]))
使用
args = vars(ap.parse_args(['image.jpeg', '(144,72)', '3'])) #string '3'. 'type = int' will convert it to an int object
答案 2 :(得分:0)
传递给parse_args的列表中的所有元素都需要是字符串,由参数解析器决定将字符串解释为您设置的类型。