CLI minimal interface.

def main_cli():
    """CLI minimal interface."""
    # Get params
    args = _cli_argument_parser()
    delta_secs = args.delay
    i2cbus = args.bus
    i2c_address = args.address
    sensor_key = args.sensor
    sensor_params = args.params
    params = {}
    if sensor_params:
        def _parse_param(str_param):
            key, value = str_param.split('=')
            try:
                value = int(value)
            except ValueError:
                pass
            return {key.strip(): value}

        [params.update(_parse_param(sp)) for sp in sensor_params]

    if sensor_key:
        from time import sleep
        # Bus init
        try:
            # noinspection PyUnresolvedReferences
            import smbus
            bus_handler = smbus.SMBus(i2cbus)
        except ImportError as exc:
            print(exc, "\n", "Please install smbus-cffi before.")
            sys.exit(-1)

        # Sensor selection
        try:
            sensor_handler, i2c_default_address = SENSORS[sensor_key]
        except KeyError:
            print("'%s' is not recognized as an implemented i2c sensor."
                  % sensor_key)
            sys.exit(-1)

        if i2c_address:
            i2c_address = hex(int(i2c_address, 0))
        else:
            i2c_address = i2c_default_address

        # Sensor init
        sensor = sensor_handler(bus_handler, i2c_address, **params)

        # Infinite loop
        try:
            while True:
                sensor.update()
                if not sensor.sample_ok:
                    print("An error has occured.")
                    break
                print(sensor.current_state_str)
                sleep(delta_secs)
        except KeyboardInterrupt:
            print("Bye!")
    else:
        # Run detection mode
        from subprocess import check_output
        cmd = '/usr/sbin/i2cdetect -y {}'.format(i2cbus)
        try:
            output = check_output(cmd.split())
            print("Running i2cdetect utility in i2c bus {}:\n"
                  "The command '{}' has returned:\n{}"
                  .format(i2cbus, cmd, output.decode()))
        except FileNotFoundError:
            print("Please install i2cdetect before.")
            sys.exit(-1)

        # Parse output
        addresses = ['0x' + l for line in output.decode().splitlines()[1:]
                     for l in line.split()[1:] if l != '--']
        if addresses:
            print("{} sensors detected in {}"
                  .format(len(addresses), ', '.join(addresses)))
        else:
            print("No i2c sensors detected.")