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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
node_modules
demo*
.*
38 changes: 38 additions & 0 deletions implement-shell-tools/cat/cat.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env python3

import argparse
import sys

parser = argparse.ArgumentParser(
prog='cat',
description='CLI tool to concatenate and print files',
)

parser.add_argument('-n', '--number', action='store_true', help='Number all outputs, starting at 1')
parser.add_argument('-b', '--nonBlank', action='store_true', help='Number only non-blank lines, starting at 1')
parser.add_argument('files', nargs='*', help='Files to read')

args = parser.parse_args()
# print(args.number, args.nonBlank, args.files)
# print(sys.argv[0], sys.argv[1])


for file in args.files:
count = 1
try:
with open(file, 'r') as f:
for line in f:
if args.nonBlank:
if line.strip():
print(f"{str(count).rjust(6)}\t{line}", end='')
count += 1
else:
print(line, end="")
elif args.number:
print(f"{str(count).rjust(6)}\t{line}", end='')
count +=1
else:
print(f"{line}", end="")

except FileNotFoundError:
print(f"cat: {file}: No such file or directory.", file=sys.stderr)
34 changes: 34 additions & 0 deletions implement-shell-tools/ls/ls.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#!/usr/bin/env python3

import argparse
import os
import sys

parser = argparse.ArgumentParser(
prog='ls',
description='CLI tool to list contents of a directory'
)

parser.add_argument('-1', '--one', action='store_true', help='Force output to be entry per line')
parser.add_argument('-a','--all', action='store_true', help='Include hidden files')
parser.add_argument('path', nargs='?', default='.', help='Directories to list')

args = parser.parse_args()

try:
entries = os.listdir(args.path)
# print(entries)
if not args.all:
entries = [entry for entry in entries if not entry.startswith('.')]
entries.sort(key=str.lower)
# print(entries)

if args.one:
for entry in entries:
print(entry)

else:
print(f" ".join(entries))

except FileNotFoundError:
print(f"ls: {args.path}: No such file or directory.", file=sys.stderr)
63 changes: 63 additions & 0 deletions implement-shell-tools/wc/wc.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#!/usr/bin/env python3

import argparse
import sys
import os

parser = argparse.ArgumentParser(
prog='wc',
description='Displays number of words, lines, and bytes in a file'
)

parser.add_argument('-l', '--lines', action='store_true', help='counts number of lines')
parser.add_argument('-w', '--words', action='store_true', help='counts sequence of characters')
parser.add_argument('-c', '--bytes', action='store_true', help='counts raw size of a file in bytes')
parser.add_argument('files', nargs='+', help='Files to read')

args = parser.parse_args()

if not args.files:
print(f"wc: missing file to read")
sys.exit(1)

total_lines, total_words, total_bytes = 0, 0, 0

for file in args.files:
try:
with open(file, 'r') as f:
content = f.read()

line_count = content.count('\n')
word_count = len(content.split())
byte_count = os.path.getsize(file)

total_lines += line_count
total_words += word_count
total_bytes += byte_count

show_all = not (args.lines or args.words or args.bytes)

output = ""
if args.lines or show_all:
output += f"{line_count:>7} "
if args.words or show_all:
output += f"{word_count:>7} "
if args.bytes or show_all:
output += f"{byte_count:>7} "

print(f"{output} {file}")

except FileNotFoundError:
print(f"wc: {args.path}: No such file or directory.", file=sys.stderr)


if len(args.files) > 1:
total = ""
if args.lines or show_all:
total += f"{total_lines:>7} "
if args.words or show_all:
total += f"{total_words:>7} "
if args.bytes or show_all:
total += f"{total_bytes:>7} "

print(f"{total}total")
Loading