diff --git a/.gitignore b/.gitignore index 3c3629e64..8f9f6b3a4 100644 --- a/.gitignore +++ b/.gitignore @@ -1 +1,6 @@ node_modules +.DS_Store +__pycache__/ +*.pyc +venv/ +type/ diff --git a/implement-shell-tools/cat/.gitignore b/implement-shell-tools/cat/.gitignore new file mode 100644 index 000000000..d6357197e --- /dev/null +++ b/implement-shell-tools/cat/.gitignore @@ -0,0 +1,4 @@ +.DS_Store +__pycache__/ +*.pyc +venv/ diff --git a/implement-shell-tools/cat/cat.py b/implement-shell-tools/cat/cat.py new file mode 100644 index 000000000..3077ec3e4 --- /dev/null +++ b/implement-shell-tools/cat/cat.py @@ -0,0 +1,33 @@ +import argparse + + +def run(args): + for file_path in args.path: + counter_number = 1 + + with open(file_path, "r") as f: + lines = f.readlines() + for line in lines: + prefix = "" + if args.n or (args.b and line.strip() != ""): + prefix = counter_number + counter_number += 1 + print(f"{prefix} {line}", end="") + + +def main(): + parser = argparse.ArgumentParser( + prog="my-cat", + description="Simple cat clone with -n and -b options", + ) + + parser.add_argument("-n", action="store_true", help="number all lines") + parser.add_argument("-b", action="store_true", help="The character to search for") + parser.add_argument("path", nargs="+", help="The file to search") + + args = parser.parse_args() + run(args) + + +if __name__ == "__main__": + main() diff --git a/implement-shell-tools/ls/ls.py b/implement-shell-tools/ls/ls.py new file mode 100644 index 000000000..aae3ff508 --- /dev/null +++ b/implement-shell-tools/ls/ls.py @@ -0,0 +1,25 @@ +import argparse +import os + + +def show_files(files, show_hidden): + for file in files: + if show_hidden or not file.startswith("."): + print(file) + + +parser = argparse.ArgumentParser( + prog="my-ls", + description="Simple ls clone with -a and -l options", +) + +parser.add_argument("-a", action="store_true", help="include hidden files") +parser.add_argument( + "-1", dest="one", action="store_true", help="list one entry per line" +) +parser.add_argument("path", nargs="?", default=".", help="The directory to list") +args = parser.parse_args() + + +directory_path = args.path +listDir = os.listdir(directory_path) diff --git a/implement-shell-tools/wc/wc.py b/implement-shell-tools/wc/wc.py new file mode 100644 index 000000000..aa8a2e86f --- /dev/null +++ b/implement-shell-tools/wc/wc.py @@ -0,0 +1,57 @@ +import argparse + +parser = argparse.ArgumentParser( + prog="my-wc", + description="Simple wc clone with -l and -w options", +) + + +def read_file(file_path): + with open(file_path, "r") as file: + return file.read() + + +def count_text(text): + line_count = text.count("\n") + word_count = len(text.split()) + char_count = len(text) + return line_count, word_count, char_count + + +lineCounter = 0 +wordCounter = 0 +charCounter = 0 + +parser.add_argument("-l", action="store_true", help="count lines") +parser.add_argument("-w", action="store_true", help="count words") +parser.add_argument("-c", action="store_true", help="count characters") +parser.add_argument("path", nargs="+", default=".", help="The file to count") +args = parser.parse_args() + + +for path in args.path: + text = read_file(path) + lines, words, chars = count_text(text) + + lineCounter += lines + wordCounter += words + charCounter += chars + + if args.l: + print(lines, path) + elif args.w: + print(words, path) + elif args.c: + print(chars, path) + else: + print(lines, words, chars, path) + +if len(args.path) > 1: + if args.l: + print(lineCounter, "total") + elif args.w: + print(wordCounter, "total") + elif args.c: + print(charCounter, "total") + else: + print(lineCounter, wordCounter, charCounter, "total")