cliclopts
Advanced tools
Comparing version 1.1.0 to 1.1.1
var cliclopts = require('./') | ||
var options = [ | ||
{ | ||
name: 'verbose', | ||
abbr: 'v', | ||
alias: ['loud'], | ||
boolean: true, | ||
help: 'be verbose' | ||
}, | ||
{ | ||
name: 'path', | ||
abbr: 'p', | ||
default: 'dat.json', | ||
help: 'path to file' | ||
} | ||
{ | ||
name: 'verbose', | ||
abbr: 'v', | ||
alias: ['loud'], | ||
boolean: true, | ||
help: 'be verbose' | ||
}, | ||
{ | ||
name: 'path', | ||
abbr: 'p', | ||
default: 'dat.json', | ||
help: 'path to file' | ||
} | ||
] | ||
@@ -18,0 +18,0 @@ |
59
index.js
@@ -1,4 +0,2 @@ | ||
var PassThrough = require('stream').PassThrough | ||
function Cliclopts(opts) { | ||
function Cliclopts (opts) { | ||
if (!(this instanceof Cliclopts)) { | ||
@@ -18,33 +16,35 @@ return new Cliclopts(opts) | ||
Cliclopts.prototype.usage = function () { | ||
var output = "" | ||
this.opts.filter(function (opt) { | ||
return opt.help | ||
}) | ||
.forEach(function (option) { | ||
output += this.indent | ||
var output = '' | ||
var helpIndex = option.helpIndex | ||
this.opts | ||
.filter(function (opt) { | ||
return opt.help | ||
}) | ||
.forEach(function (option) { | ||
output += this.indent | ||
if (!helpIndex) { | ||
helpIndex = '--' + option.name | ||
if (option.abbr) helpIndex += ', -' + option.abbr | ||
} | ||
output += helpIndex | ||
var helpIndex = option.helpIndex | ||
var offset = 0 | ||
if (helpIndex.length > this.width) { | ||
output += '\n' + this.indent | ||
} else { | ||
offset = helpIndex.length | ||
} | ||
if (!helpIndex) { | ||
helpIndex = '--' + option.name | ||
if (option.abbr) helpIndex += ', -' + option.abbr | ||
} | ||
output += helpIndex | ||
output += Array(this.width - offset).join(' ') | ||
var offset = 0 | ||
if (helpIndex.length > this.width) { | ||
output += '\n' + this.indent | ||
} else { | ||
offset = helpIndex.length | ||
} | ||
output += option.help | ||
if (option.hasOwnProperty('default')) { | ||
output += ' (default: ' + JSON.stringify(option.default) +')' | ||
} | ||
output += '\n' | ||
}.bind(this)) | ||
output += Array(this.width - offset).join(' ') | ||
output += option.help | ||
if (option.hasOwnProperty('default')) { | ||
output += ' (default: ' + JSON.stringify(option.default) + ')' | ||
} | ||
output += '\n' | ||
}.bind(this)) | ||
return output | ||
@@ -98,3 +98,2 @@ } | ||
module.exports = Cliclopts | ||
module.exports = Cliclopts |
{ | ||
"name": "cliclopts", | ||
"version": "1.1.0", | ||
"version": "1.1.1", | ||
"description": "Command line options helper and usage printer", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -8,2 +8,3 @@ # cliclopts | ||
[![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard) | ||
@@ -20,3 +21,3 @@ ## usage | ||
name: 'verbose', | ||
abbr: 'v' | ||
abbr: 'v', | ||
alias: ['loud'], | ||
@@ -29,3 +30,3 @@ boolean: true, | ||
abbr: 'p', | ||
default: './dat.json' | ||
default: './dat.json', | ||
help: 'path to file' | ||
@@ -37,2 +38,14 @@ } | ||
``` | ||
### cliclopts(options) | ||
options is an array of objects with the following possible keys: | ||
* `name` primary name of option | ||
* `abbr` one character alias of the option | ||
* `alias` other options treated as alias | ||
* `boolean` if true the option is seen as a boolean flag | ||
* `help` usage string for the option | ||
* `default` default value of the option | ||
### cliOpts.usage() | ||
@@ -77,6 +90,6 @@ | ||
```js | ||
var allowedOptions = { | ||
var allowedOptions = [ | ||
{ | ||
name: 'verbose', | ||
abbr: 'v' | ||
abbr: 'v', | ||
alias: ['cry-at-me'], | ||
@@ -97,15 +110,11 @@ boolean: true, | ||
} | ||
} | ||
] | ||
var clopts = require('cliclopts')(allowedOptions) | ||
var cliOpts = require('cliclopts')(allowedOptions) | ||
var argv = require('minimist')(process.argv.slice(2), { | ||
alias: clopts.alias(), | ||
boolean: clopts.boolean(), | ||
default: clopts.default(), | ||
}) | ||
var argv = require('minimist')(process.argv.slice(2), cliOpts.options()) | ||
if(minimist.help) { | ||
if (argv.help) { | ||
console.log('Usage: command [options]') | ||
clopts.print() | ||
cliOpts.print() | ||
process.exit() | ||
@@ -112,0 +121,0 @@ } |
20399
6
100
120
8573