使用 Python 和 argparse 库我可以捕获脚本的选项
parser.add_argument('-s','--status', action='store_true')
python script.py -s
如何在没有前导字符 -
的情况下传递选项?
python script.py status
最佳答案
设置nargs='?'
对于论点:
import argparse
parser = argparse.ArgumentParser()
parser.add_argument('status', nargs='?')
args = parser.parse_args()
print args.status
这是命令行上的内容:
$ python test.py --help
usage: test.py [-h] [status]
positional arguments:
status
optional arguments:
-h, --help show this help message and exit
$ python test.py my_status
my_status
关于python-2.7 - Python argparse 不带字符 -,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24835946/