#!/usr/bin/env python

import sys
from key_note import change, get, add, delete, scan, after_run

def usage():
	print("note -h: display help info")
	print("note command parameters")
	print("  commands")
	print("          : a or add (add a note, the parameter is a file contains the note's info")
	print("          : g or get (get note(s), the parameters are keywords")
	print("          : c or change (change a note, the parameter is a file contains the note's info")
	print("          : d or delete (delete note, the parameters are keywords")
	print("          : s or scan (list the note's info)")
	print("for comand delete, there is a recursive mood like 'note d -r key1 key2...'")
	return

comand2function = {'change':change, 'get':get, 'delete':delete, 'add':add, 'scan': scan,
                   'c':change, 'g':get, 'd':delete, 'a':add, 's': scan}

if __name__ == '__main__':
	if len(sys.argv) < 2:
		usage()
		exit()
	else:
		comand = sys.argv[1]
		params = sys.argv[2:]	
		if '-h' == comand:
			usage()
			exit()

		function = comand2function.get(comand, None)
		if not function:
			print("Error. No such command.")
			usage()
		else:
			if function==scan:
				scan()
				#function()
			elif function==add or function==change:
				function(params[0])
			elif function==get:
				function(params)
			else:
				if '-r' in params:
					index = params.index('-r')
					params.pop(index)
					function(params, recursive=True)
				else:
					function(params)
			after_run()
