@@ -425,35 +425,55 @@ def pdftopng(pdffile, pagenumber, width=900):
425
425
return im .convert ("RGBA" )
426
426
427
427
def main ():
428
- if len (sys .argv ) == 2 and sys .argv [1 ] == "--changes" :
428
+ import argparse
429
+
430
+ description = ('Calculates the differences between two specified files in PDF format '
431
+ '(or changes specified on standard input) and outputs to standard output '
432
+ 'side-by-side images with the differences marked (in PNG format).' )
433
+ parser = argparse .ArgumentParser (description = description )
434
+ parser .add_argument ('files' , nargs = '*' , # Use '*' to allow --changes with zero files
435
+ help = 'Calculate differences between the two named files' )
436
+ parser .add_argument ('--changes' , action = 'store_true' , default = False ,
437
+ help = 'Read change description from standard input, ignoring files' )
438
+ parser .add_argument ('--style' , metavar = 'box|strike|underline,box|stroke|underline' ,
439
+ default = 'strike,underline' ,
440
+ help = 'How to mark the differences in the two files (default: strike, underline)' )
441
+ parser .add_argument ('--top-margin' , metavar = 'margin' , default = 0. , type = float ,
442
+ help = 'TODO (default 0.0)' )
443
+ args = parser .parse_args ()
444
+
445
+ # Validate style
446
+ style = args .style .split (',' )
447
+ if len (style ) != 2 :
448
+ print ("ERROR: Exactly two style values must be specified, if --style is used." )
449
+ parser .print_usage ()
450
+ sys .exit (1 )
451
+ for i in [0 ,1 ]:
452
+ if style [i ] != 'box' and style [i ] != 'strike' and style [i ] != 'underline' :
453
+ print ("ERROR: --style values must be box, strike or underline, not %s." % (style [i ]))
454
+ parser .print_usage ()
455
+ sys .exit (1 )
456
+
457
+ # Ensure one of files or --changes are specified
458
+ if len (args .files ) == 0 and not args .changes :
459
+ print ("ERROR: Please specify files to compare, or use --changes option" )
460
+ parser .print_usage ()
461
+ sys .exit (1 )
462
+
463
+ if args .changes :
429
464
# to just do the rendering part
430
- render_changes (json .load (sys .stdin ))
465
+ img = render_changes (json .load (sys .stdin ), style )
466
+ img .save (sys .stdout .buffer , "PNG" )
431
467
sys .exit (0 )
432
468
433
- if len (sys .argv ) <= 1 :
434
- print ("Usage: python3 pdf-diff.py [--style box|strike|underline,box|strike|underline] before.pdf after.pdf > changes.png" , file = sys .stderr )
469
+ # Ensure enough file are specified
470
+ if len (args .files ) != 2 :
471
+ print ("ERROR: Insufficient number of files to compare; please supply exactly 2." )
472
+ parser .print_usage ()
435
473
sys .exit (1 )
436
474
437
- args = sys .argv [1 :]
438
-
439
- styles = ["strike" , "underline" ]
440
- top_margin = 0
441
- while True :
442
- if args [0 ] == "--style" :
443
- args .pop (0 )
444
- styles = args .pop (0 ).split (',' )
445
- continue
446
- if args [0 ] == "--top-margin" :
447
- args .pop (0 )
448
- top_margin = float (args .pop (0 ))
449
- continue
450
- break
451
-
452
- left_file = args .pop (0 )
453
- right_file = args .pop (0 )
454
-
455
- changes = compute_changes (left_file , right_file , top_margin = top_margin )
456
- img = render_changes (changes , styles )
475
+ changes = compute_changes (args .files [0 ], args .files [1 ], top_margin = float (args .top_margin ))
476
+ img = render_changes (changes , style )
457
477
img .save (sys .stdout .buffer , "PNG" )
458
478
459
479
0 commit comments