CLI definitions
greet.js
var clide = require('clide');
clide({
user: 'Name to greet'
}, function (props) {
console.log('Hello ' + props.user + '!');
});
node greet.js --name world
Hello world!
~/.greetrc
{
"name": "shadow"
}
greet.js
Hello shadow!
No config nor params provided? Prompts to the rescue!
greet.js
Name to greet: pfraces
Hello pfraces!
Get help
greet.js --help
Get version from package.json
greet.js --version
0.1.0
Use specific config file
greet.js --config /path/to/config
greet.js --config <( echo '{ "name": "inlined!" }' )
Hello inlined!!
password.js
var clide = require('clide');
clide({
user: 'User name',
pass: {
description: 'Password',
hidden: true
}
}, function (props) {
// Do whatever with your defined props.
// No checks for null or undefined are needed.
if (props.pass !== 'foo') {
console.log('not authorized');
} else {
console.log('Hello ' + props.user + '!');
}
// check for optional (not defined) ones
if (props.time) {
console.log(new Date());
}
}, function (err) {
// Define your error handling here.
// This fallback function is optional.
// If not provided, the possible errors will cause an exception.
console.log(err);
});
npm install clide
I used to integrate optimist, rc and prompt in my CLI scripts to define its required properties and several ways to get their values
I find this pattern so useful for quick start with your CLI minimals, so let's write the glue between them for improved laziness