#!/usr/bin/env python
# Exercise 5
# cat command

import argparse
from pathlib import Path

parser = argparse.ArgumentParser()
parser.add_argument("paths", nargs="+", type=Path)
parser.add_argument(
    "-b", "--number-nonblank", default=False, action="store_true"
)
parser.add_argument("-n", "--number", default=False, action="store_true")
args = parser.parse_args()
count = 1
rest = 0
out = []

for path in args.paths:
    if path.is_file():
        for line in open(path, "r").readlines():
            blank = len(line.strip()) == 0 and args.number_nonblank
            if blank:
                out.append({"ln": 0, "txt": ""})
            else:
                out.append({"ln": count, "txt": line.rstrip()})
                count += 1

for el in out:
    line = ""
    blank = len(el["txt"].strip()) == 0 and args.number_nonblank
    if args.number and not blank:
        total_width = len(str(out[-1]["ln"] - rest))
        curr_width = len(str(el["ln"] - rest))
        extra = " " * (total_width - curr_width)
        line = f"    {extra}{el['ln']}  "
    print(f"{line}{el['txt']}")
