From fabf515439712de141cd916d08d7b6bcbe1feaee Mon Sep 17 00:00:00 2001 From: =?utf8?q?Jos=C3=A9=20Fonseca?= Date: Fri, 1 May 2009 18:57:50 +0100 Subject: [PATCH] Allow to control color usage from command line. --- xml2txt.py | 16 ++++++++++++++-- 1 file changed, 14 insertions(+), 2 deletions(-) diff --git a/xml2txt.py b/xml2txt.py index 8b40a55..3d29d27 100755 --- a/xml2txt.py +++ b/xml2txt.py @@ -20,6 +20,7 @@ import sys +import optparse import xml.parsers.expat @@ -317,9 +318,20 @@ class TraceParser(XmlParser): def main(): - formatter = AnsiFormatter() + parser = optparse.OptionParser( + usage="\n\t%prog [options] [file] ...") + parser.add_option( + '--color', '--colour', + type="choice", choices=('never', 'always', 'auto'), metavar='WHEN', + dest="color", default="auto", + help="coloring: never, always, or auto [default: %default]") + (options, args) = parser.parse_args(sys.argv[1:]) + + if options.color == 'always' or options.color == 'auto' and sys.stdout.isatty(): + formatter = AnsiFormatter() + else: + formatter = Formatter() - args = sys.argv[1:] if args: for arg in args: if arg.endswith('.gz'): -- 2.45.2