diff --git a/implement-cowsay/cow.py b/implement-cowsay/cow.py new file mode 100644 index 000000000..7aff7f3b9 --- /dev/null +++ b/implement-cowsay/cow.py @@ -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() \ No newline at end of file diff --git a/implement-shell-tools/cat/cat.js b/implement-shell-tools/cat/cat.js new file mode 100644 index 000000000..2da8c591d --- /dev/null +++ b/implement-shell-tools/cat/cat.js @@ -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); + } +} \ No newline at end of file diff --git a/implement-shell-tools/ls/ls.js b/implement-shell-tools/ls/ls.js new file mode 100644 index 000000000..341ab34a2 --- /dev/null +++ b/implement-shell-tools/ls/ls.js @@ -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); +}); \ No newline at end of file diff --git a/implement-shell-tools/wc/wc.js b/implement-shell-tools/wc/wc.js new file mode 100644 index 000000000..ee89ff398 --- /dev/null +++ b/implement-shell-tools/wc/wc.js @@ -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); + } +} \ No newline at end of file