Skip to main content
 首页 » 编程设计

Python 参数解析

2024年10月24日17leader

我想使用 Python ArgParse 仅接受来自用户的某些输入。

所以在下面的示例中,假设我只想接受“type1/type2/type3”作为参数。那可能吗?

parser.add_argument('-t', '--type', type = str, help = 'type1/type2/type3') 

请您参考如下方法:

使用 choices argument将输入限制为一组有限的选择:

parser.add_argument('-t', '--type', choices=('type1', 'type2', 'type3'),  
                    help='type1/type2/type3')