"""
The Python standard library's 'calendar' module allows you to
render a calendar to your terminal.
https://docs.python.org/3.6/library/calendar.html

Write a program that accepts user input of the form
  `14_cal.py [month] [year]`
and does the following:
 - If the user doesn't specify any input, your program should
   print the calendar for the current month. The 'datetime'
   module may be helpful for this.
 - If the user specifies one argument, assume they passed in a
   month and render the calendar for that month of the current year.
 - If the user specifies two arguments, assume they passed in
   both the month and the year. Render the calendar for that
   month and year.
 - Otherwise, print a usage statement to the terminal indicating
   the format that your program expects arguments to be given.
   Then exit the program.

Note: the user should provide argument input (in the initial call to run the file) and not 
prompted input. Also, the brackets around year are to denote that the argument is
optional, as this is a common convention in documentation.

This would mean that from the command line you would call `python3 14_cal.py 4 2015` to 
print out a calendar for April in 2015, but if you omit either the year or both values, 
it should use today’s date to get the month and year.
"""

import sys
import calendar
from datetime import datetime, date

# This is the thing that allows users to put inputs when they run the file 
# print(type(sys.argv))

# The first arument is the current directory and the second 
# onwards is where you start getting arguments. They are always strings

# for i in sys.argv:
#   print(type(i),i)


# THIS PROGRAM ASSUMES THAT YOU WILL INPUT NUMBERS 
# NOT WORDS WHEN SELECTING A CALENDAR

# Access to current day  and date in parsed form:
today = date.today()
this_year = today.year
this_month = today.month

# Access to user specified values cast to int
try:
  user_month = int(sys.argv[1])
  user_year = int(sys.argv[2])
except:
  pass

# Initialize calendar object
cal = calendar.TextCalendar(calendar.SUNDAY)

# Determine which calendar to display
# No input:
if len(sys.argv) < 2:
  output = cal.formatmonth(this_year,this_month)
# Only 1 input (assume month of current year)
elif len(sys.argv) <3:
  output = cal.formatmonth(this_year,user_month)
# Both inputs:
elif len(sys.argv) == 3:
  output = cal.formatmonth(user_year,user_month)

# Display output
print(output)
  
