Skip to content
26 changes: 26 additions & 0 deletions implement-cowsay/cow.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import argparse
import cowsay


def main():
parser = argparse.ArgumentParser(description="Make animals say things")
parser.add_argument(
"--animal",
choices=cowsay.char_names,
default="cow",
help="The animal to be saying things."
)
parser.add_argument(
"message",
nargs="+",
help="The message to say."
)

args = parser.parse_args()
message = " ".join(args.message)

print(cowsay.get_output_string(args.animal, message))


if __name__ == "__main__":
main()
34 changes: 34 additions & 0 deletions implement-shell-tools/cat/cat.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
const fs = require("fs");

const args = process.argv.slice(2);

const showLineNumbers = args.includes("-n");
const numberNonEmptyLines = args.includes("-b");

const files = args.filter(arg => arg !== "-n" && arg !== "-b");

for (const file of files) {
const content = fs.readFileSync(file, "utf8");

if (numberNonEmptyLines) {
const lines = content.split("\n");
let lineNumber = 1;

lines.forEach((line) => {
if (line.trim() !== "") {
console.log(`${lineNumber} ${line}`);
lineNumber++;
} else {
console.log(line);
}
});
} else if (showLineNumbers) {
const lines = content.split("\n");

lines.forEach((line, index) => {
console.log(`${index + 1} ${line}`);
});
} else {
process.stdout.write(content);
}
}
21 changes: 21 additions & 0 deletions implement-shell-tools/ls/ls.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
const fs = require("fs");

const args = process.argv.slice(2);

const showAll = args.includes("-a");

// remove -a from arguments
const filteredArgs = args.filter(arg => arg !== "-a");

const target = filteredArgs[0] || ".";

let files = fs.readdirSync(target);

// hide hidden files if -a is NOT used
if (!showAll) {
files = files.filter(file => !file.startsWith("."));
}

files.forEach(file => {
console.log(file);
});
29 changes: 29 additions & 0 deletions implement-shell-tools/wc/wc.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
const fs = require("fs");

const args = process.argv.slice(2);

const showLines = args.includes("-l");
const showWords = args.includes("-w");
const showChars = args.includes("-c");

const files = args.filter(
(arg) => arg !== "-l" && arg !== "-w" && arg !== "-c"
);

for (const file of files) {
const content = fs.readFileSync(file, "utf8");

const lines = content.split("\n").length;
const words = content.trim().split(/\s+/).length;
const chars = content.length;

if (showLines) {
console.log(lines, file);
} else if (showWords) {
console.log(words, file);
} else if (showChars) {
console.log(chars, file);
} else {
console.log(lines, words, chars, file);
Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe here if the first condition is True the if loop breaks

}
}