简体   繁体   中英

Passing in arguments to command line (PYTHON)

So I have a function from GDATA API (gdata.sample_util.authorize_client(client, service=client.auth_service, source=client.source, scopes=client.auth_scopes)) which uses command line to receive arguments. How can I automate that so I can hard-code arguments?

Do you mean by hard-code arguments, arguments that you do not have to write everytime you call the function, or open the program from the command line? These are called Default Arguments. Check this out:

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

Example:

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

So you can actually call this function in different ways:

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

or like this:

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

Good luck!

if there were no command line arguments passed, you can just add parameters as you wish

import sys

sys.argv += ["-a"]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM