super-cli

1.0.1 • Public • Published

Super-CLI

npm version Build Status Standard - JavaScript Style Guide

Super-CLI is a micro framework for creating command line tools in node. The goal with Super-CLI is to have a simple and powerful API, which enables you to create beautiful CLI programs with minimal effort.

A Super-CLI script will have a structure like below.

my-script command [options, ...] [arguments, ...]

Install

npm install super-cli -—save

Usage

To get started you have to require the super-cli module and register your commands. Remember to set the environment to node at the top of the script #!/usr/bin/env node and make your script executable chmod a+x my-script.

#!/usr/bin/env node
 
const SuperCLI = require('super-cli')
 
const cli = new SuperCLI()
 
cli.on('my-command', arg => {
  console.log(arg)
})

Register commands

Commands are simply registered like below and arguments are automatically parsed to the callbacks as function arguments. If your CLI program has a lot of commands, it would be a great idea to move the command callbacks into seperate files.

cli.on('my-command', (arg1, arg2, ...) => {
  // do something
})

Check for options

cli.option('-h', '--help') // will return true if set
cli.option('-l=', '--lastname=') // will return the value of --lastname if set

Run command

Trigger a command from within the script.

cli.run('my-command')

Prompt user for input

cli.prompt('My question:').then(answer => console.log(answer))

Catch all commands

This will run if no registered command was triggered.

cli.on('*', (...args) => {
  console.log(args)
})

Listen for data on stdin

cli.stdin(data => console.log(data))

Write data to stdout

// you can use
console.log('message')
// or
cli.stdout('message')

On exit

cli.on('exit', () => {
  // this event will fire on cli exit or termination
})

Exit cli

cli.exit() // will close the cli and fire the exit event

Package Sidebar

Install

npm i super-cli

Weekly Downloads

2

Version

1.0.1

License

MIT

Last publish

Collaborators

  • kvartborg