improved return objects

This commit is contained in:
Phil Kunz 2015-10-14 20:59:01 +02:00
parent fd7352533f
commit 09152c8f5f
6 changed files with 99 additions and 39 deletions

View File

@ -1,4 +1,6 @@
/// <reference path="index.ts" />
/// <reference path="typings/tsd.d.ts" /> /// <reference path="typings/tsd.d.ts" />
/// <reference path="./interfaces.ts" />
var path = require("path"); var path = require("path");
var beautylog = require("beautylog"); var beautylog = require("beautylog");
var cliff = require("cliff"); var cliff = require("cliff");
@ -24,11 +26,19 @@ smartcli.getCommands = function () {
return argv._; return argv._;
}; };
// options // options
smartcli.checkOption = function (optionParam) { smartcli.getOption = function (optionName) {
if (argv.hasOwnProperty(optionParam)) { if (argv.hasOwnProperty(optionName)) {
return true; return {
name: optionName,
specified: true,
value: argv[optionName] //we already know from the "if" above that the value is available.
};
} }
return false; return {
name: optionName,
specified: false,
value: false
};
}; };
smartcli.getOptions = function () { smartcli.getOptions = function () {
var options = {}; var options = {};
@ -40,11 +50,13 @@ smartcli.getOptions = function () {
return options; return options;
}; };
/** /**
* returns the current working directory * returns Directory of cwd
* @returns {string} * @returns {{path: string}}
*/ */
smartcli.getCwd = function () { smartcli.getCwd = function () {
return process.cwd(); return {
path: process.cwd()
};
}; };
/* ------------------------------------------------------------------------------ /* ------------------------------------------------------------------------------
*----------------------- in program CLI interaction ----------------------------- *----------------------- in program CLI interaction -----------------------------

View File

@ -5,6 +5,7 @@
"main": "index.js", "main": "index.js",
"scripts": { "scripts": {
"test": "(cd ts/compile && gulp)", "test": "(cd ts/compile && gulp)",
"devTest": "(npm test) && (node test.js --test true)",
"reinstall": "(rm -r node_modules && npm install)", "reinstall": "(rm -r node_modules && npm install)",
"release": "(git pull origin master && npm version patch && git push origin master && git checkout release && git merge master && git push origin release && git checkout master)", "release": "(git pull origin master && npm version patch && git push origin master && git checkout release && git merge master && git push origin release && git checkout master)",
"startdev": "(git checkout master && git pull origin master)" "startdev": "(git checkout master && git pull origin master)"

20
test.js
View File

@ -9,12 +9,23 @@ for (var key in commands) {
commandsString = commandsString + ' ' + commands[key]; commandsString = commandsString + ' ' + commands[key];
} }
bl.log(commandsString); bl.log(commandsString);
var getCwdTest = function () {
console.log('The current directory is: ' + smartcli.getCwd().path);
};
var checkCommandTest = function () {
if (smartcli.checkCommand('jazz')) { if (smartcli.checkCommand('jazz')) {
bl.log('One of your commands is jazz'); bl.log('One of your commands is jazz');
} }
else { else {
bl.log('None of your commands is jazz'); bl.log('None of your commands is jazz');
} }
};
var getOptionTest = function () {
console.log('We now test for option --test');
console.log(smartcli.getOption('test'));
};
var checkOptionTest = function () {
};
var getAnswerTest = function () { var getAnswerTest = function () {
smartcli.getAnswer('How do you feel?', function (answer) { smartcli.getAnswer('How do you feel?', function (answer) {
console.log('The answer is: ' + answer); console.log('The answer is: ' + answer);
@ -24,10 +35,11 @@ var getAnswerTest = function () {
var getChoiceTest = function () { var getChoiceTest = function () {
smartcli.getChoice('What music do you like to hear?', ['Jazz', 'Blues', 'Classical'], function (answer) { smartcli.getChoice('What music do you like to hear?', ['Jazz', 'Blues', 'Classical'], function (answer) {
console.log('The answer is: ' + answer); console.log('The answer is: ' + answer);
getCwdTest();
}); });
}; };
var getCwdTest = function () { //starting command tests
console.log('The current directory is: ' + smartcli.getCwd()); getCwdTest();
}; checkCommandTest();
getOptionTest();
//starting first interaction test (the other tests are then started via callbacks)
getAnswerTest(); getAnswerTest();

View File

@ -1,4 +1,5 @@
/// <reference path="typings/tsd.d.ts" /> /// <reference path="typings/tsd.d.ts" />
/// <reference path="./interfaces.ts" />
var path = require("path"); var path = require("path");
var beautylog = require("beautylog"); var beautylog = require("beautylog");
@ -34,20 +35,23 @@ smartcli.getCommands = function ():string[] {
// options // options
smartcli.checkOption = function(optionParam:string):boolean { smartcli.getOption = function(optionName:string):CliOption {
if (argv.hasOwnProperty(optionParam)) { if (argv.hasOwnProperty(optionName)) {
return true; return {
} name:optionName,
return false specified: true,
value: argv[optionName] //we already know from the "if" above that the value is available.
}; };
smartcli.getOptionValue = function(optionParam:string):any {
if (smartcli.checkOption(optionParam)) {
return argv[optionParam]
} }
return false; return {
name:optionName,
specified: false,
value: false
}
}; };
smartcli.getOptions = function() { smartcli.getOptions = function() {
var options = {}; var options = {};
for (var key in argv) { for (var key in argv) {
@ -59,11 +63,13 @@ smartcli.getOptions = function() {
}; };
/** /**
* returns the current working directory * returns Directory of cwd
* @returns {string} * @returns {{path: string}}
*/ */
smartcli.getCwd = function () { smartcli.getCwd = function():Directory {
return process.cwd(); return {
path: process.cwd()
}
}; };

11
ts/interfaces.ts Normal file
View File

@ -0,0 +1,11 @@
/// <reference path="index.ts" />
interface CliOption {
name: string;
specified:boolean;
value: any;
}
interface Directory {
path: string;
}

View File

@ -12,12 +12,27 @@ for (var key in commands) {
} }
bl.log(commandsString); bl.log(commandsString);
var getCwdTest = function(){
console.log('The current directory is: ' + smartcli.getCwd().path);
};
var checkCommandTest = function() {
if (smartcli.checkCommand('jazz')) { if (smartcli.checkCommand('jazz')) {
bl.log('One of your commands is jazz'); bl.log('One of your commands is jazz');
} else { } else {
bl.log('None of your commands is jazz'); bl.log('None of your commands is jazz');
} }
};
var getOptionTest = function() {
console.log('We now test for option --test')
console.log(smartcli.getOption('test'));
}
var checkOptionTest = function() {
};
var getAnswerTest = function() { var getAnswerTest = function() {
smartcli.getAnswer('How do you feel?',function(answer){ smartcli.getAnswer('How do you feel?',function(answer){
@ -29,12 +44,15 @@ var getAnswerTest = function() {
var getChoiceTest = function() { var getChoiceTest = function() {
smartcli.getChoice('What music do you like to hear?',['Jazz','Blues','Classical'],function(answer){ smartcli.getChoice('What music do you like to hear?',['Jazz','Blues','Classical'],function(answer){
console.log('The answer is: ' + answer); console.log('The answer is: ' + answer);
getCwdTest();
}); });
}; };
var getCwdTest = function(){
console.log('The current directory is: ' + smartcli.getCwd());
};
//starting command tests
getCwdTest();
checkCommandTest();
getOptionTest();
//starting first interaction test (the other tests are then started via callbacks)
getAnswerTest(); getAnswerTest();