def distanceBetween(system1, system2):
  try:
    coords1 = System(system1).coords
    coords2 = System(system2).coords
  except ServerError as e:
    print(e)
    sys.exit(1)
  except NotFoundError as e:
    print(e)
    syst.exit(2)
  else:
    print(int(round(math.sqrt( (coords1['x']-coords2['x'])**2
        + (coords1['y']-coords2['y'])**2
        + (coords1['z']-coords2['z'])**2 ),0)))
    sys.exit(0)
 def runsearch(event=None):
     for child in frame.winfo_children():
         child.grid_remove()
         child.destroy()
     try:
         system = System(systemField.get())
         distances = getDistances(system, cmdrs)
         nearestCmdr = min(distances, key=distances.get)
         lbl = tk.Label(frame,
                        text='nearest CMDR: {} ({} ly from {})'.format(
                            nearestCmdr.name, distances[nearestCmdr],
                            system.name))
         lbl.grid(row=0, columnspan=2)
         row = 1
         for cmdr in distances:
             row += 1
             lbl = tk.Label(frame, text='{}:'.format(cmdr.name))
             lbl.grid(row=row, column=0)
             lbl = tk.Label(frame, text='{} ly'.format(distances[cmdr]))
             lbl.grid(row=row, column=1)
     except (ServerError, SystemNotFoundError) as e:
         lbl = tk.Label(frame, text=e)
         lbl.grid(row=0, columnspan=2)
     except EdsmApiException as e:
         lbl = tk.Label(frame, text=e)
         lbl.grid(row=0, columnspan=2)
def getDistances (system, cmdrs):
  systemcoords = System(system).coords
  distances = {}
  for cmdr in cmdrs:
    cmdrcoords = getCmdrCoords(cmdr)
    distances[cmdr] = round(distance(cmdrcoords, systemcoords))
  return distances
def getBodyCount(system):
  try:
    bodyCount = System(system).bodyCount
  except ServerError as e:
    print(e)
    sys.exit(1)
  except NotFoundError as e:
    print(e)
    sys.exit(2)
  else:
    print(bodyCount)
    sys.exit(0)
def getSystemNear(name):
    # Probably want to abort at _some_ point. I’m defining two full words left as
    # the condition for that now.
    if name.count(' ') < 2:
        ret = "Aborting search at {}, require more than 2 words to limit the "
        ret += "result set."
        return ret.format(name)

    try:
        systems = System.getSystems(name)
    except NotFoundError:
        return getSystemNear(name[:-1])
    else:
        ret = ""
        for system in systems:
            ret += "{} ({}, {}, {})\n".format(system.name, system.coords['x'],
                                              system.coords['y'],
                                              system.coords['z'])
        return ret[:-1]
def getSystemList(name):
    systems = System.getSystems(name)
    ret = ""
    for system in systems:
        ret += "{}\n".format(system.name)
    return ret[:-1]
def distanceBetween(system1, system2, roundTo=2):
    systems = System.getSystems(system1, system2)
    distance = systems[0].distanceTo(systems[1], roundTo)
    if roundTo == 0:
        distance = int(distance)
    return distance
def getBodyCount(system):
    return System(system).bodyCount
parser.add_argument('--system',
                    nargs=1,
                    help='the target system (must be in ' + 'EDDN!)',
                    required=True)
parser.add_argument('--short',
                    action='store_true',
                    help='short output (only ' + 'makes sense with `--text`)')
group = parser.add_mutually_exclusive_group()
group.add_argument('--gui', action='store_true', help='explicitly run the GUI')
group.add_argument('--text',
                   action='store_true',
                   help='explicitly give text output')

args = parser.parse_args()

system = System(args.system[0].strip().replace(' ', '').replace('', ''))
cmdrs = []
for name in args.cmdrs:
    cmdrs += [Commander(name)]
shortOutput = args.short

# =================================================================================

if args.text:
    outputText()
elif args.gui:
    outputGui()
else:
    try:
        outputGui()
    except tk.TclError: