为文件中的函数提供命令行参数

时间:2015-02-11 12:13:55

标签: python

例如,我有从命令行获取--config参数的函数。 所以要从控制台启动它,我必须输入以下内容:

>>> my_function --config

我想创建new_func.py之类的文件,然后从此处启动my_function --config

我该怎么做?

1 个答案:

答案 0 :(得分:0)

使用argparse模块:

import argparse

parser = argparse.ArgumentParser(description='Command line exemple.')
parser.add_argument('--config', dest='fileconf', action='store',
                   help='my argument description)')

args = parser.parse_args()
print args.fileconf # do something with fileconf value

enter image description here