I finished writing a script in python, and now stuck in the interface, which requires getting few options from user but not sure what is the best way to get optional arguments.. The code for that is as below...
def getOptions(argv):
try:
opts,args = getopt.getopt(argv, "hi:c:d:m", ["ifile=", "add=", "delete"])
except getopt.GetoptError:
printUsage()
sys.exit(2)
for opt, arg in opts:
if opt in ("-h", "--help"):
print ("test -m <make> [src] [dst]\n")
print ("test -i <install>[filename] \n")
.....
sys.exit()
if opt in ( "-m", "--make"):
make(arg)
sys.exit()
if opt in ("-i","--install"):
install(arg)
sys.exit()
... # few more options
else:
assert False, "unhandled option"
My question is that how can i leave out the argument (like use a default optional path for arg) and if that is not provided, get from user ??
Currently i've to provide
./test -i
how can i leave out file name and call like
./test -i
I'm stuck with same issue one more time, My development environment is python 2.6 and because of the limitations, now using optparse but with same issue, i've to support an optional argument and couldn't find way how to do that other than parsing sys.argv manually, i've almost 10 different options and if i can handle one optional argument, my script would be much more convenient for end users.