From 6ed1fbdb29439cf3b6f24f8a59b064d850f271be Mon Sep 17 00:00:00 2001 From: Oskar Grunning Date: Wed, 12 Dec 2018 16:23:15 +0100 Subject: [PATCH] fix: safe guard against no / wrong input This is missing a safe guard against cases where a user inputs a command that is missing. Currently there is no good way to solve with this commander.js. We need to implement https://github.com/sQVe/30s/issues/10 solve this. --- src/index.js | 33 +++++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/src/index.js b/src/index.js index 96cf524d..782068bf 100755 --- a/src/index.js +++ b/src/index.js @@ -26,13 +26,18 @@ const addCommand = settings => const addAction = action => [ 'action', - isTest - ? // NOTE: Commander.js sadly does not include a way to hook onto given - // actions for integration testing. We solve this by outputting state - // when NODE_ENV is test. - (input, { layout, json }) => - console.log (JSON.stringify ([action, input, layout, !!json])) - : actions[action], + (input, opts) => { + if (!input) return program.outputHelp () + + return isTest + ? // NOTE: Commander.js sadly does not include a way to hook onto given + // actions for integration testing. We solve this by outputting state + // when NODE_ENV is test. + console.log ( + JSON.stringify ([action, input, !!opts.cp, !!opts.json, opts.layout]) + ) + : actions[action] (input, opts) + }, ] const commonOptions = [ ['option', '-c, --cp', 'copy code to clipboard', false], @@ -71,10 +76,18 @@ program.on ('--help', () => [ '', 'Examples:', - ' view all', - ' search flatten --output json all', - ' tag --print ic array', + ' v head', + ' view head', + '', + ' s -j flatten', + ' search --json flatten', + '', + ' t -l ic array', + ' tag --layout ic array', ].join ('\n') ) ) program.parse (process.argv) +if (process.argv.length <= 2) { + program.outputHelp () +}