From 4725c6b68c3ab4331cff2a93790f3bf27d601d1e Mon Sep 17 00:00:00 2001 From: Dave Jones Date: Tue, 13 Jun 2017 16:15:15 +0100 Subject: [PATCH] Tidy up pinout a little: Ensure all error messages are printed on stderr instead of stdout; make sure all errors formatted the same and that argparse's default exit codes are followed --- gpiozero/cli/pinout.py | 95 +++++++++++++++++++++--------------------- 1 file changed, 47 insertions(+), 48 deletions(-) diff --git a/gpiozero/cli/pinout.py b/gpiozero/cli/pinout.py index 70b84c8..b12b622 100755 --- a/gpiozero/cli/pinout.py +++ b/gpiozero/cli/pinout.py @@ -10,57 +10,56 @@ from __future__ import unicode_literals, absolute_import, print_function, divisi import argparse import sys -from gpiozero import * +from gpiozero import pi_info -def parse_args(args): - parser = argparse.ArgumentParser( - description=__doc__ - ) +class PinoutTool(object): + def __init__(self): + self.parser = argparse.ArgumentParser( + description=__doc__ + ) + self.parser.add_argument( + '-r', '--revision', + dest='revision', + default='', + help='RPi revision. Default is to autodetect revision of current device' + ) + self.parser.add_argument( + '-c', '--color', + action="store_true", + default=None, + help='Force colored output (by default, the output will include ANSI' + 'color codes if run in a color-capable terminal). See also --monochrome' + ) + self.parser.add_argument( + '-m', '--monochrome', + dest='color', + action='store_false', + help='Force monochrome output. See also --color' + ) - parser.add_argument( - '-r', '--revision', - dest='revision', - default='', - help='RPi revision. Default is to autodetect revision of current device' - ) - - parser.add_argument( - '-c', '--color', - action="store_true", - default=None, - help='Force colored output (by default, the output will include ANSI' - 'color codes if run in a color-capable terminal). See also --monochrome' - ) - - parser.add_argument( - '-m', '--monochrome', - dest='color', - action='store_false', - help='Force monochrome output. See also --color' - ) - - try: - args = parser.parse_args(args) - except argparse.ArgumentError as ex: - print('Error parsing arguments.') - parser.error(str(ex.message)) - sys.exit(1) - return args - - -def main(): - args = parse_args(sys.argv[1:]) - - if args.revision == '': + def __call__(self, args=None): + if args is None: + args = sys.argv[1:] try: - pi_info().pprint(color=args.color) - except IOError: - print('This device is not a Raspberry Pi') - sys.exit(1) - else: - pi_info(args.revision).pprint(color=args.color) + return self.main(self.parser.parse_args(args)) or 0 + except argparse.ArgumentError as e: + # argparse errors are already nicely formatted, print to stderr and + # exit with code 2 + raise e + except Exception as e: + # Output anything else nicely formatted on stderr and exit code 1 + self.parser.exit(1, '{prog}: error: {message}\n'.format( + prog=self.parser.prog, message=e)) + + def main(self, args): + if args.revision == '': + try: + pi_info().pprint(color=args.color) + except IOError: + raise IOError('This device is not a Raspberry Pi') + else: + pi_info(args.revision).pprint(color=args.color) -if __name__ == '__main__': - main() +main = PinoutTool()