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 argparse
|
||||||
import sys
|
import sys
|
||||||
|
|
||||||
from gpiozero import *
|
from gpiozero import pi_info
|
||||||
|
|
||||||
|
|
||||||
def parse_args(args):
|
class PinoutTool(object):
|
||||||
parser = argparse.ArgumentParser(
|
def __init__(self):
|
||||||
description=__doc__
|
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(
|
def __call__(self, args=None):
|
||||||
'-r', '--revision',
|
if args is None:
|
||||||
dest='revision',
|
args = sys.argv[1:]
|
||||||
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 == '':
|
|
||||||
try:
|
try:
|
||||||
pi_info().pprint(color=args.color)
|
return self.main(self.parser.parse_args(args)) or 0
|
||||||
except IOError:
|
except argparse.ArgumentError as e:
|
||||||
print('This device is not a Raspberry Pi')
|
# argparse errors are already nicely formatted, print to stderr and
|
||||||
sys.exit(1)
|
# exit with code 2
|
||||||
else:
|
raise e
|
||||||
pi_info(args.revision).pprint(color=args.color)
|
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 = PinoutTool()
|
||||||
main()
|
|
||||||
|
|||||||
Reference in New Issue
Block a user