CLI-Builder
API Docs
Trying out an example
This example can be found in bin/example-cli.js
git clone https://github.com/maarteNNNN/cli-builder.git
npm install
npm link
cli-builder help
npm unlink
Installing
npm install cli-builder
Be sure to take a look at bin/example-cli.js
Instantiate a new CLI with new REPLClient({ ...options })
REPL
stands for Read Eval Print Loop
defining an commands
Object as shown below in below
cli.run(commands);
Commands example
const commands = {
test: {
execute: () => console.log('this is the test run'),
help: 'help of test',
testing: {
execute: ({ argument, options }) =>
console.log('ARGUMENT: ', argument, '\nOPTIONS: ', options),
help: 'testing help',
input: '<arg-to-pass-to-execute-function>',
options: [{ option: 'f', help: 'Follow the logs' }],
},
testing2: {
execute: () => console.log('executing testing2'),
help: 'testing2 help',
},
},
test2: {
execute: () => console.log('this is the test2 run'),
help: 'help of test2',
},
deep: {
nesting: {
works: {
as: {
command: () =>
console.log('to run this type `deep nesting works as command`'),
},
},
},
},
runSomeFunction: async () => {
},
exampleWithOneExecute: {
knownCommand: {
help: 'Help for this known command',
},
'any-yet-unknown-property': {
help: 'Help for ANY UNKOWN PROPERTY',
},
async execute({ argument, options }) {
console.log(this);
console.log(param);
console.log(options);
},
},
};
WARNING
Help is dynamically mounted to the commands
object. It generates a function which will mount all child help
properties.
NOTE
Running is going through the object and should be written as arguments the following way: deep nesting works as command
for the example above (It executes the function or the execute
child function in case help wants to be added).
deep nesting works as command --help
(executes --help or -h
child function in case it's available)
WARNING
functions are added as camelCase but are transformed to kebab-case:
run-some-function
will call result in runSomeFunction
.
ADVANCED EXAMPLE
actions
can be used to integrate imported files. You can check out the example of a full-fledged cli implementation in ldpos-commander or SocketCluster.
The CLI (REPLCient
) object is passed to the actions functions by Function.prototype.bind
The bindActionArgs
are passed via Function.prototype.bind
as well. Except if an argument is passed in the commands object function Eg.
someFunctionCmd: {
execute: (argument) => cli.actions.someFunction(argument),
}
A more apprehensive example
const actions = {
getUserData = async (id, someFunction, aString, aNumber) => {
try {
const data = await axios.get(`user/${id}`)
console.log(this.argv)
this.successLog(data)
someFunction()
console.log(aString, aNumber)
} catch (e) {
throw new Error(e)
}
}
}
const options = {
bindActionArgs = [123, () => console.log('function executed'), 'a string', 23123]
}
const cli = new REPLClient(options)
const commands = {
anActionTest: async () => await cli.actions.getUserData()
}
cli.run(commands)
using cli-builder an-action-test
will execute the getUserData
function with the bindActionArgs
parameters bound to it.
Defining help
with only one execute
function
When you want to dynamically output to the console Eg. get a JSON Object from an API with undefined structure but you want to be able to log any of those properties to the console it can be done via:
const commands = {
entrypoint: {
any: {
help: 'Help for any',
},
extra: {
help: 'Help for extra',
},
helping: {
help: 'Help for helping',
},
commands: {
help: 'Help for commands',
},
'any-unknown-property': {
help: 'Help for ANY UNKOWN PROPERTY',
},
async execute(param) {
param = cli.kebabCaseToCamel(param);
const {
data: { data },
} = axios.get('https://reqres.in/api/users?page=2');
if (!data[param] === undefined)
throw new Error('Custom property not found.');
this.successLog(data[param], `${param}:`);
},
},
};