From b400a214ad59b77968feeea61741557bcb6222d9 Mon Sep 17 00:00:00 2001 From: William Ballenthin Date: Tue, 7 Jul 2020 12:14:00 -0600 Subject: [PATCH] main: add cli option --color closes #121 --- capa/main.py | 21 ++++++++++++++++----- 1 file changed, 16 insertions(+), 5 deletions(-) diff --git a/capa/main.py b/capa/main.py index 503f5f09..a7ca0737 100644 --- a/capa/main.py +++ b/capa/main.py @@ -397,6 +397,9 @@ def main(argv=None): ) parser.add_argument("-d", "--debug", action="store_true", help="Enable debugging output on STDERR") parser.add_argument("-q", "--quiet", action="store_true", help="Disable all output but errors") + parser.add_argument( + "--color", type=str, choices=("auto", "always", "never"), default="auto", help="Enable ANSI color codes in results, default: only during interactive session" + ) parser.add_argument( "-f", "--format", choices=[f[0] for f in formats], default="auto", help="Select sample format, %s" % format_help ) @@ -503,11 +506,19 @@ def main(argv=None): if not (args.verbose or args.vverbose or args.json): return -1 - # colorama will detect: - # - when on Windows console, and fixup coloring, and - # - when not an interactive session, and disable coloring - # renderers should use coloring and assume it will be stripped out if necessary. - colorama.init() + if args.color == "always": + colorama.init(strip=False) + elif args.color == "auto": + # colorama will detect: + # - when on Windows console, and fixup coloring, and + # - when not an interactive session, and disable coloring + # renderers should use coloring and assume it will be stripped out if necessary. + colorama.init() + elif args.color == "never": + colorama.init(strip=True) + else: + raise RuntimeError("unexpected --color value: " + args.color) + if args.json: print(capa.render.render_json(meta, rules, capabilities)) elif args.vverbose: