import sys
import argparse

from srts200 import Srts200, AXES

def app():
  if len(sys.argv)>1 and sys.argv[1] == "position":
    parser = argparse.ArgumentParser(prog="position", description="Position of Axis")
    parser.add_argument("axis", help="axis name", choices=["radome", "elevation", "azimuth", "polarization"], type=str, nargs=1)
    try:
      args = parser.parse_args(sys.argv[2:])
    except Exception:
      print(parser.format_help())
      exit()
    axis = None
    if args.axis[0]=="radome":
        axis = AXES.RADOME
    elif args.axis[0]=="elevation":
        axis = AXES.ELEVATION
    elif args.axis[0]=="azimuth":
        axis = AXES.AZIMUTH
    else: # args["polarization"]:
        axis = AXES.POLARIZATION
    srts = Srts200()
    print(srts.position(axis))
  elif len(sys.argv)>1 and sys.argv[1] == "move":
    parser = argparse.ArgumentParser(prog="move", description="Move Axis")
    parser.add_argument("axis", help="axis name", choices=["radome", "elevation", "azimuth", "polarization"], type=str, nargs=1)
    parser.add_argument("target", help="target position", type=float, nargs=1)
    parser.add_argument("--absolute", help="absolutely position", action="store_true", default=True)
    try:
      args = parser.parse_args(sys.argv[2:])
    except Exception:
      print(parser.format_help())
      exit()
    axis = None
    if args.axis[0]=="radome":
        axis = AXES.RADOME
    elif args.axis[0]=="elevation":
        axis = AXES.ELEVATION
    elif args.axis[0]=="azimuth":
        axis = AXES.AZIMUTH
    else: # args["polarization"]:
        axis = AXES.POLARIZATION
    srts = Srts200()
    srts.move(axis, args.target[0])
  elif len(sys.argv)>1 and sys.argv[1] == "stop":
    parser = argparse.ArgumentParser(prog="move", description="Move Axis")
    parser.add_argument("axis", help="axis name", choices=["radome", "elevation", "azimuth", "polarization"], type=str, nargs='?')
    try:
      args = parser.parse_args(sys.argv[2:])
    except Exception:
      print(parser.format_help())
      exit()
    axis = None
    if args.axis:        
        if args.axis[0]=="radome":
            axis = AXES.RADOME
        elif args.axis[0]=="elevation":
            axis = AXES.ELEVATION
        elif args.axis[0]=="azimuth":
            axis = AXES.AZIMUTH
        else: # args["polarization"]:
            axis = AXES.POLARIZATION
    srts = Srts200()
    srts.stop(axis)
  else:
    parser = argparse.ArgumentParser(prog="srts200", description="SRTS200 Radome Measurement Pedestal Control Command Line Program")
    parser.add_argument("position", help="position of axis", type=str, nargs="?")
    parser.add_argument("move", help="move axis", type=str, nargs="?")
    parser.add_argument("stop", help="stop axis", type=str, nargs="?")
    print(parser.format_help().replace("positional arguments","module/function").replace("[move] ","").replace("[stop] ",""))

if __name__ == "__main__":
  app()
