I’m new to Python and what I have learnt from getopt is that I can pass command line arguments while executing the python script. Now my question is, is there a way to not set the number of arguments that I can pass in the command line?
My code looks something like this:
try:
opts, args = getopt.getopt(sys.argv[1:],"h:1:2:3:4:",["help=", "1week=","2week=", "3week=", "4week="])
except getopt.GetoptError:
print 'test.py -1 <week 1> -2 <week 2> -3 <week 3> -4 <week 4>'
sys.exit(2)
for opt, arg in opts:
if opt == '-h':
print 'test.py -1 <week 1> -2 <week 2> -3 <week 3> -4 <week 4>'
sys.exit()
elif opt in ("-1", "--1week"):
week1 = arg
elif opt in ("-2", "--2week"):
week2 = arg
elif opt in ("-3", "--3week"):
week3 = arg
elif opt in ("-4", "--4week"):
week4 = arg
And to run the above code I use
python test.py -1 89 -2 88 -3 87 -4 86. Is there anyway I can modify this where in I don’t have to always give 4 arguments, but also 3? I want it to be flexible where in sometimes I might give 3 arguments or 4.
If your program accepts one or more week numbers:
Example
Output
To produce
'weekly_1_2_3'from the aboveargs.weeks:The result is
'weekly_1_2_3_4'for1 2 3 4command line.