text: #!/usr/bin/env python
  # -*- coding: utf-8 -*-
  
  import sys
  import subprocess
  import argparse
  from getpass import getpass
  
  from mgqueue import mgqueue
  
  ########################################################
  # main
  if( len(sys.argv) == 1 ):
  	mgqueue.ls_queue()
  	sys.exit()
  
  parser = argparse.ArgumentParser(description=mgqueue.title+'-'+mgqueue.version)
  
  parser.add_argument('queue', help='queue name')
  parser.add_argument('-ls', '-l', action='store_true', help='show task list')
  
  subparser = parser.add_subparsers(help='chose one command')
  
  
  ########################################################
  ## remove files
  def clear(mgQueue, args):
  	try:
  		msg = mgQueue.clear_file()
  	except ValueError as err:
  		print('***** ERROR *****')
  		msg = err
  	print(msg)
  parser_clear = subparser.add_parser('clear', help='remove reated files')
  parser_clear.set_defaults(func=clear)
  
  ########################################################
  ## initialize prefix
  def prefix(mgQueue, args):
  	try:
  		msg = mgQueue.save_prefix( args.prefix_data[0] )
  	except ValueError as err:
  		print('***** ERROR *****')
  		msg = err
  	print(msg)
  parser_prefix = subparser.add_parser('prefix', help='initialize queue with prefix')
  parser_prefix.set_defaults(func=prefix)
  parser_prefix.add_argument('prefix_data', nargs=1, help='specify prefix')
  
  ########################################################
  ## check queue
  def check(mgQueue, args):
  	msg = mgQueue.check_queue()
  parser_check = subparser.add_parser('check', help='check queue')
  parser_check.set_defaults(func=check)
  
  ########################################################
  ## start daemon
  def start(mgQueue, args):
  	gmail_account = args.gmail[0]
  	password = args.password[0]
  	
  	if( gmail_account != '' ):
  		if( password == '' ):
  			password = getpass( 'password for {account}@gmail.com: '.format(account=gmail_account) )
  
  	print( 'Daemon for ' + args.queue + ' is starting.' )
  	
  	try:
  		mgQueue.daemon_start(gmail_account=gmail_account, password=password)
  	except ValueError as msg:
  		print('***** ERROR *****')
  		print(msg)
  parser_start = subparser.add_parser('start', help='start daemon for queue')
  parser_start.set_defaults(func=start)
  parser_start.add_argument('-gmail', nargs=1, default=[''], help='account name for gmail,  [GMAIL]@gmail.com')
  parser_start.add_argument('-password',  nargs=1, default=[''], help='password for gmail,  [GMAIL]@gmail.com (NOT RECOMENDED)')
  
  ########################################################
  ## stop daemon
  def stop(mgQueue, args):
  	try:
  		msg = mgQueue.daemon_stop()
  	except ValueError as err:
  		print('***** ERROR *****')
  		msg = err
  	print(msg)
  parser_stop = subparser.add_parser('stop', help='start stop for queue')
  parser_stop.set_defaults(func=stop)
  
  ########################################################
  ## show log
  def log(mgQueue, args):
  	try:
  		if( args.r == None ):
  			mgQueue.cat_log()
  		else:
  			mgQueue.cat_log(args.r[0])
  	except ValueError as err:
  		print('***** ERROR *****')
  		print(err)
  parser_log = subparser.add_parser('log', help='show log of queue')
  parser_log.set_defaults(func=log)
  parser_log.add_argument('-r', nargs=1, help='regular expression')
  
  ########################################################
  ## add task
  def ad(mgQueue, args):
  	try:
  		task, msg = mgQueue.add_cmd( args.command, stdout=args.stdout[0], stderr=args.stderr[0] )
  	except ValueError as err:
  		print('***** ERROR *****')
  		msg = err
  	print(msg)
  parser_ad = subparser.add_parser('ad', help='add task to queue')
  parser_ad.set_defaults(func=ad)
  parser_ad.add_argument('command', nargs='+', help='specify task')
  parser_ad.add_argument('-stdout', nargs=1, default=['/dev/null'], help='specify stdout')
  parser_ad.add_argument('-stderr', nargs=1, default=['/dev/null'], help='specify stderr')
  
  ########################################################
  ## remove task
  def rm(mgQueue, args):
  	try: 
  		task, msg = mgQueue.remove_ind( args.index[0] )
  	except ValueError as err:
  		print('***** ERROR *****')
  		msg = err
  	print(msg)
  parser_rm = subparser.add_parser('rm', help='remove task from queue')
  parser_rm.set_defaults(func=rm)
  parser_rm.add_argument('index', nargs=1, type=int, help='task index')
  
  ########################################################
  ## remove all task
  def rmall(mgQueue, args):
  	mgQueue.remove_all()
  parser_rmall = subparser.add_parser('rmall', help='remove all task from queue')
  parser_rmall.set_defaults(func=rmall)
  
  ########################################################
  ## up task
  def up(mgQueue, args):
  	try:
  		task, newind, msg = mgQueue.up_ind( args.index[0], args.c[0] )
  	except ValueError as err:
  		print('***** ERROR *****')
  		msg = err
  	print(msg)
  parser_up = subparser.add_parser('up', help='up task in queue' )
  parser_up.set_defaults(func=up)
  parser_up.add_argument('index', nargs=1, type=int, help='task index')
  parser_up.add_argument('-c', nargs=1, type=int, default=[1], help='up count')
  
  ########################################################
  ## down task
  def dn(mgQueue, args):
  	try:
  		task, newind, msg = mgQueue.down_ind( args.index[0], args.c[0] )
  	except ValueError as err:
  		print('***** ERROR *****')
  		msg = err
  	print(msg)
  parser_dn = subparser.add_parser('dn', help='down task in queue' )
  parser_dn.set_defaults(func=dn)
  parser_dn.add_argument('index', nargs=1, type=int, help='task index')
  parser_dn.add_argument('-c', nargs=1, type=int, default=[1], help='down count')
  
  ########################################################
  ## move task
  def mv(mgQueue, args):
  	try:
  		task, newind, msg = mgQueue.move_ind( args.index[0], args.c[0] )
  	except ValueError as err:
  		print('***** ERROR *****')
  		msg = err
  	print(msg)
  parser_mv = subparser.add_parser('mv', help='move task in queue' )
  parser_mv.set_defaults(func=mv)
  parser_mv.add_argument('index', nargs=1, type=int, help='task index')
  parser_mv.add_argument('-c', nargs=1, type=int, default=1, help='new index')
  
  ########################################################
  ## switch task
  def sw(mgQueue, args):
  	try:
  		sw_queue = mgqueue.mgqueue(args.sw_queue[0])
  		task, msg = mgQueue.remove_ind( args.index[0] )
  		sw_queue.add_task( task )
  		
  	except ValueError as err:
  		print('***** ERROR *****')
  		msg = err
  		print(msg)
  	
  parser_sw = subparser.add_parser('sw', help='swtich task in queue to other queue' )
  parser_sw.set_defaults(func=sw)
  parser_sw.add_argument('index', nargs=1, type=int, help='task index')
  parser_sw.add_argument('sw_queue', nargs=1, help='queue to be appended')
  
  ########################################################
  # ls task
  def ls(mgQueue, args):
  	if( not args.ls ):
  		mgQueue.ls_task()
  parser.set_defaults(func=ls)
  
  
  ########################################################
  # call subfunction
  args = parser.parse_args()
  mgQueue = mgqueue.mgqueue(args.queue)
  args.func(mgQueue,args)
  
  
  ########################################################
  # -ls
  if( args.ls ):
  	mgQueue.ls_task()
  
  