use argparse, comments in english
This commit is contained in:
parent
fea7e2c03f
commit
60c86f9182
1 changed files with 26 additions and 20 deletions
42
colout.py
42
colout.py
|
|
@ -1,4 +1,4 @@
|
||||||
#!/usr/bin/env python3
|
#!/usr/bin/env python
|
||||||
#encoding: utf-8
|
#encoding: utf-8
|
||||||
|
|
||||||
import re
|
import re
|
||||||
|
|
@ -62,26 +62,32 @@ def colorup( text, pattern, color, style = "standard" ):
|
||||||
|
|
||||||
if __name__ == "__main__":
|
if __name__ == "__main__":
|
||||||
import sys
|
import sys
|
||||||
|
import argparse
|
||||||
|
|
||||||
pattern = ".*"
|
parser = argparse.ArgumentParser(
|
||||||
color= "red"
|
description="A regular expression based formatter that color up an arbitrary text output stream.")
|
||||||
style = "bold"
|
|
||||||
|
|
||||||
nargs = len(sys.argv)
|
parser.add_argument("pattern", metavar="REGEX", type=str, nargs=1,
|
||||||
|
help="A regular expression")
|
||||||
|
|
||||||
if nargs <= 1 or nargs >= 5:
|
parser.add_argument("color", metavar="COLOR", type=str, nargs='?',
|
||||||
msg = "Usage: colorout pattern [color] [style]"
|
default="red",
|
||||||
msg += "\n\tAvailable colors: "+" ".join(colors)
|
help="One of the following colors: "+" ".join(colors), choices = colors)
|
||||||
msg += "\n\tAvailable styles: "+" ".join(styles)
|
|
||||||
sys.exit(msg)
|
parser.add_argument("style", metavar="STYLE", type=str, nargs='?',
|
||||||
else:
|
default="bold",
|
||||||
if nargs > 1:
|
help="One of the following styles: "+" ".join(styles), choices=styles)
|
||||||
pattern = sys.argv[1]
|
|
||||||
if nargs > 2:
|
parser.add_argument("-e", "--stderr", action="store_true",
|
||||||
color = sys.argv[2]
|
help="Output on the stderr instead of stdout")
|
||||||
if nargs > 3:
|
|
||||||
style = sys.argv[3]
|
args = parser.parse_args()
|
||||||
|
|
||||||
for line in sys.stdin:
|
for line in sys.stdin:
|
||||||
print( colorup( line, pattern, color, style ), end="" )
|
if not args.stderr:
|
||||||
|
print colorup( line, args.pattern[0], args.color, args.style ),
|
||||||
|
sys.stdout.flush()
|
||||||
|
else:
|
||||||
|
print >> sys.stderr, colorup( line, args.pattern[0], args.color, args.style ),
|
||||||
|
sys.stderr.flush()
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue