Search code examples
pythongdata

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?


Solution

  • 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!