diff --git a/README.md b/README.md index 77f1b98..35ccfe5 100644 --- a/README.md +++ b/README.md @@ -2,14 +2,14 @@ nodejs wrapper for CLI related tasks [![Dev Status](https://img.shields.io/badge/DevStatus-Active-green.svg)](https://github.com/pushrocks/smartcli/commits/dev) -### Buildstatus/Dependencies +## Buildstatus/Dependencies [![Build Status](https://travis-ci.org/pushrocks/smartcli.svg?branch=master)](https://travis-ci.org/pushrocks/smartcli) [![devDependency Status](https://david-dm.org/pushrocks/smartcli/dev-status.svg)](https://david-dm.org/pushrocks/smartcli#info=devDependencies) -### Install the package +## Install the package npm install smartcli -### Usage +## Usage this plugin tries to establish some logic in which CLI tools work. @@ -24,11 +24,23 @@ mytool function argument1 argument2 --option1 option1Value --option2 option2Valu * option is an option you can add (like -m for message) * optionValue is the referenced option value (like a commit message) + +### The inner organization of smartcli +**smartcli** exposes three major groups of functions: + +* check functions + * are grouped in **smartcli.checks** object +* get functions + * are grouped in **smartcli.get** object +* async interaction functions + * are grouped in **smartcli.interaction** object + ```js var smartcli = require("smartcli"); +/* -------------- Check Functions -------------------*/ //returns true for terminal command "node myjs.js jazz" -smartcli.checkCommand('jazz'); +smartcli.check.command('jazz'); /** * returns an object for terminal command "node myjs.js --myoption something" like so @@ -38,7 +50,7 @@ smartcli.checkCommand('jazz'); * value: 'something' * } */ -smartcli.getOption('myoption'); +smartcli.get.option('myoption'); ``` Cheers diff --git a/index.js b/index.js index d7401ec..28bb86c 100644 --- a/index.js +++ b/index.js @@ -95,8 +95,8 @@ var SmartcliGetters; smartcli.get.command = function () { var cliCommand = { specified: smartcli.check.commandPresence(), - name: plugins.argv._[1], - arguments: smartcli.get.commandArguments(1) + name: plugins.argv._[0], + arguments: smartcli.get.commandArgs() }; return cliCommand; }; @@ -122,20 +122,30 @@ var SmartcliGetters; return commandArgument; }; /** - * + * returns array with commandArgs * @returns {CliCommandArgument[]} */ smartcli.get.commandArgs = function () { var commandArgs = []; - for (var command in plugins.argv._) - return commandArgs; + var argsArray = smartcli.get.commandArray().slice(0); + argsArray.shift(); + for (var item in argsArray) { + var commandArgument = { + specified: true, + name: argsArray[item], + level: item + 1 + }; + commandArgs.push(commandArgument); + } + return commandArgs; }; /** * returns complete command array * @returns {any} */ smartcli.get.commandArray = function () { - return plugins.argv._; + var commandArray = plugins.argv._; + return commandArray; }; /** * returns a cli option @@ -148,7 +158,7 @@ var SmartcliGetters; specified: false, value: false }; - if (plugins.argv.hasOwnProperty(optionName)) { + if (plugins.smartparam.exists(plugins.argv, optionName)) { cliOption.name = optionName; cliOption.specified = true; cliOption.value = plugins.argv[optionName]; //we already know from the "if" above that the value is available. diff --git a/test.js b/test.js index ec0acea..4c95e3b 100644 --- a/test.js +++ b/test.js @@ -79,6 +79,67 @@ checkCommandArgumentPresenceTest(); /* ------------------------------------------------------------------ * * ------------------- GETTERS TESTS -------------------------------- * * ------------------------------------------------------------------ */ +var getCommandTest = function () { + var cliCommand = smartcli.get.command(); + if (cliCommand.name == "jazz") { + bl.success('The specified command name is "jazz". Perfect!'); + } + else { + bl.error('The specified command name is not "jazz". Something is wrong!'); + process.exit(1); + } +}; +getCommandTest(); +var getCommandArgumentTest = function () { + var cliArgument = smartcli.get.commandArgument(1); + var cliArgument2 = smartcli.get.commandArgument(2); + if (cliArgument.name == "jam") { + bl.success('The first specified command argument name is "jam". Perfect!'); + } + else { + bl.error('The first specified command argument name is not "jam". Something is wrong!'); + process.exit(1); + } + if (cliArgument2.name == "undefined") { + bl.success('The second specified command argument name is "undefined". Perfect!'); + } + else { + bl.error('The second specified command argument name is not "undefined". Something is wrong!'); + process.exit(1); + } +}; +getCommandArgumentTest(); +var getCommandArgsTest = function () { + var commandArgs = smartcli.get.commandArgs(); + if (commandArgs[0].name == "jam") { + bl.success("first command argument is 'jam'. Perfect!"); + } + else { + bl.error("first command argument is not 'jam'. Something is wromg!"); + console.log(commandArgs[0].name); + process.exit(1); + } +}; +getCommandArgsTest(); +var getOptionTest = function () { + var cliOption = smartcli.get.option("awesome"); + var cliOption2 = smartcli.get.option("terrific"); + if (cliOption.specified) { + bl.success("awesome is specified. Perfect!"); + } + else { + bl.error("awesome is not specified. Somehthing is wrong"); + process.exit(1); + } + if (!cliOption2.specified) { + bl.success("terrific is not specified. Perfect!"); + } + else { + bl.error("terrific is specified. Somehthing is wrong"); + process.exit(1); + } +}; +getOptionTest(); var getCwdTest = function () { bl.info('The current directory is: ' + smartcli.get.cwd().path); }; diff --git a/ts/smartcli.getters.ts b/ts/smartcli.getters.ts index 5a6659c..2d7a400 100644 --- a/ts/smartcli.getters.ts +++ b/ts/smartcli.getters.ts @@ -10,8 +10,8 @@ module SmartcliGetters { smartcli.get.command = function():CliCommand { var cliCommand = { specified: smartcli.check.commandPresence(), - name: plugins.argv._[1], - arguments: smartcli.get.commandArguments(1) + name: plugins.argv._[0], + arguments: smartcli.get.commandArgs() } return cliCommand; }; @@ -39,12 +39,21 @@ module SmartcliGetters { }; /** - * + * returns array with commandArgs * @returns {CliCommandArgument[]} */ smartcli.get.commandArgs = function():CliCommandArgument[] { var commandArgs:CliCommandArgument[] = []; - for (var command in plugins.argv._) + var argsArray = smartcli.get.commandArray().slice(0); + argsArray.shift(); + for (var item in argsArray){ + var commandArgument:CliCommandArgument = { + specified:true, + name:argsArray[item], + level: item + 1 + } + commandArgs.push(commandArgument); + } return commandArgs; }; @@ -53,7 +62,8 @@ module SmartcliGetters { * @returns {any} */ smartcli.get.commandArray = function ():string[] { - return plugins.argv._; + var commandArray = plugins.argv._; + return commandArray; }; @@ -68,7 +78,7 @@ module SmartcliGetters { specified: false, value: false }; - if (plugins.argv.hasOwnProperty(optionName)) { + if (plugins.smartparam.exists(plugins.argv,optionName)) { cliOption.name = optionName; cliOption.specified = true; cliOption.value = plugins.argv[optionName] //we already know from the "if" above that the value is available. diff --git a/ts/test.ts b/ts/test.ts index cba325c..b0534b5 100644 --- a/ts/test.ts +++ b/ts/test.ts @@ -84,6 +84,67 @@ checkCommandArgumentPresenceTest(); /* ------------------------------------------------------------------ * * ------------------- GETTERS TESTS -------------------------------- * * ------------------------------------------------------------------ */ +var getCommandTest = function(){ + var cliCommand = smartcli.get.command(); + if(cliCommand.name == "jazz") { + bl.success('The specified command name is "jazz". Perfect!'); + } else { + bl.error('The specified command name is not "jazz". Something is wrong!'); + process.exit(1); + } + +}; +getCommandTest(); + +var getCommandArgumentTest = function() { + var cliArgument = smartcli.get.commandArgument(1); + var cliArgument2 = smartcli.get.commandArgument(2); + if(cliArgument.name == "jam") { + bl.success('The first specified command argument name is "jam". Perfect!'); + } else { + bl.error('The first specified command argument name is not "jam". Something is wrong!'); + process.exit(1); + } + + if(cliArgument2.name == "undefined") { + bl.success('The second specified command argument name is "undefined". Perfect!'); + } else { + bl.error('The second specified command argument name is not "undefined". Something is wrong!'); + process.exit(1); + } +}; +getCommandArgumentTest(); + +var getCommandArgsTest = function() { + var commandArgs = smartcli.get.commandArgs(); + if(commandArgs[0].name == "jam") { + bl.success("first command argument is 'jam'. Perfect!"); + } else { + bl.error("first command argument is not 'jam'. Something is wromg!"); + console.log(commandArgs[0].name); + process.exit(1); + } +}; +getCommandArgsTest(); + +var getOptionTest = function() { + var cliOption = smartcli.get.option("awesome"); + var cliOption2 = smartcli.get.option("terrific"); + if(cliOption.specified){ + bl.success("awesome is specified. Perfect!") + } else { + bl.error("awesome is not specified. Somehthing is wrong"); + process.exit(1); + } + if(!cliOption2.specified){ + bl.success("terrific is not specified. Perfect!") + } else { + bl.error("terrific is specified. Somehthing is wrong"); + process.exit(1); + } +}; +getOptionTest(); + var getCwdTest = function(){ bl.info('The current directory is: ' + smartcli.get.cwd().path); };