2012-08-23 131 views
0

所以我有一个函数来自GDATA API(gdata.sample_util.authorize_client(client,service = client.auth_service,source = client.source,scopes = client.auth_scopes)),它使用命令行来接收参数。我怎么能自动化,所以我可以硬编码参数?传递参数到命令行(PYTHON)

+1

你的问题是什么,你想从命令行传递参数吗?如果是这样,你的问题是什么?你想自动化什么?为什么和你想要硬编码 – schacki

回答

1

你通过硬编码参数的意思是,你不必参数写每次调用该函数,或者从命令行打开程序?这些被称为默认参数。检查了这一点:

http://docs.python.org/release/1.5.1p1/tut/defaultArgs.html

例子:

def ask_ok(prompt, retries=4, complaint='Yes or no, please!'): 
    while 1: 
     ok = raw_input(prompt) 
     if ok in ('y', 'ye', 'yes'): return 1 
     if ok in ('n', 'no', 'nop', 'nope'): return 0 
     retries = retries - 1 
     if retries < 0: raise IOError, 'refusenik user' 
     print complaint 

所以,实际上你可以调用不同的方式这个功能:

ask_ok('Do you really want to quit?')

或像这样:

ask_ok('OK to overwrite the file?', 2) 

祝你好运!

0

如果没有命令行参数传递,你可以添加参数,如你所愿

import sys 

sys.argv += ["-a"]