From 67a4522f56859565f6d6624ffdd4f0f0587fd178 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladimir=20Vrzi=C4=87?= Date: Fri, 24 Feb 2023 18:10:13 +0100 Subject: [PATCH] Added check for correct command line arguments --- b2sum.js | 13 +++++++++---- 1 file changed, 9 insertions(+), 4 deletions(-) diff --git a/b2sum.js b/b2sum.js index a2594bf..1bcb7a2 100755 --- a/b2sum.js +++ b/b2sum.js @@ -3,23 +3,28 @@ /** * Takes an algorithm name and prints the hex digest of stdin. * Example: - * cat file | ./b2sum.js blake2b + * $ node b2sum.js blake2b < file */ "use strict"; -const blake2 = require('./index'); +if (process.argv.length === 2) { + const EXIT_USAGE = 1; + const progname = process.argv[0].concat(' ', process.argv[1]); + const usageMsg = `Usage: ${progname} `; + console.error(usageMsg); + process.exit(EXIT_USAGE); +} const algo = process.argv[2]; +const blake2 = require('./index'); const hash = new blake2.Hash(algo); hash.setEncoding('hex'); const stream = process.stdin; - stream.on('end', function() { hash.end(); const digest = hash.read(); console.log(digest); }); - stream.pipe(hash);