From 4b98427e78a9b5d9f1e8a85fcfe93702239ae374 Mon Sep 17 00:00:00 2001 From: Ahmad Hmedan Date: Sun, 26 Jul 2026 17:30:36 +0100 Subject: [PATCH 1/4] implement wc CLI --- implement-shell-tools/wc/wc.py | 47 ++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) create mode 100644 implement-shell-tools/wc/wc.py diff --git a/implement-shell-tools/wc/wc.py b/implement-shell-tools/wc/wc.py new file mode 100644 index 000000000..ca97a0d39 --- /dev/null +++ b/implement-shell-tools/wc/wc.py @@ -0,0 +1,47 @@ +import argparse + +parser = argparse.ArgumentParser(prog= "wc", + description="Count lines, words, and characters in one or more files.") + +parser.add_argument("-c",action="store_true",help="Count the number of characters.") +parser.add_argument("-w",action="store_true", help="Count the number of words.") +parser.add_argument("-l",action="store_true",help="Count the number of lines.") +parser.add_argument("paths",nargs="+",help="Path(s) to the file(s) to process.") + + +total_results = { + +} + + +args =parser.parse_args() +paths= args.paths; +no_flags = not args.c and not args.w and not args.l + +for path in paths: + with open(path, "r") as f: + content = f.read() + + count_lines = len(content.splitlines()) + count_words = len(content.split()) + count_characters = len(content) + + results = {} + + if no_flags or args.l: + results["count_lines"] = count_lines + + if no_flags or args.w: + results["count_words"] = count_words + + if no_flags or args.c: + results["count_characters"] = count_characters + + for key, value in results.items(): + total_results[key] = total_results.get(key, 0) + value + + print(" ".join(map(str, results.values())), path) + +if len(paths) > 1: + print(" ".join(map(str, total_results.values())), "total") + \ No newline at end of file From 0d43179e209697d8425a0749203b2f2be727b794 Mon Sep 17 00:00:00 2001 From: Ahmad Hmedan Date: Sun, 26 Jul 2026 23:54:13 +0100 Subject: [PATCH 2/4] feat: implement ls --- implement-shell-tools/ls/ls.py | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) create mode 100644 implement-shell-tools/ls/ls.py diff --git a/implement-shell-tools/ls/ls.py b/implement-shell-tools/ls/ls.py new file mode 100644 index 000000000..e0478f16d --- /dev/null +++ b/implement-shell-tools/ls/ls.py @@ -0,0 +1,29 @@ +import argparse +from pathlib import Path + +parser = argparse.ArgumentParser( + prog="ls", + description="ls implementation", +) + +parser.add_argument("path", nargs="?", help="The file to process", default=".") +parser.add_argument( + "-1", "--one_per_line", action="store_true", help="one file per line" +) +parser.add_argument("-a", action="store_true", help="Show hidden files") + + +args = parser.parse_args() + +path = Path(args.path) + +items = [] +for item in sorted(path.iterdir()): + if args.a or not item.name.startswith("."): + items.append(item.name) + +if args.one_per_line: + for name in items: + print(name) +else: + print(" ".join(items)) From e609ff3b97905c62e8dc1d3d31430051bc8623aa Mon Sep 17 00:00:00 2001 From: Ahmad Hmedan Date: Sun, 26 Jul 2026 23:54:32 +0100 Subject: [PATCH 3/4] style: --- implement-shell-tools/wc/wc.py | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/implement-shell-tools/wc/wc.py b/implement-shell-tools/wc/wc.py index ca97a0d39..d5cf69d89 100644 --- a/implement-shell-tools/wc/wc.py +++ b/implement-shell-tools/wc/wc.py @@ -1,21 +1,20 @@ import argparse -parser = argparse.ArgumentParser(prog= "wc", - description="Count lines, words, and characters in one or more files.") +parser = argparse.ArgumentParser( + prog="wc", description="Count lines, words, and characters in one or more files." +) -parser.add_argument("-c",action="store_true",help="Count the number of characters.") -parser.add_argument("-w",action="store_true", help="Count the number of words.") -parser.add_argument("-l",action="store_true",help="Count the number of lines.") -parser.add_argument("paths",nargs="+",help="Path(s) to the file(s) to process.") +parser.add_argument("-c", action="store_true", help="Count the number of characters.") +parser.add_argument("-w", action="store_true", help="Count the number of words.") +parser.add_argument("-l", action="store_true", help="Count the number of lines.") +parser.add_argument("paths", nargs="+", help="Path(s) to the file(s) to process.") -total_results = { - -} +total_results = {} -args =parser.parse_args() -paths= args.paths; +args = parser.parse_args() +paths = args.paths no_flags = not args.c and not args.w and not args.l for path in paths: @@ -44,4 +43,3 @@ if len(paths) > 1: print(" ".join(map(str, total_results.values())), "total") - \ No newline at end of file From 801138c3b45018028ab5940f7fa66e619a98c6c4 Mon Sep 17 00:00:00 2001 From: Ahmad Hmedan Date: Mon, 27 Jul 2026 00:08:50 +0100 Subject: [PATCH 4/4] feat:implement cat --- implement-shell-tools/cat/cat.py | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) create mode 100644 implement-shell-tools/cat/cat.py diff --git a/implement-shell-tools/cat/cat.py b/implement-shell-tools/cat/cat.py new file mode 100644 index 000000000..9cefdff51 --- /dev/null +++ b/implement-shell-tools/cat/cat.py @@ -0,0 +1,28 @@ +import argparse + +parser = argparse.ArgumentParser( + prog="cat", + description="implement cat", +) + +parser.add_argument("paths", nargs="+", help="The file to process") +parser.add_argument("-n", action="store_true", help="Number all lines") +parser.add_argument("-b", action="store_true", help="Number non-empty lines only") + +args = parser.parse_args() +counter = 1 + +for path in args.paths: + with open(path, "r") as f: + for line in f: + if args.n: + print(f"\t{counter} {line}", end="") + counter += 1 + elif args.b: + if line.strip() != "": + print(f"\t{counter} {line}", end="") + counter += 1 + else: + print(f"\t {line}", end="") + else: + print(f" {line}", end="")