#!/usr/bin/env python3 import click from helpers import CliContext, Logger, EepromProgrammer @click.group() @click.option('--device', '-d', default='/dev/ttyUSB0', show_default=True, metavar='DEVICE', help="Set the serial device") @click.option('--baud', '-b', default=38400, show_default=True, metavar='BAUDRATE', help="Set the baud rate of the serial device") @click.option('--verbose', '-v', is_flag=True, help='Print debug output') @click.pass_context def eepprog(ctx: click.Context, device: str, baud: int, verbose: bool): # Create dependencies logger = Logger(verbose=verbose) eeprom_programmer = EepromProgrammer(logger=logger, device=device, baudrate=baud) logger.debug("Creating CLI context (device: {}, bauds: {})".format(device, baud)) # Create CLI context ctx.obj = CliContext( logger=logger, eeprom_programmer=eeprom_programmer, ) # Define clean up functions ctx.call_on_close(eeprom_programmer.close) @eepprog.command('hello', short_help='Say hello. :)') @click.pass_obj def hello(context: CliContext): """ Say hello. :) Just a test command that tests the logger. """ context.logger.debug('hello hello', 413, 'hellooo') context.logger.info('hello!') context.logger.info('the same', click.style('but in green!', fg='green')) context.logger.warn('this is fine') context.logger.error('ohno') context.logger.error('error, but a friendly one :)', fg='green') context.logger.success('yay!') @eepprog.command('test', short_help="Send INIT command to the programmer and read answer") @click.pass_obj def test(context: CliContext): """ Send an 'INIT' command to the programmer and read the answer. Test command for debugging. """ context.eeprom_programmer.test() # TODO command: list-devices -> serial.tools.list_ports # TODO shell: Run an interactive shell if __name__ == '__main__': eepprog()