Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add showUsage helper

  • Loading branch information...
commit 16a41ae4417ec8fd2b40589a6e3e8d8d2c6eb713 1 parent 6edc048
@Filirom1 authored
Showing with 79 additions and 0 deletions.
  1. +16 −0 examples/my-program.js
  2. +2 −0  lib/nopt.js
  3. +61 −0 lib/usage.js
View
16 examples/my-program.js
@@ -21,10 +21,26 @@ var nopt = require("../lib/nopt")
, "g" : ["--flag"]
, "s" : "--flag"
}
+ , description = { "foo" : "Something really foooooooo"
+ , "bar" : "A bar thing"
+ , "baz" : "More or less baz"
+ , "flag" : "Flag it as well"
+ , "pick" : "Or pick something"
+ }
+ , defaults = { "foo" : null
+ , "bar" : 42
+ , "baz" : "/etc/passwd"
+ , "bloo" : "small"
+ , "pick" : false
+ }
// everything is optional.
// knownOpts and shorthands default to {}
// arg list defaults to process.argv
// slice defaults to 2
, parsed = nopt(knownOpts, shortHands, process.argv, 2)
+ , usage = nopt.usage(knownOpts, shortHands, description, defaults)
console.log("parsed =\n"+ require("util").inspect(parsed))
+
+console.log('\nUsage: ')
+console.log(usage)
View
2  lib/nopt.js
@@ -8,9 +8,11 @@ var url = require("url")
, path = require("path")
, Stream = require("stream").Stream
, abbrev = require("abbrev")
+ , usage = require("./usage")
module.exports = exports = nopt
exports.clean = clean
+exports.usage = usage
exports.typeDefs =
{ String : { type: String, validate: validateString }
View
61 lib/usage.js
@@ -0,0 +1,61 @@
+module.exports = function showUsage(knownOpts, shortHands /*, description1, description2, ... */){
+ var descriptions = Array.prototype.slice.call(arguments, 2, arguments.length),
+ knownOptsMaxLen = maxLength(Object.keys(knownOpts)),
+ shortHands2 = {},
+ SPACE = 4;
+
+ // create a shortHands object with the long name as key.
+ Object.keys(shortHands).forEach(function(key){
+ var value = Array.isArray(shortHands[key]) ? shortHands[key][0] : shortHands[key];
+ var option = value.replace('--', '');
+ shortHands2[option] = shortHands2[option] || [];
+ shortHands2[option].push(key);
+ });
+
+ // we can have severals shorthands for one option
+ var shortHandsMaxLength = values(shortHands2).reduce(function(a, b){
+ a = Array.isArray(a) ? a.join(', -').length : a;
+ b = Array.isArray(b) ? b.join(', -').length : b;
+ return ( a > b) ? a : b;
+ });
+
+ // start by writing all known options
+ var out = {};
+ Object.keys(knownOpts).forEach(function(key){
+ var shorts = shortHands2[key] ? ', -' + shortHands2[key].join(', -') : '';
+ var cmd = rpad('', SPACE) + '--' + key + shorts;
+ out[key] = rpad(cmd, knownOptsMaxLen + shortHandsMaxLength + 5 + 2 * SPACE);
+ });
+
+ // then write all the descriptions
+ descriptions.forEach(function(description){
+ var descriptionMaxLen = maxLength(values(description));
+ Object.keys(knownOpts).forEach(function(key){
+ var value = '' + (description[key] !== undefined ? description[key] : '');
+ out[key] += rpad(value, descriptionMaxLen + SPACE);
+ });
+ });
+
+ return values(out).join('\n');
+};
+
+// string right padding helper
+function rpad(str, length) {
+ while (str.length < length)
+ str = str + ' ';
+ return str;
+}
+
+// Object.values
+function values(o){
+ return Object.keys(o).map(function(k){ return o[k]; });
+}
+
+// Return the max length of an array of string.
+function maxLength(arr){
+ return arr.map(function(str){
+ return ('' + str).length;
+ }).reduce(function(a,b){
+ return ( a > b ) ? a : b;
+ });
+}
Please sign in to comment.
Something went wrong with that request. Please try again.