Base module for writing advanced shell scripts with Node.js
npm install @clitools/baseBase 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.
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)
``bash`
$ npm i --save @clitools/base
[...]
example-tool:
`javascript
#!/usr/bin/env node
require('./lib/cli');
cli.program
.command('test ', '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:
`bash
$ ./example-tool
Usage:
example-tool
Commands:
example-tool test 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
``