#!/usr/bin/python
# Author: Theodore Zacharia
# V0.1: 03/08/2021 - Initial Release
#
# This script provides a template to start all other scripts with
# supports arguments being passed
#
# The canonical solution in the standard library is argparse
# but there is also a getopt solution which keeps it the same as a shell script
# 
#
import sys
import os, getopt
import time
from datetime import datetime

# ****************************************************
# Globals
trace = 0

# ****************************************************
# Functions

def usage():
	print ('[-h] [-t|-T] [-a argvalue] [-b myarg1value|--myarg1 myarg1value] [-c myarg2value|--myarg2 myarg2value] positional_params')
	print ("where:")
	print (" -h displays this help message")
	print (" -t is trace mode, -T is more trace info")
	print (" -a is an arg value")
	print ("Examples:")
	print ("python ", sys.argv[0] , "-t -a avalue")

	#print (msg)

def getargs():
	global trace

	try:
		opts, args = getopt.getopt(sys.argv[1:],"hTtHa:b:c:",["myarg1=","myarg2="])
	except getopt.GetoptError as err:
		print (err)
		sys.exit(2)
	for opt, arg in opts:
		print ("checking opt=", opt)
		if opt == '-h':
			usage()
			sys.exit()
		if opt == '-t':
			trace=1  # set trace level
		elif opt == '-T':
			trace=2  # set trace level higher
		elif opt == '-a':
			print ("1")
			print ("arg -a is ", "sss", arg)
			# dict_infile = arg.replace('"', 'Z')
		elif opt in ("-b", "--myarg1"):
			print ("2")
			print ("arg -b or --myarg1 is ", arg)
		elif opt in ("-c", "--myarg1"):
			print ("3")
			print ("arg -c or --myarg2 is ", arg)


	print ("args=", args)


# ****************************************************
# Mainline


getargs()

if len(sys.argv) > 1:
	if trace > 0:
		if trace > 1:
			current_time = datetime.now()
		else:
			current_time = time.ctime()
		print ("Starting at", current_time)
		print ("args processed")
		print (sys.argv)