# coding: utf-8

from __future__ import absolute_import
from __future__ import print_function

import inspect
import pprint
import requests
import sys

from . import config


def command(parser, args):
    """ Interact with registered hosts. """
    subparsers = parser.add_subparsers(dest='subcommand')
    subcommands = {
        'list': _list,
        }
    for name, fn in subcommands.items():
        subparsers.add_parser(name, help=inspect.getdoc(fn))
    subargs, extra = parser.parse_known_args(args)
    subcommands[subargs.subcommand](
        subparsers.choices[subargs.subcommand],
        extra)


def _list(parser, args):
    """ List all registered hosts. """
    response = requests.get(
        config.href('/api/v1/hosts'),
        **config.requests_opts())
    if config.args.raw:
        pprint.pprint(response.json())
        sys.exit(0)
    try:
        response.raise_for_status()
    except requests.exceptions.HTTPError:
        print(response.json()['message'], file=sys.stderr)
        sys.exit(1)
    for item in response.json()['items']:
        print(item['Hosts']['host_name'])

@@ -665,24 +665,156 @@
  hosts. %22%22%22%0A
+    parser.add_argument(%0A        '--fields',%0A        help=%22Print host details.%22)%0A    subargs, extra = parser.parse_known_args(args)%0A
     response
@@ -868,16 +868,59 @@
 osts'),%0A
+        params=%7B'fields': subargs.fields%7D,%0A
         
@@ -1248,38 +1248,409 @@
     
-print(item%5B'Hosts'%5D%5B'host_name'%5D
+line = %5B%5D%0A        line_keys = %5B'host_name', 'cluster_name'%5D%0A        for key in line_keys:%0A            if key in item%5B'Hosts'%5D:%0A                line.append(item%5B'Hosts'%5D%5Bkey%5D)%0A        print(' - '.join(line))%0A        for key, value in item%5B'Hosts'%5D.items():%0A            if key not in line_keys:%0A                print('%7Bindent%7D%7Bkey%7D: %7Bvalue%7D'.format(%0A                    indent=' '*4, key=key, value=value)
 )%0A

Implement host list --fields.