From f2e5cbcdccccde3a25c800ae6ea90e34822c4c30 Mon Sep 17 00:00:00 2001 From: Daniel Lenski Date: Fri, 14 Apr 2023 11:37:59 -0700 Subject: [PATCH] Also show Java ZXing library version in --version output This requires an error-deferring `ArgumentParser` subclass, because we need to initialize the `BarCodeReader` class before we can get the Java ZXing library version. (I took the error-deferring subclass from https://gist.github.com/jmoiron/6543743 and lightly adapted it.) --- zxing/__main__.py | 23 +++++++++++++++++++++-- 1 file changed, 21 insertions(+), 2 deletions(-) diff --git a/zxing/__main__.py b/zxing/__main__.py index 9585480..f779516 100644 --- a/zxing/__main__.py +++ b/zxing/__main__.py @@ -6,18 +6,37 @@ from .version import __version__ +class ErrorDeferredArgumentParser(argparse.ArgumentParser): + def __init__(self, *args, **kwargs): + self._errors = [] + super().__init__(*args, **kwargs) + + def error(self, message): + self._errors.append(message) + + def handle_errors(self): + for e in self._errors: + super().error(e) + + def main(): - p = argparse.ArgumentParser() + p = ErrorDeferredArgumentParser() p.add_argument('-c', '--csv', action='store_true') p.add_argument('--try-harder', action='store_true') p.add_argument('image', nargs='+') p.add_argument('-P', '--classpath', help=argparse.SUPPRESS) p.add_argument('-J', '--java', help=argparse.SUPPRESS) - p.add_argument('-V', '--version', action='version', version='%(prog)s ' + __version__) + p.add_argument('-V', '--version', action='store_true') args = p.parse_args() + if p._errors and not args.version: + p.handle_errors() bcr = BarCodeReader(args.classpath, args.java) + if args.version: + p.exit(0, '%s v%s\n' + 'using Java ZXing library version v%s\n' % (p.prog, __version__, bcr.zxing_version)) + if args.csv: wr = csv.writer(stdout) wr.writerow(('Filename', 'Format', 'Type', 'Raw', 'Parsed'))