import xmlrpclib, sys, getopt

global cmd, serverUrl, dir, title, action
dir = "."
title = "New job"
startIndex = 1
endIndex = 1
retry = 10
affinity = ""
priority = 1000

def usage():
	print ("Usage: control.py [OPTIONS] SERVER_URL ACTION [COMMAND]")
	print ("Control the Coalition server located at SERVER_URL.\n")
	print("Actions:")
	print("  add: add a job, use option -c for command")
	print("  list: list the jobs on the server")
	print ("Options:")
	print ("  -h, --help\t\tShow this help")
	print ("  -c, --command=COMMAND\t\tIf action is add, add command to server")
	print ("  -d, --directory=DIR\tWorking directory (default: "+dir+")")
	print ("  -t, --title=TITLE\tSet the job title (default: "+title+")")
	print ("  -p, --priority=PRIORITY\tPriority of the job (default: "+str(priority)+")")
	print ("  -r, --retry=RETRY\tNumber of retry this jobs can do (default: "+str(retry)+")")
	print ("  -a, --affinity=AFFINITY\tAffinity words to workers, separated by a comma (default: \"\"")
	print ("  -v, --verbose\t\tIncrease verbosity")

	print ("\nExample : control -t \"Job\" -a \"Linux\" -c \"echo Hello world!\" http://localhost:8080 add")

# Parse the options
try:
	opts, args = getopt.getopt(sys.argv[1:], "a:d:e:hr:s:t:v:c:", ["affinity=", "directory=", "end=", "help", "retry=", "start=", "title=", "verbose=", "command="])
	if len(args) != 2 :
		usage()
		sys.exit(2)
	serverUrl = args[0]
	action = args[1]
except getopt.GetoptError, err:
	# print help information and exit:
	print str(err) # will print something like "option -a not recognized"
	usage()
	sys.exit(2)
for o, a in opts:
	if o in ("-h", "--help"):
		usage()
		sys.exit(2)
	elif o in ("-d", "--directory"):
		dir = a
	elif o in ("-s", "--start"):
		startIndex = int(a)
	elif o in ("-e", "--end"):
		endIndex = int(a)
	elif o in ("-v", "--verbose"):
		verbose = True
	elif o in ("-r", "--retry"):
		retry = int(a)
	elif o in ("-p", "--priority"):
		priority = int(a)
	elif o in ("-a", "--affinity"):
		affinity = a
	elif o in ("-t", "--title"):
		title = a
	elif o in ("-c", "--command"):
		cmd=a
	else:
		assert False, "unhandled option " + o

# Log function
def output (str):
	if verbose:
		print (str)

server = xmlrpclib.ServerProxy(serverUrl + "/xmlrpc")

if action=="add":
	server.addjobwithaffinity (title, cmd, dir, priority, retry, affinity)
	print("job added")
elif action=="list":
	jobs=server.getjobs()
	for i in range(len(jobs)):
		print(str(jobs[i]["ID"])+" "+str(jobs[i]["Title"])+" "+str(jobs[i]["State"])+" "+str(jobs[i]["Priority"])+" "+str(jobs[i]["Affinity"])+" "+str(jobs[i]["Worker"])+" "+str(jobs[i]["Duration"])+" "+str(jobs[i]["Try"])+" "+str(jobs[i]["Command"])+" "+str(jobs[i]["Dir"]))

@@ -65,17 +65,20 @@
 , action
+,id
 %0A
-
 dir = %22.
@@ -165,16 +165,22 @@
 y = 1000
+%0Aid=-1
 %0A%0Adef us
@@ -429,32 +429,106 @@
 on the server%22)%0A
+%09print(%22  remove: remove job designated by id, option -id is necessary%22) %0A
 %09print (%22Options
@@ -1072,16 +1072,62 @@
  %5C%22%5C%22%22)%0A
+%09print(%22   -i, --jobid=JOBID%5CtID of the Job%22)%0A
 %09print (
@@ -1360,16 +1360,18 @@
 s:t:v:c:
+i:
 %22, %5B%22aff
@@ -2201,16 +2201,53 @@
 %09%09cmd=a%0A
+%09elif o in (%22-i%22, %22--jobid%22):%0A%09%09id=a%0A
 %09else:%0A%09
@@ -2584,16 +2584,16 @@
 jobs)):%0A
-
 %09%09print(
@@ -2854,8 +2854,206 @@
 Dir%22%5D))%0A
+elif action==%22remove%22:%0A%09if id%3C0: %0A%09%09print(%22Use option -id to specify the job id to remove%22)%0A%09else:%0A%09%09server.clearjob(int(id))%0Aelse:%0A%09print(%22I don't know what to do with myself. Use another action%22)%0A

remove action has been added to control.py