#!/usr/bin/env python
# Exercise 10
# sort command

import sys
import argparse
from pathlib import Path


def modify(lines, args, name):
    if name in args.keys() and args[name] is True:
        for i, line in enumerate(lines):
            match name:
                case "ignore_leading_blanks":
                    lines[i]["mut"] = lines[i]["mut"].strip()
                case "ignore_case":
                    lines[i]["mut"] = lines[i]["mut"].lower()
                case "numeric_sort":
                    if lines[i]["mut"].isnumeric():
                        lines[i]["mut"] = int(lines[i]["mut"])


parser = argparse.ArgumentParser()
parser.add_argument("stream", default=sys.stdin, nargs="*")
parser.add_argument("-f", "--ignore-case", action="store_true")
parser.add_argument("-b", "--ignore-leading-blanks", action="store_true")
parser.add_argument("-n", "--numeric-sort", action="store_true")
parser.add_argument("-r", "--reverse", action="store_true")
args = parser.parse_args().__dict__
lines = []

for stream in args["stream"]:
    if isinstance(args["stream"], list):
        for line in Path(stream).open().readlines():
            lines.append({"fix": line.rstrip(), "mut": line.rstrip()})
    else:
        lines.append({"fix": stream.strip(), "mut": stream.strip()})

modify(lines, args, "ignore_leading_blanks")
modify(lines, args, "ignore_case")
modify(lines, args, "numeric_sort")

lines = sorted(lines, key=lambda k: k["mut"])

if args["reverse"]:
    lines.reverse()

for line in lines:
    print(line["fix"])
