@@ -148,7 +148,27 @@ diffs. For comparing directories and files, see also, the :mod:`filecmp` module.
148148 expressed in the format returned by :func: `time.ctime `. If not specified, the
149149 strings default to blanks.
150150
151- :file: `Tools/scripts/diff.py ` is a command-line front-end for this function.
151+ ::
152+
153+ >>> s1 = ['bacon\n', 'eggs\n', 'ham\n', 'guido\n']
154+ >>> s2 = ['python\n', 'eggy\n', 'hamster\n', 'guido\n']
155+ >>> for line in context_diff(s1, s2, fromfile='before.py', tofile='after.py'):
156+ ... sys.stdout.write(line)
157+ *** before.py
158+ --- after.py
159+ ***************
160+ *** 1,4 ****
161+ ! bacon
162+ ! eggs
163+ ! ham
164+ guido
165+ --- 1,4 ----
166+ ! python
167+ ! eggy
168+ ! hamster
169+ guido
170+
171+ See :ref: `difflib-interface ` for a more detailed example.
152172
153173 .. versionadded :: 2.3
154174
@@ -265,7 +285,25 @@ diffs. For comparing directories and files, see also, the :mod:`filecmp` module.
265285 expressed in the format returned by :func: `time.ctime `. If not specified, the
266286 strings default to blanks.
267287
268- :file: `Tools/scripts/diff.py ` is a command-line front-end for this function.
288+ ::
289+
290+
291+ >>> s1 = ['bacon\n', 'eggs\n', 'ham\n', 'guido\n']
292+ >>> s2 = ['python\n', 'eggy\n', 'hamster\n', 'guido\n']
293+ >>> for line in unified_diff(s1, s2, fromfile='before.py', tofile='after.py'):
294+ ... sys.stdout.write(line)
295+ --- before.py
296+ +++ after.py
297+ @@ -1,4 +1,4 @@
298+ -bacon
299+ -eggs
300+ -ham
301+ +python
302+ +eggy
303+ +hamster
304+ guido
305+
306+ See :ref: `difflib-interface ` for a more detailed example.
269307
270308 .. versionadded :: 2.3
271309
@@ -649,3 +687,75 @@ As a single multi-line string it looks like this::
649687 ? ++++ ^ ^
650688 + 5. Flat is better than nested.
651689
690+
691+ .. _difflib-interface :
692+
693+ A command-line interface to difflib
694+ -----------------------------------
695+
696+ This example shows how to use difflib to create a ``diff ``-like utility.
697+ It is also contained in the Python source distribution, as
698+ :file: `Tools/scripts/diff.py `.
699+
700+ ::
701+
702+ """ Command line interface to difflib.py providing diffs in four formats:
703+
704+ * ndiff: lists every line and highlights interline changes.
705+ * context: highlights clusters of changes in a before/after format.
706+ * unified: highlights clusters of changes in an inline format.
707+ * html: generates side by side comparison with change highlights.
708+
709+ """
710+
711+ import sys, os, time, difflib, optparse
712+
713+ def main():
714+ # Configure the option parser
715+ usage = "usage: %prog [options] fromfile tofile"
716+ parser = optparse.OptionParser(usage)
717+ parser.add_option("-c", action="store_true", default=False,
718+ help='Produce a context format diff (default)')
719+ parser.add_option("-u", action="store_true", default=False,
720+ help='Produce a unified format diff')
721+ hlp = 'Produce HTML side by side diff (can use -c and -l in conjunction)'
722+ parser.add_option("-m", action="store_true", default=False, help=hlp)
723+ parser.add_option("-n", action="store_true", default=False,
724+ help='Produce a ndiff format diff')
725+ parser.add_option("-l", "--lines", type="int", default=3,
726+ help='Set number of context lines (default 3)')
727+ (options, args) = parser.parse_args()
728+
729+ if len(args) == 0:
730+ parser.print_help()
731+ sys.exit(1)
732+ if len(args) != 2:
733+ parser.error("need to specify both a fromfile and tofile")
734+
735+ n = options.lines
736+ fromfile, tofile = args # as specified in the usage string
737+
738+ # we're passing these as arguments to the diff function
739+ fromdate = time.ctime(os.stat(fromfile).st_mtime)
740+ todate = time.ctime(os.stat(tofile).st_mtime)
741+ fromlines = open(fromfile, 'U').readlines()
742+ tolines = open(tofile, 'U').readlines()
743+
744+ if options.u:
745+ diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile,
746+ fromdate, todate, n=n)
747+ elif options.n:
748+ diff = difflib.ndiff(fromlines, tolines)
749+ elif options.m:
750+ diff = difflib.HtmlDiff().make_file(fromlines, tolines, fromfile,
751+ tofile, context=options.c,
752+ numlines=n)
753+ else:
754+ diff = difflib.context_diff(fromlines, tolines, fromfile, tofile,
755+ fromdate, todate, n=n)
756+
757+ # we're using writelines because diff is a generator
758+ sys.stdout.writelines(diff)
759+
760+ if __name__ == '__main__':
761+ main()
0 commit comments