DEPRECATION NOTICE
This library has been depreciated. Use classy-commander or commander instead!
kitten-cli
The purrfect Command Line parsing library for Node.js console apps!
Description
Kitten CLI enables rapid development of command line apps / tasks in node.
Installation
npm install kitten-cli --save
Usage
Simple Hello World Example
index.js
var cli = require('kitten-cli');
cli
.command('greet')
.value('name')
.action(greet);
cli.run();
function greet(command) {
console.log('Hello ' + command.value('name'));
}
Running node index.js greet World
would print Hello World
to the console
Importing
var cli = require('kitten-cli');
Registering Commands
There are 2 ways to register commands:
- You can use the fluent API:
cli
.command('login')
.value('user-name')
.value('password')
.option('-r', '--remember-me')
.action(login)
.command('logout')
.action(logout);
- Or declare your commands in a file using the intuitive DSL:
commands.txt
login <user-name> <password> -r|--remember-me
logout
Then load your commands from the file:
index.js
cli.commandsFromFile('commands.txt');
Note that if you use the DSL and you want to associate actions with your commands you still need to wire up your actions via the fluent API afterwards.
cli
.command('login').action(login)
.command('logout').action(logout);
Using options
You can declare options for a command. Note that you can specify multiple names for an option.
index.js
cli
.command('greet')
.value('name')
.option('-s', '--shout')
.action(showGreeting);
cli.run();
...
Then check them in your action handler:
...
function showGreeting(command) {
var message = 'Hi ' + command.value('name');
if (command.option('shout')) {
message = message.toUpperCase();
}
console.log(message);
}
Running node index.js greet Zoidberg
will print Hi Zoidberg
Running node index.js greet Zoidberg -s
will print HI ZOIDBERG
Options with values
If you declare a value right after declaring an option, the value will be associated with the option (not the command).
cli
.command('bio')
.value('first-name')
.option('-l', '--last-name').value('last-name')
.option('-a', '--age').value('age')
.action(showBio);
function showBio(command) {
var message = 'First Name: ' + command.value('first-name');
if (command.option('last-name')) {
message += '\nLast Name' + command
.option('last-name')
.value('last-name');
}
if (command.option('age')) {
message += '\nAge:' + command
.option('age')
.value('age');
}
console.log(message);
}
Running node index.js bio Rick --last-name Sanchez --age 60
would print:
First Name: Rick
Last Name: Sanchez
Age: 60
Got an Issue / Feature Request?
Then please add an issue on GitHub! 😺