From ce1dde6d5447659ca2311876d11f47307d4a36c1 Mon Sep 17 00:00:00 2001 From: Phil Kunz Date: Wed, 7 Oct 2015 01:07:38 +0200 Subject: [PATCH] now handling CLI options --- index.js | 32 ++++++++++++++++++++++++++++++++ test.js | 15 +++++++++++++++ ts/index.ts | 45 +++++++++++++++++++++++++++++++++++++++++++++ ts/test.ts | 18 ++++++++++++++++++ 4 files changed, 110 insertions(+) diff --git a/index.js b/index.js index c7fcc85..2d69c1f 100644 --- a/index.js +++ b/index.js @@ -10,6 +10,35 @@ var smartcli = {}; smartcli.inquirer = inquirer; smartcli.cliff = cliff; smartcli.argv = argv; +/* ------------------------------------------------------------------------------ + *----------------------- initial call CLI args ----------------------------- + *----------------------------------------------------------------------------- */ +// commands +smartcli.checkCommand = function (commandString) { + if (argv._.indexOf(commandString) != -1) { + return true; + } + return false; +}; +smartcli.getCommands = function () { + return argv._; +}; +// options +smartcli.checkOption = function (optionParam) { + if (argv.hasOwnProperty(optionParam)) { + return true; + } + return false; +}; +smartcli.getOptions = function () { + var options = {}; + for (var key in argv) { + if (key != "_") { + options['key'] = argv['key']; + } + } + return options; +}; /** * returns the current working directory * @returns {string} @@ -17,6 +46,9 @@ smartcli.argv = argv; smartcli.getCwd = function () { return process.cwd(); }; +/* ------------------------------------------------------------------------------ +*----------------------- in program CLI interaction ----------------------------- +*----------------------------------------------------------------------------- */ /** * executes callback with answer to question as argument * @param questionString the question you want to ask the user diff --git a/test.js b/test.js index 2e63d4e..06a3aa7 100644 --- a/test.js +++ b/test.js @@ -1,5 +1,20 @@ /// var smartcli = require("./index.js"); +var bl = require("beautylog"); +bl.log('now starting Test'); +bl.log('starting with initial CLI commands and options'); +var commandsString = 'You specified the following commands:'; +var commands = smartcli.getCommands(); +for (var key in commands) { + commandsString = commandsString + ' ' + commands[key]; +} +bl.log(commandsString); +if (smartcli.checkCommand('jazz')) { + bl.log('One of your commands is jazz'); +} +else { + bl.log('None of your commands is jazz'); +} var getAnswerTest = function () { smartcli.getAnswer('How do you feel?', function (answer) { console.log('The answer is: ' + answer); diff --git a/ts/index.ts b/ts/index.ts index 2257510..a276cd1 100644 --- a/ts/index.ts +++ b/ts/index.ts @@ -14,6 +14,43 @@ smartcli.inquirer = inquirer; smartcli.cliff = cliff; smartcli.argv = argv; +/* ------------------------------------------------------------------------------ + *----------------------- initial call CLI args ----------------------------- + *----------------------------------------------------------------------------- */ + +// commands + + +smartcli.checkCommand = function(commandString:string):boolean { + if (argv._.indexOf(commandString) != -1) { + return true + } + return false; +}; + +smartcli.getCommands = function ():string[] { + return argv._; +}; + + +// options +smartcli.checkOption = function(optionParam:string):boolean { + if (argv.hasOwnProperty(optionParam)) { + return true; + } + return false +} + +smartcli.getOptions = function() { + var options = {}; + for (var key in argv) { + if (key != "_") { + options['key'] = argv['key']; + } + } + return options; +}; + /** * returns the current working directory * @returns {string} @@ -22,6 +59,14 @@ smartcli.getCwd = function () { return process.cwd(); }; + + +/* ------------------------------------------------------------------------------ +*----------------------- in program CLI interaction ----------------------------- +*----------------------------------------------------------------------------- */ + + + /** * executes callback with answer to question as argument * @param questionString the question you want to ask the user diff --git a/ts/test.ts b/ts/test.ts index e809186..b4bd533 100644 --- a/ts/test.ts +++ b/ts/test.ts @@ -1,5 +1,23 @@ /// var smartcli = require("./index.js"); +var bl = require("beautylog"); + +bl.log('now starting Test'); +bl.log('starting with initial CLI commands and options'); + +var commandsString:string = 'You specified the following commands:'; +var commands = smartcli.getCommands(); +for (var key in commands) { + commandsString = commandsString + ' ' + commands[key]; +} +bl.log(commandsString); + +if (smartcli.checkCommand('jazz')) { + bl.log('One of your commands is jazz'); +} else { + bl.log('None of your commands is jazz'); +} + var getAnswerTest = function() { smartcli.getAnswer('How do you feel?',function(answer){