diff --git a/README.md b/README.md index 0d299c4..2d36ed7 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,30 @@ # hashscript A hash for use in written text. Read [Software Design Document](docs/sdd/Software-Design-Document.pdf). +## command line examples + +### command line example - hash notation +```sh +secret='CaptainJackSparrow' +message='First, I counted to $A, then $B, and then $C.' + +# display script with dynamic vars +src/hashscript-cli.js script-vars "{$secret}$message" + +# hash it +hash=$(src/hashscript-cli.js hash "{$secret}$message" 2>>/dev/null) +echo "$hash" + +# display script with encoded digits +src/hashscript-cli.js script-nums -h "$hash" "{$secret}$message" +``` + +``` +First, I counted to $A, then $B, and then $C. +4 2 10 +First, I counted to 4, then 2, and then 10. +``` + ## generate SDD ```sh cd docs/sdd diff --git a/src/hashscript-cli.js b/src/hashscript-cli.js new file mode 100755 index 0000000..5a2ed20 --- /dev/null +++ b/src/hashscript-cli.js @@ -0,0 +1,55 @@ +#!/usr/bin/env node + +function getsec(text) { + return text.split("}")[0] + "}"; +} +function getmsg(text) { + return text.split("}")[1]; +} + +function runmodule(name, cb) { + const modulespec = { hash: "./hash.js", roundtrip: "./roundTrip.js" }; + import(modulespec[name]).then((module) => { + cb(module); + }); +} + +function main(args) { + const cmdspec = { + hash: "hash", + scriptvars: "script-vars", + scriptnums: "script-nums", + }; + const subcmd = args.shift(); + var scriptVars = false; + switch (subcmd) { + case cmdspec.hash: + runmodule(subcmd, (hashModule) => { + console.log( + hashModule.default + .hashInferPluralDigitCsv(getsec(args[0]), getmsg(args[0])) + .replace(/,/g, " ") + ); + }); + break; + case cmdspec.scriptvars: + scriptVars = true; + //fall-through + case cmdspec.scriptnums: + var hash = null; + var text = args[0]; + if (!scriptVars) { + args.shift(); // -h + hash = args.shift(); + text = args.shift(); + } + runmodule("roundtrip", (roundTripModule) => { + console.log( + roundTripModule.roundTrip(text, hash, scriptVars).split("}")[1] + ); + }); + break; + } +} + +main(process.argv.slice(2));