smartcli/ts/smartcli.getters.ts

97 lines
2.6 KiB
TypeScript
Raw Normal View History

2016-06-10 00:27:04 +00:00
import "typings-global";
2015-11-05 20:43:34 +00:00
2016-05-12 01:27:45 +00:00
import * as interfaces from "./smartcli.interfaces";
2016-03-18 17:08:26 +00:00
import plugins = require("./smartcli.plugins");
import SmartcliChecks = require("./smartcli.checks");
2015-11-05 20:43:34 +00:00
2016-03-18 17:08:26 +00:00
/**
* gets the second or higher value of plugins.argv._ if specified and returns it as commandArgument
* @param argumentLevel
* @returns {{specified: (boolean|boolean), name}}
*/
2016-05-12 01:27:45 +00:00
export let commandArgument = function(argumentLevel):interfaces.CliCommandArgument {
var commandArgument:interfaces.CliCommandArgument = {
2016-03-18 17:08:26 +00:00
specified: false,
name: "undefined",
level:argumentLevel
};
if(argumentLevel < 1) {
plugins.beautylog.error("smartcli.get.argument cannot be invoked with an argumentLevel smaller than 1");
return commandArgument;
}
if(SmartcliChecks.commandArgumentPresence(argumentLevel)) {
commandArgument.specified = true;
commandArgument.name = plugins.argv._[argumentLevel];
}
return commandArgument;
};
2016-03-18 17:08:26 +00:00
/**
* returns array with commandArgs
* @returns {CliCommandArgument[]}
*/
2016-05-12 01:27:45 +00:00
export let commandArgs = function():interfaces.CliCommandArgument[] {
var commandArgs:interfaces.CliCommandArgument[] = [];
2016-03-18 17:08:26 +00:00
var argsArray = commandArray().slice(0);
argsArray.shift();
2016-05-11 00:21:30 +00:00
for (let item in argsArray){
2016-05-12 01:27:45 +00:00
let commandArgument:interfaces.CliCommandArgument = {
2016-03-18 17:08:26 +00:00
specified:true,
name:argsArray[item],
2016-05-11 00:21:30 +00:00
level: parseInt(item) + 1
2016-03-18 17:08:26 +00:00
}
commandArgs.push(commandArgument);
}
return commandArgs;
};
/**
* returns complete command array
* @returns {any}
*/
export let commandArray = function ():string[] {
var commandArray = plugins.argv._;
return commandArray;
};
2015-11-05 20:43:34 +00:00
2016-03-18 17:08:26 +00:00
/**
* returns a cli option
* @param optionName
* @returns {CliOption}
*/
2016-05-12 01:27:45 +00:00
export let option = function(optionName:string):interfaces.CliOption {
var cliOption:interfaces.CliOption = {
2016-03-18 17:08:26 +00:00
name:optionName,
specified: false,
value: false
};
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.
}
return cliOption;
};
2015-11-05 20:43:34 +00:00
2016-03-18 17:08:26 +00:00
export let options = function() {
var options = {};
for (var key in plugins.argv) {
if (key != "_") {
options['key'] = plugins.argv['key'];
}
}
return options;
};
2015-11-05 20:43:34 +00:00
2016-03-18 17:08:26 +00:00
/**
* returns Directory of cwd
* @returns {{path: string}}
*/
2016-05-12 01:27:45 +00:00
export let cwd = function():interfaces.Directory {
2016-03-18 17:08:26 +00:00
return {
path: process.cwd()
2015-11-05 20:43:34 +00:00
}
2016-03-18 17:08:26 +00:00
};