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="wc",
description="wc 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))
62 changes: 62 additions & 0 deletions implement-shell-tools/wc/wc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
import argparse

parser = argparse.ArgumentParser(
prog="wc",
description="wc implementation",
)

parser.add_argument("paths", nargs="+", help="The file to process")
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")
args = parser.parse_args()

total = {
"lines_counter": 0,
"words_counter": 0,
"characters_counter": 0,
}
for path in args.paths:

with open(path, "r") as f:
content = f.read()

line_counter = len(content.split("\n")) - 1
words_counter = len(content.split())
characters_counter = len(content)
if not args.l and not args.w and not args.c:
print(f"{line_counter} {words_counter} {characters_counter} {path}")
total["lines_counter"] += line_counter
total["words_counter"] += words_counter
total["characters_counter"] += characters_counter
else:
results = []
if args.l:
results.append(line_counter)
total["lines_counter"] += line_counter
if args.w:
results.append(words_counter)
total["words_counter"] += words_counter
if args.c:
results.append(characters_counter)
total["characters_counter"] += characters_counter
results = " ".join(map(str, results))
print(results, path)

if len(args.paths) > 1:
if not args.l and not args.w and not args.c:
print(
f"{total['lines_counter']} {total['words_counter']} {total['characters_counter']} total"
)

else:
total_with_flags = []

if args.l:
total_with_flags.append(total["lines_counter"])
if args.w:
total_with_flags.append(total["words_counter"])
if args.c:
total_with_flags.append(total["characters_counter"])
print(" ".join(map(str, total_with_flags))," total")

Loading