@@ -13,7 +13,7 @@ import os
13
13
import sys
14
14
import errno
15
15
import difflib
16
- import optparse
16
+ from optparse import Option , OptionParser
17
17
import re
18
18
import filecmp
19
19
import unicodedata
@@ -386,12 +386,31 @@ class ConsoleDiff(object):
386
386
def simple_colorize (s , chosen_color ):
387
387
return "%s%s%s" % (color_codes [chosen_color ], s , color_codes ["none" ])
388
388
389
+
390
+ class MultipleOption (Option ):
391
+ ACTIONS = Option .ACTIONS + ("extend" ,)
392
+ STORE_ACTIONS = Option .STORE_ACTIONS + ("extend" ,)
393
+ TYPED_ACTIONS = Option .TYPED_ACTIONS + ("extend" ,)
394
+ ALWAYS_TYPED_ACTIONS = Option .ALWAYS_TYPED_ACTIONS + ("extend" ,)
395
+
396
+ def take_action (self , action , dest , opt , value , values , parser ):
397
+ if action == "extend" :
398
+ values .ensure_value (dest , []).append (value )
399
+ else :
400
+ Option .take_action (self , action , dest , opt , value , values , parser )
401
+
402
+
389
403
def start ():
390
404
# If you change any of these, also update README.
391
- parser = optparse .OptionParser (usage = "usage: %prog [options] left_file right_file" ,
392
- description = "Show differences between files in a two column view." )
405
+ parser = OptionParser (usage = "usage: %prog [options] left_file right_file" ,
406
+ version = "icdiff version %s" % __version__ ,
407
+ description = "Show differences between files in a two column view." ,
408
+ option_class = MultipleOption )
409
+ parser .add_option ("-u" , "--patch" , default = True ,
410
+ action = "store_true" ,
411
+ help = "generate patch. This is always true, and only exists for compatibility" )
393
412
parser .add_option ("--cols" , default = None ,
394
- help = "specify the width of the screen. Autodetection is Linux only" )
413
+ help = "specify the width of the screen. Autodetection is Unix only" )
395
414
parser .add_option ("--head" , default = 0 ,
396
415
help = "consider only the first N lines of each file" )
397
416
parser .add_option ("--highlight" , default = False ,
@@ -406,27 +425,26 @@ def start():
406
425
parser .add_option ("--no-headers" , default = False ,
407
426
action = "store_true" ,
408
427
help = "don't label the left and right sides with their file names" )
409
- parser .add_option ("--numlines" , default = 5 ,
428
+ parser .add_option ("-U" , "--unified" , "--numlines" , default = 5 ,
429
+ metavar = "NUM" ,
410
430
help = "how many lines of context to print; can't be combined with --whole-file" )
411
431
parser .add_option ("--recursive" , default = False ,
412
432
action = "store_true" ,
413
433
help = "recursively compare subdirectories" )
414
434
parser .add_option ("--show-all-spaces" , default = False ,
415
435
action = "store_true" ,
416
436
help = "color all non-matching whitespace including that which is not needed for drawing the eye to changes. Slow, ugly, displays all changes" )
417
- parser .add_option ("--version" , default = False ,
418
- action = "store_true" ,
419
- help = "print version and exit" )
420
437
parser .add_option ("--whole-file" , default = False ,
421
438
action = "store_true" ,
422
439
help = "show the whole file instead of just changed lines and context" )
440
+ parser .add_option ("-L" , "--label" ,
441
+ action = "extend" ,
442
+ type = "string" ,
443
+ dest = 'labels' ,
444
+ help = "override file labels with arbitrary tags. Use twice, one for each file" )
423
445
424
446
(options , args ) = parser .parse_args ()
425
447
426
- if options .version :
427
- print ("icdiff version %s" % __version__ )
428
- sys .exit ()
429
-
430
448
if len (args ) != 2 :
431
449
parser .print_help ()
432
450
sys .exit ()
@@ -481,7 +499,14 @@ def diff_recursively(options, a, b):
481
499
print_meta ("File %s is a file while %s is a directory" % (a , b ))
482
500
483
501
def diff_files (options , a , b ):
484
- headers = a , b
502
+ if options .labels :
503
+ if len (options .labels ) == 2 :
504
+ headers = options .labels
505
+ else :
506
+ sys .stderr .write ("error: to use arbitrary file labels, specify -L twice.\n " )
507
+ sys .exit (1 )
508
+ else :
509
+ headers = a , b
485
510
if options .no_headers :
486
511
headers = None , None
487
512
@@ -506,7 +531,7 @@ def diff_files(options, a, b):
506
531
for line in cd .make_table (
507
532
lines_a , lines_b , headers [0 ], headers [1 ],
508
533
context = (not options .whole_file ),
509
- numlines = int (options .numlines )):
534
+ numlines = int (options .unified )):
510
535
print (line )
511
536
sys .stdout .flush ()
512
537
0 commit comments