2015-02-11 77 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