@@ -743,65 +743,4 @@ This example shows how to use difflib to create a ``diff``-like utility.
743743It is also contained in the Python source distribution, as
744744:file: `Tools/scripts/diff.py `.
745745
746- .. testcode ::
747-
748- """ Command line interface to difflib.py providing diffs in four formats:
749-
750- * ndiff: lists every line and highlights interline changes.
751- * context: highlights clusters of changes in a before/after format.
752- * unified: highlights clusters of changes in an inline format.
753- * html: generates side by side comparison with change highlights.
754-
755- """
756-
757- import sys, os, time, difflib, optparse
758-
759- def main():
760- # Configure the option parser
761- usage = "usage: %prog [options] fromfile tofile"
762- parser = optparse.OptionParser(usage)
763- parser.add_option("-c", action="store_true", default=False,
764- help='Produce a context format diff (default)')
765- parser.add_option("-u", action="store_true", default=False,
766- help='Produce a unified format diff')
767- hlp = 'Produce HTML side by side diff (can use -c and -l in conjunction)'
768- parser.add_option("-m", action="store_true", default=False, help=hlp)
769- parser.add_option("-n", action="store_true", default=False,
770- help='Produce a ndiff format diff')
771- parser.add_option("-l", "--lines", type="int", default=3,
772- help='Set number of context lines (default 3)')
773- (options, args) = parser.parse_args()
774-
775- if len(args) == 0:
776- parser.print_help()
777- sys.exit(1)
778- if len(args) != 2:
779- parser.error("need to specify both a fromfile and tofile")
780-
781- n = options.lines
782- fromfile, tofile = args # as specified in the usage string
783-
784- # we're passing these as arguments to the diff function
785- fromdate = time.ctime(os.stat(fromfile).st_mtime)
786- todate = time.ctime(os.stat(tofile).st_mtime)
787- with open(fromfile) as fromf, open(tofile) as tof:
788- fromlines, tolines = list(fromf), list(tof)
789-
790- if options.u:
791- diff = difflib.unified_diff(fromlines, tolines, fromfile, tofile,
792- fromdate, todate, n=n)
793- elif options.n:
794- diff = difflib.ndiff(fromlines, tolines)
795- elif options.m:
796- diff = difflib.HtmlDiff().make_file(fromlines, tolines, fromfile,
797- tofile, context=options.c,
798- numlines=n)
799- else:
800- diff = difflib.context_diff(fromlines, tolines, fromfile, tofile,
801- fromdate, todate, n=n)
802-
803- # we're using writelines because diff is a generator
804- sys.stdout.writelines(diff)
805-
806- if __name__ == '__main__':
807- main()
746+ .. literalinclude :: ../../Tools/scripts/diff.py
0 commit comments