1.python内置方式
import sys
print "script is ", sys.argv[0]
for i in range(1, len(sys.argv)):
print "parameter is ", i, sys.argv[i]
运行结果为:
[machine@localhost zj]$ python parametertest.py nihao ma
script is parametertest.py
parameter is 1 nihao
parameter is 2 ma
2.argparse模块
注意,需要安装argparse模块,conda install argparse。
>>> import argparse
>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--num', type=int, default=10)
_StoreAction(option_strings=['--num'], dest='num', nargs=None, const=None, default=10, type=<type 'int'>, choices=None, help=None, metavar=None)
项目中使用如下:
import argparse
def parse_args():
parser = argparse.ArgumentParser()
parser.add_argument('--name', default='oys')
parser.add_argument('--sex', type=int, default=1)
args = parser.parse_args();
return args
if __name__ == '__main__':
args = parse_args()
print 'name is : %s .' % args.name
print 'sex is : %s .' % args.sex
执行效果如下:
(tensorflow2)[machine@localhost zj]$ python parametertest.py
name is : oys .
sex is : 1 .
(tensorflow2)[machine@localhost zj]$ python parametertest.py --name zj --sex 0
name is : zj .
sex is : 0 .
3.tensorflow FLAGS模块
import tensorflow as tf
tf.app.flags.DEFINE_string('nihaoma', 'question',"a question")
tf.app.flags.DEFINE_integer('oys', 1,"1 biaoshi hao")
FLAGS = tf.app.flags.FLAGS
def main(_):
print(FLAGS.nihaoma)
print(FLAGS.oys)
if __name__ == '__main__':
tf.app.run()
执行效果如下:
(tensorflow2)[machine@localhost zj]$ python parametertest.py
question
1
(tensorflow2)[machine@localhost zj]$ python parametertest.py --nihaoma enen --oys 1
enen
1
写这篇博文是自己在阅读GAN代码中没有发现参数解析过程,觉得奇怪就找了相关人员询问和查询,得知了一些内容,觉得有必要总结一下。欢迎指正!
参考文献:
1.https://www.cnblogs.com/saiwa/articles/5253713.html
2.https://www.2cto.com/kf/201412/363654.html
3.http://blog.csdn.net/leiting_imecas/article/details/72367937