mirror of
				https://github.com/KevinMidboe/python-gpiozero.git
				synced 2025-10-29 17:50:37 +00:00 
			
		
		
		
	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
This commit is contained in:
		| @@ -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() | ||||
|   | ||||
		Reference in New Issue
	
	Block a user