1 """ Command line interface to difflib.py providing diffs in three formats:
3 * ndiff: lists every line and highlights interline changes.
4 * context: highlights clusters of changes in a before/after format
5 * unified: highlights clusters of changes in an inline format.
9 import sys
, os
, time
, difflib
, optparse
11 usage
= "usage: %prog [options] fromfile tofile"
12 parser
= optparse
.OptionParser(usage
)
13 parser
.add_option("-c", action
="store_true", default
=False, help='Produce a context format diff (default)')
14 parser
.add_option("-u", action
="store_true", default
=False, help='Produce a unified format diff')
15 parser
.add_option("-n", action
="store_true", default
=False, help='Produce a ndiff format diff')
16 parser
.add_option("-l", "--lines", type="int", default
=3, help='Set number of context lines (default 3)')
17 (options
, args
) = parser
.parse_args()
23 parser
.error("need to specify both a fromfile and tofile")
26 fromfile
, tofile
= args
28 fromdate
= time
.ctime(os
.stat(fromfile
).st_mtime
)
29 todate
= time
.ctime(os
.stat(tofile
).st_mtime
)
30 fromlines
= open(fromfile
).readlines()
31 tolines
= open(tofile
).readlines()
34 diff
= difflib
.unified_diff(fromlines
, tolines
, fromfile
, tofile
, fromdate
, todate
, n
=n
)
36 diff
= difflib
.ndiff(fromlines
, tolines
)
38 diff
= difflib
.context_diff(fromlines
, tolines
, fromfile
, tofile
, fromdate
, todate
, n
=n
)
40 sys
.stdout
.writelines(diff
)