使用 Python 的 argparse 接受 0-5 形式的数字范围?
使用 argparse,有没有办法接受一系列数字并将它们转换为列表?
例如:
python example.py --range 0-5
是否有某种方法以该形式输入命令行参数并最终得到:
args.range = [0,1,2,3,4,5]
并且还可以输入 --range 2 = [2]
?
Using argparse, is there a way to accept a range of numbers and convert them into a list?
For example:
python example.py --range 0-5
Is there some way input a command line argument in that form and end up with:
args.range = [0,1,2,3,4,5]
And also have the possibility to input --range 2 = [2]
?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以在
type
参数中编写自己的解析器,例如You could just write your own parser in the
type
argument, e.g.您可以只使用字符串参数,然后使用
range(*rangeStr.split(','))
解析它。You can just use a string argument and then parse it with
range(*rangeStr.split(','))
.