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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With