2015-11-04 34 views
1

我试图使用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'但​​模块不被触发。

+2

哦,对于......你实际上从来没有'parse_args'! – jonrsharpe

回答

3

您需要实际调用它!

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