storbi v2.0.2
storbi
storbi is an opinionated framework for creating command line applications in Node. Why not commander or vorpal or insert other framework here? Well, I tried building an app with those and it just didn't feel right. So I started building my app from scratch without a framework and realized that I had built a framework in my app. A little copy and pasta and tweaking some fiddly bits and storbi was born.
Simple Example
import CLI from 'storbi'
class MyAwesomeCommand {
constructor() {
this.name = 'command'
}
run() {
console.log('I do awesome CLI stuff!')
}
}
const cli = new CLI(MyAwesomeCommand)
cli.run()
$> node index.js command
I do awesome CLI stuff!
See the examples directory for more ways to use storbi.
Installation
yarn add storbi
CLI
The cli constructor accepts two arguments. The list of commands and options.
const cli = new CLI([Command1, Command2], { name: 'my-tool', version: '1.0.0' })
If the options are not passed in the CLI constructor options, storbi will attempt to infer them from your package.json.
You may also pass a single command to the constructor in lieu of an Array.
Commands
A Command in storbi is any JavaScript class that has a name property and a method called run.
class Command {
constructor() {
this.name = 'command'
}
run(args) {
// Perform your command actions here
}
}
Command arguments are passed to your run method from the CLI in the form of:
{ args: ['one', 'two'], flags: { flag: value }}
A few examples
$> node index.js command --force
# { flags: { force: true } }
$> node index.js config init -r=20
# { flags: { r: '20' } }
$> node index.js new test.js
# { args: ['test.js'] }
Sub-Commands
Sub-commands are created by passing a command class to the constructor of another command class. For the sub-command to work properly the parent command class needs to extend CLI.Command
and pass it's commands to the Command
class.
const CLI = require('storbi')
class New {
constructor() {
this.name = 'new'
}
run(args) {
// Do new stuff
}
}
class Config extends CLI.Command {
constructor(cmds) {
super(cmds)
this.name = 'config'
}
}
You would then need to create an instance of the Config
class to pass to the CLI constructor. Note the lack of a run
method on the Config
class. This run method will never be called and should be omitted.