|
| 1 | +# cat command built in Python |
| 2 | +# Note: Does not work with multiple arguments (wip) |
| 3 | + |
| 4 | +#!/usr/bin/env python |
| 5 | +import getopt |
| 6 | +import sys |
| 7 | +import os |
| 8 | +import re |
| 9 | + |
| 10 | +argumentList = sys.argv[1:] |
| 11 | + |
| 12 | +options = "bEnsThv:" |
| 13 | + |
| 14 | +long_options = ['--number-nonblank', '--show-ends', '--number', '--squeeze-blank', '--show-tabs', '--help', '--version'] |
| 15 | + |
| 16 | +try: |
| 17 | + arguments, values = getopt.getopt(argumentList, options, long_options) |
| 18 | + for currentArgument, currentValue in arguments: |
| 19 | + if currentArgument in ('-h', '--help'): |
| 20 | + |
| 21 | + sys.stdout.write("Usage: cat [OPTION].. [FILE]... \n" + |
| 22 | + "Concatenate FILE(s) to standard output.\n" + |
| 23 | + "When FILE is -, read standard input.\n" + |
| 24 | + "\n" + |
| 25 | + " -b, --number-nonblank number nonempty output lines, overrides -n \n" + |
| 26 | + " -E, --show-ends display $ at end of each line \n" + |
| 27 | + " -n, --number number all output lines \n" + |
| 28 | + " -s, --squeeze-blank suppress repeated empty output lines \n" + |
| 29 | + " -T, --show-tabs display TAB characters as ^I \n" + |
| 30 | + " -h, --help display this help and exit \n" + |
| 31 | + " -v, --version output version information and exit \n" + |
| 32 | + "\n" + |
| 33 | + "Examples: \n" + |
| 34 | + " cat f - g Output f's contents, then standard input, then g's contents. \n" + |
| 35 | + " cat Copy standard input to standard output. \n" + |
| 36 | + "\n" + |
| 37 | + "GNU coreutils online help: <https://www.gnu.org/software/coreutils/> \n" |
| 38 | + "Full documentation <https://www.gnu.org/software/coreutils/cat> \n" + |
| 39 | + "or available locally via: info \'(coreutils) cat invocation\' \n") |
| 40 | + raise SystemExit |
| 41 | + |
| 42 | + elif currentArgument in ('-v', '--version'): |
| 43 | + |
| 44 | + sys.stdout.write("cat (GNU coreutils) 8.32\n" + |
| 45 | + "Copyright (C) 2020 Free Software Foundation, Inc.\n" + |
| 46 | + "License GPLv3+: GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.\n" + |
| 47 | + "This is free software: you are free to change and redistribute it.\n" + |
| 48 | + "There is NO WARRANTY, to the extent permitted by law.\n" + |
| 49 | + "\n" + |
| 50 | + "Written by Torbjorn Granlund and Richard M. Stallman.") |
| 51 | + raise SystemExit |
| 52 | + |
| 53 | + elif currentArgument in ('-n', '--number'): |
| 54 | + |
| 55 | + with open(sys.argv[-1], 'r') as f: |
| 56 | + count = 0 |
| 57 | + for line in f: |
| 58 | + count += 1 |
| 59 | + sys.stdout.write(f' {count} {line}') |
| 60 | + raise SystemExit |
| 61 | + |
| 62 | + elif currentArgument in ('-b', '--number-noblank'): |
| 63 | + |
| 64 | + with open(sys.argv[-1], 'r') as f: |
| 65 | + count = 0 |
| 66 | + for line in f: |
| 67 | + if line == "\n": |
| 68 | + sys.stdout.write("\n") |
| 69 | + else: |
| 70 | + count += 1 |
| 71 | + sys.stdout.write(f' {count} {line}') |
| 72 | + raise SystemExit |
| 73 | + |
| 74 | + elif currentArgument in ('-E', '--show-ends'): |
| 75 | + |
| 76 | + with open(sys.argv[-1], 'r') as f: |
| 77 | + for line in f: |
| 78 | + line = line.rstrip('\n') + '$' |
| 79 | + sys.stdout.write(line+'\n') |
| 80 | + raise SystemExit |
| 81 | + |
| 82 | + elif currentArgument in ('-s', '--squeeze-blank'): |
| 83 | + |
| 84 | + with open(sys.argv[-1], 'r') as f: |
| 85 | + sys.stdout.write(re.sub(r'\n+','\n',f.read())) |
| 86 | + raise SystemExit |
| 87 | + |
| 88 | + elif currentArgument in ('-T', '--show-tabs'): |
| 89 | + |
| 90 | + with open(sys.argv[-1], 'r') as f: |
| 91 | + sys.stdout.write(re.sub(r'\t','^I',f.read())) |
| 92 | + raise SystemExit |
| 93 | + |
| 94 | +except getopt.error as err: |
| 95 | + print(str(err)) |
| 96 | + |
| 97 | +for i in argumentList: |
| 98 | + |
| 99 | + if i == '-': |
| 100 | + for line in sys.stdin: |
| 101 | + sys.stdout.write(line) |
| 102 | + |
| 103 | + if not os.path.exists(i): |
| 104 | + sys.exit(f'File does not exsist: {i}') |
| 105 | + |
| 106 | + with open(i, 'r') as f: |
| 107 | + sys.stdout.write(f.read()) |
| 108 | + |
| 109 | + f.close() |
0 commit comments