├── cliclopts.png ├── collaborators.md ├── example.js ├── index.js ├── package.json └── readme.md /cliclopts.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/finnp/cliclopts/cb2ebec3d20e2e633cce9263d9becd9972531490/cliclopts.png -------------------------------------------------------------------------------- /collaborators.md: -------------------------------------------------------------------------------- 1 | ## Collaborators 2 | 3 | cliclopts is only possible due to the excellent work of the following collaborators: 4 | 5 | 6 | 7 |
maxogdenGitHub/maxogden
finnpGitHub/finnp
8 | -------------------------------------------------------------------------------- /example.js: -------------------------------------------------------------------------------- 1 | var cliclopts = require('./') 2 | 3 | var options = [ 4 | { 5 | name: 'verbose', 6 | abbr: 'v', 7 | alias: ['loud'], 8 | boolean: true, 9 | help: 'be verbose' 10 | }, 11 | { 12 | name: 'path', 13 | abbr: 'p', 14 | default: 'dat.json', 15 | help: 'path to file' 16 | } 17 | ] 18 | 19 | var clopts = cliclopts(options) 20 | 21 | clopts.print() 22 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | function Cliclopts (opts) { 2 | if (!(this instanceof Cliclopts)) { 3 | return new Cliclopts(opts) 4 | } 5 | this.opts = opts || [] 6 | this.indent = ' ' 7 | this.width = 23 8 | } 9 | 10 | Cliclopts.prototype.print = function (stream) { 11 | var output = stream || process.stdout 12 | output.write(this.usage()) 13 | } 14 | 15 | Cliclopts.prototype.usage = function () { 16 | var output = '' 17 | 18 | this.opts 19 | .filter(function (opt) { 20 | return opt.help 21 | }) 22 | .forEach(function (option) { 23 | output += this.indent 24 | 25 | var helpIndex = option.helpIndex 26 | 27 | if (!helpIndex) { 28 | helpIndex = '--' + option.name 29 | if (option.abbr) helpIndex += ', -' + option.abbr 30 | } 31 | output += helpIndex 32 | 33 | var offset = 0 34 | if (helpIndex.length > this.width) { 35 | output += '\n' + this.indent 36 | } else { 37 | offset = helpIndex.length 38 | } 39 | 40 | output += Array(this.width - offset).join(' ') 41 | 42 | output += option.help 43 | if (option.hasOwnProperty('default')) { 44 | output += ' (default: ' + JSON.stringify(option.default) + ')' 45 | } 46 | output += '\n' 47 | }.bind(this)) 48 | 49 | return output 50 | } 51 | 52 | Cliclopts.prototype.booleans = function () { 53 | return this.opts 54 | .filter(function (opt) { 55 | return opt.boolean 56 | }) 57 | .map(function (opt) { 58 | return opt.name 59 | }) 60 | } 61 | 62 | Cliclopts.prototype.boolean = Cliclopts.prototype.booleans 63 | 64 | Cliclopts.prototype.default = function () { 65 | var defaults = {} 66 | this.opts.forEach(function (opt) { 67 | if ('default' in opt) { 68 | defaults[opt.name] = opt.default 69 | if ('abbr' in opt) defaults[opt.abbr] = opt.default 70 | } 71 | }) 72 | return defaults 73 | } 74 | 75 | Cliclopts.prototype.alias = function () { 76 | var alias = {} 77 | this.opts.forEach(function (opt) { 78 | var build = [] 79 | if ('alias' in opt) { 80 | if (typeof opt.alias === 'string') build.push(opt.alias) 81 | else build = build.concat(opt.alias) 82 | } 83 | if ('abbr' in opt) build.push(opt.abbr) 84 | alias[opt.name] = build 85 | }) 86 | return alias 87 | } 88 | 89 | Cliclopts.prototype.options = function () { 90 | return { 91 | alias: this.alias(), 92 | boolean: this.boolean(), 93 | default: this.default() 94 | } 95 | } 96 | 97 | module.exports = Cliclopts 98 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "cliclopts", 3 | "version": "1.1.1", 4 | "description": "Command line options helper and usage printer", 5 | "main": "index.js", 6 | "scripts": { 7 | "test": "echo \"Error: no test specified\" && exit 1" 8 | }, 9 | "repository": { 10 | "type": "git", 11 | "url": "https://github.com/finnp/cliclopts.git" 12 | }, 13 | "keywords": [ 14 | "cli", 15 | "minimist", 16 | "options" 17 | ], 18 | "author": "Finn Pauls", 19 | "license": "MIT", 20 | "bugs": { 21 | "url": "https://github.com/finnp/cliclopts/issues" 22 | }, 23 | "homepage": "https://github.com/finnp/cliclopts" 24 | } 25 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # cliclopts 2 | [![NPM](https://nodei.co/npm/cliclopts.png)](https://nodei.co/npm/cliclopts/) 3 | 4 | ![cliclopts](cliclopts.png) 5 | 6 | Command line options helper and usage printer, works well with [minimist](https://www.npmjs.com/package/minimist), inspired by [nomnom](https://www.npmjs.com/package/nomnom) 7 | 8 | [![js-standard-style](https://cdn.rawgit.com/feross/standard/master/badge.svg)](https://github.com/feross/standard) 9 | 10 | ## usage 11 | 12 | Define the allowed options in an array and pass it to 'cliclopts' 13 | 14 | ```js 15 | var cliclopts = require('cliclopts') 16 | 17 | var options = [ 18 | { 19 | name: 'verbose', 20 | abbr: 'v', 21 | alias: ['loud'], 22 | boolean: true, 23 | help: 'be verbose' 24 | }, 25 | { 26 | name: 'path', 27 | abbr: 'p', 28 | default: './dat.json', 29 | help: 'path to file' 30 | } 31 | ] 32 | 33 | var cliOpts = cliclopts(options) 34 | ``` 35 | ### cliclopts(options) 36 | 37 | options is an array of objects with the following possible keys: 38 | 39 | * `name` primary name of option 40 | * `abbr` one character alias of the option 41 | * `alias` other options treated as alias 42 | * `boolean` if true the option is seen as a boolean flag 43 | * `help` usage string for the option 44 | * `default` default value of the option 45 | 46 | 47 | ### cliOpts.usage() 48 | 49 | Returns the usage information as string: 50 | 51 | ``` 52 | --verbose, -v be verbose 53 | --path, -p path to file (default: "dat.json") 54 | ``` 55 | 56 | ### cliOpts.print() 57 | 58 | Prints the usage information. 59 | 60 | ### cliOpts.boolean() 61 | 62 | Returns Array of all command names that are specified as boolean. 63 | 64 | ### cliOpts.alias() 65 | 66 | Returns Object with command names as keys and alias list as value (including abbr) 67 | 68 | ### cliOpts.default() 69 | 70 | Returns Object with command names as keys and default values as values. 71 | 72 | ### cliOpts.options() 73 | 74 | Returns 75 | ```js 76 | { 77 | alias: cliOpts.alias(), 78 | boolean: cliOpts.boolean(), 79 | default: cliOpts.default() 80 | } 81 | ``` 82 | 83 | 84 | ## Example usage with `minimist` 85 | ```js 86 | var allowedOptions = [ 87 | { 88 | name: 'verbose', 89 | abbr: 'v', 90 | alias: ['cry-at-me'], 91 | boolean: true, 92 | help: 'be verbose' 93 | }, 94 | { 95 | name: 'path', 96 | abbr: 'p', 97 | help: 'path to the file' 98 | }, 99 | { 100 | name: 'help', 101 | abbr: 'h', 102 | help: 'show help', 103 | boolean: true 104 | } 105 | ] 106 | 107 | var cliOpts = require('cliclopts')(allowedOptions) 108 | 109 | var argv = require('minimist')(process.argv.slice(2), cliOpts.options()) 110 | 111 | if (argv.help) { 112 | console.log('Usage: command [options]') 113 | cliOpts.print() 114 | process.exit() 115 | } 116 | 117 | yourprogram(argv) 118 | ``` 119 | 120 | --------------------------------------------------------------------------------