Created
February 9, 2018 14:28
-
-
Save alexvicegrab/92883319fc60c70eacc1113817bf8310 to your computer and use it in GitHub Desktop.
Colourise the information encoded in the parser
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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