Skip to content

Commit b974154

Browse files
authored
Merge pull request JoshData#13 from neiljp/arg_parse
Command-line options: use argparse, add more checks (& pass style to --changes)
2 parents f9fe805 + b95c56e commit b974154

File tree

1 file changed

+44
-24
lines changed

1 file changed

+44
-24
lines changed

pdf_diff/command_line.py

Lines changed: 44 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -435,35 +435,55 @@ def pdftopng(pdffile, pagenumber, width=900):
435435
return im.convert("RGBA")
436436

437437
def main():
438-
if len(sys.argv) == 2 and sys.argv[1] == "--changes":
438+
import argparse
439+
440+
description = ('Calculates the differences between two specified files in PDF format '
441+
'(or changes specified on standard input) and outputs to standard output '
442+
'side-by-side images with the differences marked (in PNG format).')
443+
parser = argparse.ArgumentParser(description=description)
444+
parser.add_argument('files', nargs='*', # Use '*' to allow --changes with zero files
445+
help='Calculate differences between the two named files')
446+
parser.add_argument('--changes', action='store_true', default=False,
447+
help='Read change description from standard input, ignoring files')
448+
parser.add_argument('--style', metavar='box|strike|underline,box|stroke|underline',
449+
default='strike,underline',
450+
help='How to mark the differences in the two files (default: strike, underline)')
451+
parser.add_argument('--top-margin', metavar='margin', default=0., type=float,
452+
help='TODO (default 0.0)')
453+
args = parser.parse_args()
454+
455+
# Validate style
456+
style = args.style.split(',')
457+
if len(style) != 2:
458+
print("ERROR: Exactly two style values must be specified, if --style is used.")
459+
parser.print_usage()
460+
sys.exit(1)
461+
for i in [0,1]:
462+
if style[i] != 'box' and style[i] != 'strike' and style[i] != 'underline':
463+
print("ERROR: --style values must be box, strike or underline, not %s." % (style[i]))
464+
parser.print_usage()
465+
sys.exit(1)
466+
467+
# Ensure one of files or --changes are specified
468+
if len(args.files) == 0 and not args.changes:
469+
print("ERROR: Please specify files to compare, or use --changes option")
470+
parser.print_usage()
471+
sys.exit(1)
472+
473+
if args.changes:
439474
# to just do the rendering part
440-
render_changes(json.load(sys.stdin))
475+
img = render_changes(json.load(sys.stdin), style)
476+
img.save(sys.stdout.buffer, "PNG")
441477
sys.exit(0)
442478

443-
if len(sys.argv) <= 1:
444-
print("Usage: python3 pdf-diff.py [--style box|strike|underline,box|strike|underline] before.pdf after.pdf > changes.png", file=sys.stderr)
479+
# Ensure enough file are specified
480+
if len(args.files) != 2:
481+
print("ERROR: Insufficient number of files to compare; please supply exactly 2.")
482+
parser.print_usage()
445483
sys.exit(1)
446484

447-
args = sys.argv[1:]
448-
449-
styles = ["strike", "underline"]
450-
top_margin = 0
451-
while True:
452-
if args[0] == "--style":
453-
args.pop(0)
454-
styles = args.pop(0).split(',')
455-
continue
456-
if args[0] == "--top-margin":
457-
args.pop(0)
458-
top_margin = float(args.pop(0))
459-
continue
460-
break
461-
462-
left_file = args.pop(0)
463-
right_file = args.pop(0)
464-
465-
changes = compute_changes(left_file, right_file, top_margin=top_margin)
466-
img = render_changes(changes, styles)
485+
changes = compute_changes(args.files[0], args.files[1], top_margin=float(args.top_margin))
486+
img = render_changes(changes, style)
467487
img.save(sys.stdout.buffer, "PNG")
468488

469489

0 commit comments

Comments
 (0)