Show More
@@ -28,7 +28,7 b'' | |||
|
28 | 28 | scan Python source code and re-emit it with no changes to its original |
|
29 | 29 | formatting (which is the hard part). |
|
30 | 30 | |
|
31 |
$Id: PyColorize.py 234 |
|
|
31 | $Id: PyColorize.py 2342 2007-05-15 14:46:58Z vivainio $""" | |
|
32 | 32 | |
|
33 | 33 | __all__ = ['ANSICodeColors','Parser'] |
|
34 | 34 | |
@@ -244,8 +244,8 b' class Parser:' | |||
|
244 | 244 | owrite('%s%s%s' % (color,toktext,colors.normal)) |
|
245 | 245 | |
|
246 | 246 | def main(argv=None): |
|
247 |
"""Run as a command-line script: colorize a python file using ANSI |
|
|
248 | escapes and print to stdout. | |
|
247 | """Run as a command-line script: colorize a python file or stdin using ANSI | |
|
248 | color escapes and print to stdout. | |
|
249 | 249 | |
|
250 | 250 | Inputs: |
|
251 | 251 | |
@@ -253,9 +253,10 b' def main(argv=None):' | |||
|
253 | 253 | arguments. If None, use sys.argv[1:]. |
|
254 | 254 | """ |
|
255 | 255 | |
|
256 | usage_msg = """%prog [options] filename | |
|
256 | usage_msg = """%prog [options] [filename] | |
|
257 | 257 | |
|
258 |
Colorize a python file using ANSI color escapes and print to stdout. |
|
|
258 | Colorize a python file or stdin using ANSI color escapes and print to stdout. | |
|
259 | If no filename is given, or if filename is -, read standard input.""" | |
|
259 | 260 | |
|
260 | 261 | parser = optparse.OptionParser(usage=usage_msg) |
|
261 | 262 | newopt = parser.add_option |
@@ -267,18 +268,34 b' Colorize a python file using ANSI color escapes and print to stdout."""' | |||
|
267 | 268 | |
|
268 | 269 | opts,args = parser.parse_args(argv) |
|
269 | 270 | |
|
270 |
if len(args) |
|
|
271 | parser.error("you must give one filename.") | |
|
271 | if len(args) > 1: | |
|
272 | parser.error("you must give at most one filename.") | |
|
272 | 273 | |
|
273 | # write colorized version to stdout | |
|
274 | if len(args) == 0: | |
|
275 | fname = '-' # no filename given; setup to read from stdin | |
|
276 | else: | |
|
274 | 277 | fname = args[0] |
|
278 | ||
|
279 | if fname == '-': | |
|
280 | stream = sys.stdin | |
|
281 | else: | |
|
282 | stream = file(fname) | |
|
283 | ||
|
275 | 284 | parser = Parser() |
|
285 | ||
|
286 | # we need nested try blocks because pre-2.5 python doesn't support unified | |
|
287 | # try-except-finally | |
|
276 | 288 | try: |
|
277 | parser.format(file(fname).read(),scheme=opts.scheme_name) | |
|
289 | try: | |
|
290 | # write colorized version to stdout | |
|
291 | parser.format(stream.read(),scheme=opts.scheme_name) | |
|
278 | 292 | except IOError,msg: |
|
279 | 293 | # if user reads through a pager and quits, don't print traceback |
|
280 | 294 | if msg.args != (32,'Broken pipe'): |
|
281 | 295 | raise |
|
296 | finally: | |
|
297 | if stream is not sys.stdin: | |
|
298 | stream.close() # in case a non-handled exception happened above | |
|
282 | 299 | |
|
283 | 300 | if __name__ == "__main__": |
|
284 | 301 | main() |
@@ -16,12 +16,14 b'' | |||
|
16 | 16 | .\" .sp <n> insert n+1 empty lines |
|
17 | 17 | .\" for manpage-specific macros, see man(7) |
|
18 | 18 | .SH NAME |
|
19 | pycolor \- Colorize a python file using ANSI and print to stdout. | |
|
19 | pycolor \- Colorize a python file or stdin using ANSI and print to stdout. | |
|
20 | 20 | .SH SYNOPSIS |
|
21 | 21 | .B pycolor |
|
22 |
.RI [ options ] |
|
|
22 | .RI [ options ] | |
|
23 | .RI [ file ] | |
|
23 | 24 | .SH DESCRIPTION |
|
24 |
Prints a colorized version of the input file |
|
|
25 | Prints a colorized version of the input file (or standard input if no file is | |
|
26 | given, or the file name - is given) to standard out. | |
|
25 | 27 | .SH OPTIONS |
|
26 | 28 | .TP |
|
27 | 29 | .B \-h, \-\-help |
General Comments 0
You need to be logged in to leave comments.
Login now