What are Command-line arguments?

Recall from Lesson 9, Example: Is it a Leap Year, Hands-on #10 and our modular program, yearquery.py, (which you can see carried over from Lesson 9 below for reference):

File #2 - The Module Caller File: yearquery.py 

# file: yearquery.py
"""Ask for years and say if they're leap years"""
import yearprop

while True:  # keep processing new input
   # keep asking for input until we get an integer
   while True: 
      try:
         year = int(input("Year (0 to stop)?"))
         break # if the year is 0, then the while loop stops
      except:
         print("That is not an integer.") 
   # process input

   if yearprop.is_leap_year(year):
      print(year, "is a leap year!")
   else:
      print(year, "is not a leap year.")
   if year==0: break  # stop processing input
print("Done")

The terminal command line execution of the Python scripts looked like the following:

$ python yearquery.py

Recall that we had to manually type in the years, and it would tell us whether each year was a leap year or not. Now, it's time to automate that process, meaning that we should be able to just give some years directly when we run the script, like this:

$ python yearquery.py 1972 2000 2017

This approach is more obviously what we want. But this won't just work out of the box, because the current yearquery.py script we have is not set up to handle command line arguments, which is because the query (caller) script is not currently set up to expect them. As it stands, it still prompts the user for input interactively.

We’ll need to modify yearquery.py to handle command line arguments properly.

You have completed 0% of the lesson
0%