2015-02-11 75 views
0

例如,我有一个函数,它从命令行获取参数--config。 所以从控制台启动它,我必须输入以下内容:为文件中的函数提供命令行参数

>>> my_function --config 

我要创建文件中像new_func.py,从这里发射my_function --config

我该怎么做?

+0

你有一个更具体的例子吗? 否则,它听起来像你需要导入SYS,然后使用sys.argv将参数存储在变量中。一个例子会很好:) – Onedot618 2015-02-11 12:17:02

回答

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