def parse_hosts(hosts):
    """
  Parse the -h/--host argument.

  All things that accept -h handle it in the same way. The rules:

  * --host is repeatable:

      -h host1 -h host2 -h host3

  * --host can be a list of comma-delimited hosts:

      -h host1,host2,host3 -h host4,host5

  * --hosts can be given a name, which will be printed in the logs in lieu
    of long hostnames, which helps reduce the column count

      -h host1=zaybxcwd-01.mycompany.com,host2=zaybxcwd-02.mycompany.com

  * --hosts can be a filename containing a list of hosts with each line
    formatted as described above.

    The '#' character may be used in the file for comments, and blank lines
    are ignored.

      -h @myhosts.txt
  """
    res = []
    for host in hosts:
        if host[0] == '@':
            path = host[1:]
            with open(path) as pathf:
                for line in pathf:
                    line = line.strip()
                    if not line or line[0] == '#':
                        continue
                    res.append(line)
        elif ',' in host:
            res.extend(host.rstrip(',').split(','))
        else:
            res.append(host)
    hosts = res
    res = []
    for host in hosts:
        if '=' in host:
            name, host = host.split('=', 1)
            res.append(name.strip(), host.strip())
        else:
            res.append((host, host))
    return res
