Skip to content

Commit

Permalink
Also show Java ZXing library version in --version output
Browse files Browse the repository at this point in the history
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.)
  • Loading branch information
dlenski committed Apr 14, 2023
1 parent 8bffb1a commit f2e5cbc
Showing 1 changed file with 21 additions and 2 deletions.
23 changes: 21 additions & 2 deletions zxing/__main__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'))
Expand Down

0 comments on commit f2e5cbc

Please sign in to comment.