0.1.12 • Published 3 years ago
@lkmx/cli v0.1.12
LKMX CLI
This simple library is meant to streamline the process of building CLIs with nodejs.
Documentation
The main concept to build a CLI are the commands, these are the main actions available to the user.
The commands have the following structure:
Field | Type | Description |
---|---|---|
name | String | The name of the command. |
description | String | A description for the command. |
argument | Object | An object containing a key and a description for the argument that follows the command. |
options | Array | An array with the options supported by the command in the form of objects. |
action | Function | A function to be executed when the used invokes the specified command. The function receives the argument and an object with the options as parameters. |
Commands
Create one file per command. For example, split.js
:
module.exports = {
name: 'split',
description: 'Split a string as substrings and display them as an array',
argument: {
key: '<string>',
description: 'string to split'
},
options: [
{
key: '--first',
description: 'display just the first substring',
},
{
key: '-s, --separator <char>',
description: 'separator character',
default: ','
}
],
requiredOptions: [
{
key: '-ro, --req-option <option>',
description: 'option',
default: 'default value to be suggested',
choices: ['small', 'medium', 'large'],
}
]
action: (str, options) => {
const limit = options.first ? 1 : undefined;
console.log(str.split(options.separator, limit));
}
}
CLI
Install dependencies
npm install @lkmx/cli@0.1.6
Code CLI
Use the following code as a template to develop your own CLI
// Import @lkmx/cli library
const CLI = require('@lkmx/cli')
// Import the custom commands
const split = require('./commands/split.js');
const example = require('./commands/example.js');
// Initialize the CLI
const cli = new CLI(
'string-util',
'CLI to some Javascript string utilities',
'0.1.0'
);
// Add all the commands
cli.add(split);
cli.add(example);
// Execute the CLI
cli.exec();
Development
In order to develop over this simple library a new CLI needs to be created and using npm link
perform the necessary changes.