aboutsummaryrefslogtreecommitdiff
path: root/rainbow_parenthesis/__main__.py
blob: 7e820fcd29ea5c962f375b9a6e28bcc43e88212f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
"""
Entry point for rainbow parenthesis.

Reads a string from stdin, and outputs it to stdout with all
parenthesis prettily colored.
"""

from . import colorize, Colored, __version__
from . import term
import argparse


def build_argparse() -> argparse.ArgumentParser:
    """
    Construct the argument parser for ``rainbow`` .

    This is mostly a separate procedure to allow direct generation of
    man pages.
    """
    parser = argparse.ArgumentParser(prog='rainbow')
    parser.add_argument('input', type=argparse.FileType('r'),
                        nargs='?', default='-')
    parser.add_argument('--version',
                        action='version',
                        version=f'%(prog)s {__version__}')
    return parser


def main():
    """Interactive entry point for the program."""
    parser = build_argparse()
    args = parser.parse_args()

    for item in colorize(args.input):
        match item:
            case Colored():
                print(term.colorize(item), end='')
            case s:
                print(s, end='')


if __name__ == '__main__':
    main()