Implement CLI tool that will encode and decode a text by Caesar cipher.
CLI tool should accept 4 options (short alias and full name):
- -s, --shift: a shift
- -i, --input: an input file
- -o, --output: an output file
- -a, --action: an action encode/decode
Basic Scope
- For command-line arguments could be used one of
- https://www.npmjs.com/package/commander
- https://www.npmjs.com/package/minimist
- any other similar module.
Action(encode/decode) and theshiftare required, if one of them missed - an error should be shown, the process should exit with non-zero status code.- If the input file is missed - use
stdinas an input source. - If the output file is missed - use
stdoutas an output destination. - If the input and/or output file is given but doesn't exist or you can't read it (e.g. because of permissions or it is a directory) - human-friendly error should be printed in
stderr. - If passed params are fine the output (file or
stdout) should contain encoded/decoded content of input (file orstdin). - For encoding/decoding use only the English alphabet, all other characters should be kept untouched.
- Using
streamsfor reading, writing and transformation of text is mandatory. - The tool must work correctly with an integer values of
shift (-s, --shift)that are greater than alphabet length. - If any option is duplicated (i.e.
bash $ node my_caesar_cli -a encode -s 7 --shift 7) then human-friendly error should be printed instderr.
Advanced Scope
- The tool must work correctly with an integer values of
shift (-s, --shift)that are lower than zero.
Hints: As suggested solution to make streams code more robust, and memory effective, consider to use pipeline method. Structure can be the following:
pipeline(
input_stream, // input file stream or stdin stream
transform_stream, // Transform stream
output_stream // output file stream or stdout stream
)
.then(success and error callbacks)Usage example:
- -a (--action) is encode
$ node my_caesar_cli -a encode -s 7 -i "./input.txt" -o "./output.txt"input.txt
This is secret. Message about "_" symbol!
output.txt
Aopz pz zljyla. Tlzzhnl hivba "_" zftivs!
$ node my_caesar_cli --action encode --shift 7 --input plain.txt --output encoded.txtplain.txt
This is secret. Message about "_" symbol!
encoded.txt
Aopz pz zljyla. Tlzzhnl hivba "_" zftivs!
- -a (--action) is decode
Decoding encoded initial string with the same -s(--shift) number produces the initial string.
$ node my_caesar_cli --action decode --shift 7 --input encoded.txt --output plain.txtencoded.txt
Aopz pz zljyla. Tlzzhnl hivba "_" zftivs!
plain.txt
This is secret. Message about "_" symbol!
- (Optional) Negative shift handling
$ node my_caesar_cli --action encode --shift -1 --input plain.txt --output encoded.txtplain.txt
This is secret. Message about "_" symbol!
encoded.txt
Sghr hr rdbqds. Ldrrzfd zants "_" rxlank!