@clitools/base
v2.3.1
Published
Base module for writing advanced shell scripts with Node.js
Downloads
9
Readme
@clitools/base
Base module for writing advanced shell scripts with Node.js.
At it's core it uses Yargs for option and parameter parsing, so it's a good idea to look at that documentation first.
Features
The target is to have a base module for writing Linux shell scripts, with APIs for most common CLI tasks, like:
- Parsing command line optionsa and arguments (cli.js, using: Yargs)
- Bash completion script generation (cli.js, using: Yargs)
- User configuration file management (config.js)
- Access to system key management for secure storage of credentials (Plugin: @clitools/keystore)
- Error handling (error-handler.js)
- Debug mode, different verbosity levels (error-handler.js, ui.js)
- Silent mode (ui.js)
- Integrates request for API calls, crawling, downloading, etc. (request.js)
- Consistent, formatted cli text output (ui.js, using: Chalk, columnify, cli-progress)
Install
$ npm i --save @clitools/base
[...]
Example
example-tool:
#!/usr/bin/env node
require('./lib/cli');
cli.program
.command('test <param>', 'Example command', (program) => {
program
.positional('param', {
description: 'Test parameter'
});
}, ({ param }) => {
cli.ui.startProgress('Test...');
return cli.promises.throttle([
() => Promise.resolve(param),
() => Promise.resolve('A'),
() => Promise.resolve('B'),
() => Promise.resolve('C'),
() => Promise.resolve('D'),
() => Promise.resolve('E'),
() => Promise.resolve('F'),
() => Promise.resolve('G'),
() => Promise.resolve('H'),
() => Promise.resolve('I'),
() => Promise.resolve('X')
], 1000, (p) => {
cli.ui.updateProgress(p);
})
.then((result) => {
cli.ui.stopProgress();
cli.ui.print(result);
});
});
cli.run();
Output:
$ ./example-tool
Usage:
example-tool <command>
Commands:
example-tool test <param> Example command
example-tool completion generate bash completion script
Options:
-v, --verbose Show more information [count]
-s, --silent No output [boolean]
-d, --debug Debug mode (stacktraces, very verbose) [boolean]
-h, --help Show help [boolean]
-V, --version Show version number [boolean]
Error:
Not enough non-option arguments: got 0, need at least 1