- Create your own cli
- Auto generates guide for cli and individual commands
- Interactive mode (using
-iflag) - Argument defaults
npm i @kvnlnt/spawn
mycli.js
// the lib const cli = require("@kvnlnt/spawn"); // add a command, add an argument, a callback and an example cli .command("hello", "Prints hello") .argument("output", "o", "Hello to") .callback(resp => console.log("Hello,", resp.output)) .example("hello -o=World", "Prints; Hello World"); // add another cli .command("goodbye", "Prints goodbye") .argument("output", "o", "Goodbye to") .callback(resp => console.log("Goodbye,", resp.output)) .example("goodbye -o=World", "Prints: Goodbye World"); // set the default command cli.defaultCommand("help"); // run the cli cli.run();
Now set up your tool to be a cli with npm link and you'll get something like this
mycli hello -o=You
# result => Hello YouAll methods are meant to be chained. This is to support a clean api. To keep things organized it's recommended that you keep your callbacks as external scripts. It keep my in a tasks folder.
Adds a command
| Arg | Type | Desc |
|---|---|---|
| name | string | name of command |
| desc | string | description of command |
Adds an argument to last added command
| Arg | Type | Desc |
|---|---|---|
| name | string | name of argument |
| abbr | string | abbreviated argument name |
| desc | string | description of argument |
| def | anything | default value |
Adds callback to last added command
| Arg | Type | Desc |
|---|---|---|
| f | function | function called when command is executed |
Adds example to last added command
| Arg | Type | Desc |
|---|---|---|
| cmd | string | example command |
| desc | string | description |
Sets default command
| Arg | Type | Desc |
|---|---|---|
| cmd | string | name of command |
Runs the cli
This is the auto generated guide. To use it, assign it as a callback. Something like this:
cli.command("guide", "Prints guide").callback(cli.printGuide);
This is the autogenerated guide for a command. Commands are bound to the cli instance and therefore are available inside the callback as this.printCommandGuide. Just pass in the string name of the command. Here's an example:
// your callback function myCallback(resp) { if (resp.help === true) return this.printCommandGuide("myCallback"); console.log(resp.output); } // your cli cli .command("myCallback", "does stuff") .argument("help", "h", "Output help", false) .callback(myCallback);
The following will output the command's guide:
mycli myCallback -h
You can pass the -i flag without a command and use your tool interactively, like this:
mycli -i
All examples are in the Examples folder.