Comparing version 1.0.0-beta to 1.0.0-beta2
111
index.js
@@ -0,4 +1,17 @@ | ||
var MAX_LINE_WIDTH = process.stdout.columns || 200; | ||
var MIN_OFFSET = 25; | ||
var errorHandler; | ||
var commandsPath; | ||
var reAstral = /[\uD800-\uDBFF][\uDC00-\uDFFF]/g; | ||
var ansiRegex = /\x1B\[([0-9]{1,3}(;[0-9]{1,3})*)?[m|K]/g; | ||
function stringLength(str){ | ||
return str | ||
.replace(ansiRegex, '') | ||
.replace(reAstral, ' ') | ||
.length; | ||
} | ||
function camelize(name){ | ||
@@ -15,3 +28,3 @@ return name.replace(/-(.)/g, function(m, ch){ | ||
function pad(width, str){ | ||
return str + Array(Math.max(0, width - str.length) + 1).join(' '); | ||
return str + Array(Math.max(0, width - stringLength(str)) + 1).join(' '); | ||
} | ||
@@ -286,2 +299,4 @@ | ||
commandsPath = [command.name]; | ||
for (var i = 0; i < args.length; i++) | ||
@@ -382,2 +397,4 @@ { | ||
commandsPath.push(command.name); | ||
resultToken = { | ||
@@ -425,2 +442,3 @@ command: command, | ||
/** | ||
@@ -722,2 +740,30 @@ * @class | ||
function showCommandHelp(command){ | ||
function breakByLines(str, offset){ | ||
var words = str.split(' '); | ||
var maxWidth = MAX_LINE_WIDTH - offset || 0; | ||
var lines = []; | ||
var line = ''; | ||
while (words.length) | ||
{ | ||
var word = words.shift(); | ||
if (!line || (line.length + word.length + 1) < maxWidth) | ||
{ | ||
line += (line ? ' ' : '') + word; | ||
} | ||
else | ||
{ | ||
lines.push(line); | ||
words.unshift(word); | ||
line = ''; | ||
} | ||
} | ||
lines.push(line); | ||
return lines.map(function(line, idx){ | ||
return (idx && offset ? pad(offset, '') : '') + line; | ||
}).join('\n'); | ||
} | ||
function commandsHelp(){ | ||
@@ -727,20 +773,17 @@ if (!command.hasCommands()) | ||
var lines = []; | ||
for (var name in command.commands) | ||
{ | ||
var maxNameLength = MIN_OFFSET - 2; | ||
var lines = Object.keys(command.commands).sort().map(function(name){ | ||
var cmd = command.commands[name]; | ||
var args = ''; | ||
/*var args = cmd._args.map(function(arg){ | ||
return arg.required | ||
? '<' + arg.name + '>' | ||
: '[' + arg.name + ']'; | ||
}).join(' ');*/ | ||
var line = { | ||
name: chalk.green(name) + | ||
(cmd.hasOptions() ? ' [<options>]' : '') + | ||
(cmd._args && cmd._args.length ? ' [<args>]' : ''), | ||
description: cmd.description_ || '' | ||
}; | ||
lines.push( | ||
' ' + | ||
pad(22, name + (cmd.hasOptions() ? ' [options]' : '') + ' ' + args) + | ||
(cmd.description_ ? ' ' + cmd.description_ : '') | ||
); | ||
} | ||
maxNameLength = Math.max(maxNameLength, stringLength(line.name)); | ||
return line; | ||
}); | ||
return [ | ||
@@ -750,3 +793,5 @@ '', | ||
'', | ||
lines.join('\n'), | ||
lines.map(function(line){ | ||
return ' ' + pad(maxNameLength, line.name) + ' ' + breakByLines(line.description, maxNameLength + 4); | ||
}).join('\n'), | ||
'' | ||
@@ -760,10 +805,16 @@ ].join('\n'); | ||
var options = []; | ||
var maxNameLength = MIN_OFFSET - 2; | ||
var lines = Object.keys(command.long).sort().map(function(name){ | ||
var option = command.long[name]; | ||
var line = { | ||
name: option.usage.replace(/(^|\s)(-[^\s,]+)/ig, function(m, p, flag){ | ||
return p + chalk.yellow(flag); | ||
}), | ||
description: option.description | ||
}; | ||
for (var key in command.long) | ||
options.push(command.long[key]); | ||
maxNameLength = Math.max(maxNameLength, stringLength(line.name)); | ||
var width = options.reduce(function(res, option){ | ||
return Math.max(res, option.usage.length); | ||
}, 0); | ||
return line; | ||
}); | ||
@@ -775,4 +826,4 @@ // Prepend the help information | ||
'', | ||
options.map(function(option){ | ||
return ' ' + pad(width, option.usage) + ' ' + option.description; | ||
lines.map(function(line){ | ||
return ' ' + pad(maxNameLength, line.name) + ' ' + breakByLines(line.description, maxNameLength + 4); | ||
}).join('\n'), | ||
@@ -784,3 +835,6 @@ '' | ||
var output = []; | ||
var chalk = require('chalk'); | ||
chalk.enabled = module.exports.color && process.stdout.isTTY; | ||
if (command.description_) | ||
@@ -790,3 +844,6 @@ output.push(command.description_ + '\n'); | ||
output.push( | ||
'Usage:\n\n ' + command.name + (command.hasOptions() ? ' [options]' : '') + (command.hasCommands() ? ' [command]' : ''), | ||
'Usage:\n\n ' + | ||
chalk.cyan(commandsPath ? commandsPath.join(' ') : command.name) + | ||
(command.hasOptions() ? ' [' + chalk.yellow('<options>') + ']' : '') + | ||
(command.hasCommands() ? ' [' + chalk.green('<command>') + ']' : ''), | ||
commandsHelp() + | ||
@@ -805,2 +862,4 @@ optionsHelp() | ||
module.exports = { | ||
color: true, | ||
Error: ParseError, | ||
@@ -807,0 +866,0 @@ Argument: Argument, |
@@ -6,3 +6,3 @@ { | ||
"author": "Roman Dvornov <rdvornov@gmail.com>", | ||
"version": "1.0.0-beta", | ||
"version": "1.0.0-beta2", | ||
"keywords": ["cli", "command", "option", "argument", "completion"], | ||
@@ -17,5 +17,6 @@ "homepage": "https://github.com/lahmatiy/clap", | ||
"engines": { | ||
"node": ">=0.8.0" | ||
"node": ">=0.10.0" | ||
}, | ||
"dependencies": { | ||
"chalk": "0.5.1" | ||
}, | ||
@@ -22,0 +23,0 @@ "devDependencies": { |
20827
707
1
+ Addedchalk@0.5.1
+ Addedansi-regex@0.2.1(transitive)
+ Addedansi-styles@1.1.0(transitive)
+ Addedchalk@0.5.1(transitive)
+ Addedescape-string-regexp@1.0.5(transitive)
+ Addedhas-ansi@0.1.0(transitive)
+ Addedstrip-ansi@0.3.0(transitive)
+ Addedsupports-color@0.2.0(transitive)