Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions implement-shell-tools/cat/cat.py
Original file line number Diff line number Diff line change
@@ -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="")
29 changes: 29 additions & 0 deletions implement-shell-tools/ls/ls.py
Original file line number Diff line number Diff line change
@@ -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))
45 changes: 45 additions & 0 deletions implement-shell-tools/wc/wc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
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")
Loading