Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

kwargs in python executables

I'm trying to create a program that can be called from the command line and use keyword arguments in python 2.6. So far I've tried:

#!/usr/bin/python
def read(foo = 5):
    print foo
    return 0
if __name__ == '__main__'
    read()

When I try to run this from the command line: ./test.py the program prints 5 as expected. Is there a way to use ./test.py foo=6? I want to preserve the keyword arguments.

It seems like a simple question, but I haven't found a good source for this.

like image 576
greatscott Avatar asked Oct 24 '12 17:10

greatscott


1 Answers

python has built in library to help you achieve passing command line arguments to a script argparse. THe usage is a little different then what you are describing in your question though...

On a basic level you can access all command line arguments by sys.argv, which will be a list of arguments

Sorry should have mentioned the python 2.6 library is called optparse

like image 109
dm03514 Avatar answered Oct 09 '22 05:10

dm03514