Skip to content
This repository was archived by the owner on May 7, 2026. It is now read-only.

Commit f2e5cbc

Browse files
committed
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.)
1 parent 8bffb1a commit f2e5cbc

1 file changed

Lines changed: 21 additions & 2 deletions

File tree

zxing/__main__.py

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,37 @@
66
from .version import __version__
77

88

9+
class ErrorDeferredArgumentParser(argparse.ArgumentParser):
10+
def __init__(self, *args, **kwargs):
11+
self._errors = []
12+
super().__init__(*args, **kwargs)
13+
14+
def error(self, message):
15+
self._errors.append(message)
16+
17+
def handle_errors(self):
18+
for e in self._errors:
19+
super().error(e)
20+
21+
922
def main():
10-
p = argparse.ArgumentParser()
23+
p = ErrorDeferredArgumentParser()
1124
p.add_argument('-c', '--csv', action='store_true')
1225
p.add_argument('--try-harder', action='store_true')
1326
p.add_argument('image', nargs='+')
1427
p.add_argument('-P', '--classpath', help=argparse.SUPPRESS)
1528
p.add_argument('-J', '--java', help=argparse.SUPPRESS)
16-
p.add_argument('-V', '--version', action='version', version='%(prog)s ' + __version__)
29+
p.add_argument('-V', '--version', action='store_true')
1730
args = p.parse_args()
31+
if p._errors and not args.version:
32+
p.handle_errors()
1833

1934
bcr = BarCodeReader(args.classpath, args.java)
2035

36+
if args.version:
37+
p.exit(0, '%s v%s\n'
38+
'using Java ZXing library version v%s\n' % (p.prog, __version__, bcr.zxing_version))
39+
2140
if args.csv:
2241
wr = csv.writer(stdout)
2342
wr.writerow(('Filename', 'Format', 'Type', 'Raw', 'Parsed'))

0 commit comments

Comments
 (0)