I would like to create a function which can take either 1 or 2 arguments. Currently, I have a function which takes exactly 2 arguments through CMD:
def test(self,countName,optionalArg):
if countName == "lowest":
#something
if optionalArg == "furthest:
#something
else:
#something else
if __name__ == '__main__':
countName = sys.argv[1]
optionalArg = sys.argv[2]
temp = len(sys.argv)
for i in xrange(1,temp):
sys.argv.pop()
I would then run:
python filename.py lowest furthest
Using this means that passing the second arg is a must. If I try to run my script just by passing one arg, it encounters an error (as expected). My question is, how do you create an optional argument, which could either be passed or not, depending on the situation?
For example:
python filename.py lowest
In this situation, I expect the program to perform the "#something else" script, as nothing was passed and it is different than "furthest".
Please do not write the code for me, I am here to learn :)