argparse没有得到触发
问题描述:
我试图使用Python检查模块的参数参数:argparse没有得到触发
import argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Example with non-optional arguments')
parser.add_argument('count', action="store", type=int)
parser.add_argument('units', action="store")
print 'test'
当我运行该脚本(python test.py some inches
),它只是打印输出'test'
但模块不被触发。
答
你必须解析参数才能工作。您需要致电parser.parse_args()
或parser.parse_known_args()
。更多信息可以在这里找到:
https://docs.python.org/3/library/argparse.html#argparse.ArgumentParser.parse_args
答
您需要实际调用它!
import argparse
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Example with non-optional arguments')
parser.add_argument('count', action="store", type=int)
parser.add_argument('units', action="store")
args = parser.parse_args()
print args
哦,对于......你实际上从来没有'parse_args'! – jonrsharpe