from __future__ import print_function

import argparse
import logging
import sys


from . import __version__
from .shell import Shell

try:
    raw_input
except NameError:
    raw_input = input


class CLI(object):
    def run(self):
        logging.basicConfig(level=logging.ERROR)

        params = self.get_params()
        s = Shell(params.hosts,
                  params.connect_timeout,
                  setup_readline=params.run_once == "")

        if params.run_once != "":
            sys.exit(0 if s.onecmd(params.run_once) == None else 1)

        intro = "Welcome to zk-shell (%s)" % (__version__)
        first = True
        while True:
            try:
                s.run(intro if first else None)
            except KeyboardInterrupt:
                done = raw_input("\nExit? (y|n) ")
                if done == "y":
                    break
            first = False

    def get_params(self):
        parser = argparse.ArgumentParser()
        parser.add_argument("--connect-timeout",
                            type=int,
                            default=10,
                            help="ZK connect timeout")
        parser.add_argument("--run-once",
                            type=str,
                            default="",
                            help="Run a command non-interactively and exit")
        parser.add_argument("hosts",
                            nargs="*",
                            help="ZK hosts to connect")
        return parser.parse_args()

from __future__ import print_function

import argparse
import logging
import sys


from . import __version__
from .shell import Shell

try:
    raw_input
except NameError:
    raw_input = input


class CLI(object):
    def run(self):
        logging.basicConfig(level=logging.ERROR)

        params = self.get_params()
        s = Shell(params.hosts,
                  params.connect_timeout,
                  setup_readline=params.run_once == "")

        if params.run_once != "":
            try:
                sys.exit(0 if s.onecmd(params.run_once) == None else 1)
            except IOError:
                sys.exit(1)

        intro = "Welcome to zk-shell (%s)" % (__version__)
        first = True
        while True:
            try:
                s.run(intro if first else None)
            except KeyboardInterrupt:
                done = raw_input("\nExit? (y|n) ")
                if done == "y":
                    break
            first = False

    def get_params(self):
        parser = argparse.ArgumentParser()
        parser.add_argument("--connect-timeout",
                            type=int,
                            default=10,
                            help="ZK connect timeout")
        parser.add_argument("--run-once",
                            type=str,
                            default="",
                            help="Run a command non-interactively and exit")
        parser.add_argument("hosts",
                            nargs="*",
                            help="ZK hosts to connect")
        return parser.parse_args()

Handle IOError in run_once mode so paging works
Handle IOError in run_once mode so paging works

Signed-off-by: Raul Gutierrez S <f25f6873bbbde69f1fe653b3e6bd40d543b8d0e0@itevenworks.net>
