Skip to content

Instantly share code, notes, and snippets.

@alexvicegrab
Created February 9, 2018 14:28
Show Gist options
  • Save alexvicegrab/92883319fc60c70eacc1113817bf8310 to your computer and use it in GitHub Desktop.
Save alexvicegrab/92883319fc60c70eacc1113817bf8310 to your computer and use it in GitHub Desktop.
Colourise the information encoded in the parser
#! /usr/bin/env python
"""
Parser will be colourised using ANSI colors via "blessings" library
Calling:
$ python ./parser.py --help
Will show the help printed in a different colour:
$ python ./parser.py options --foo sausage --bar egg
Will display the argument namespace in colour
"""
import argparse
import json
from blessings import Terminal
t = Terminal()
if __name__ == '__main__':
parser = argparse.ArgumentParser(
description='Template parser example',
formatter_class=argparse.RawTextHelpFormatter)
# Add an argument that works for all parsers
parser.add_argument('-v', '--verbose', action='store_true',
help=t.yellow('Do we print the results we obtain after running the commands?'))
parser.add_argument('-f', '--foo', required=True, type=str,
help=t.yellow('Foo value'))
parser.add_argument('-b', '--bar', required=True, type=str,
help=t.yellow('Bar value'))
arguments = parser.parse_args()
print(t.green(str(arguments)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment