Merge pull request #208 from zxqfox/feature/descriptions-multiline-support
Before adoption: this commit came in with the history the project adopted, so no landing records it.
multiline descriptions support
authored by Randall Koutnik · committed by Randall Koutnik · parents e326d94, 4931c33 · Browse files · 6 files·+83 added−6 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
6 files+83 added−6 removed
- tests:+55 added−0 removed
- other:+28 added−6 removed
Provenance
- Commit
6134fe4- Tree
7adff54- Author
Randall Koutnik <rkoutnik@gmail.com>- Committer
Randall Koutnik <rkoutnik@gmail.com>- Parent 1
e326d94- Parent 2
4931c33
6 files changed+83 added−6 removed
modified:examples/pizza+1 added-1 removed
10 unmodified lines11 .description('An application for pizzas ordering')12 .option('-p, --peppers', 'Add peppers')13 .option('-P, --pineapple', 'Add pineapple')14 .option('-b, --bbq', 'Add bbq sauce')15- .option('-c, --cheese <type>', 'Add the specified type of cheese [marble]')15+ .option('-c, --cheese <type>', 'Add the specified type\nof cheese [marble]')16 .option('-C, --no-cheese', 'You do not want any cheese')17 .parse(process.argv);18 19 console.log('you ordered a pizza with:');10 unmodified linesadded:test/test.command.description.js+27 added
123456789101112131415161718192021222324252627var program = require('../') , sinon = require('sinon').sandbox.create() , should = require('should');
sinon.stub(process, 'exit');sinon.stub(process.stdout, 'write');
program .command('mycommand [options]', 'this is my command with\nmultiple lines in description.\nyay folks!');
program.parse(['node', 'mycommand', '--help']);
process.exit.calledOnce.should.be.true;process.exit.calledWith(0).should.be.true;
process.stdout.write.calledOnce.should.be.true;process.stdout.write.args.length.should.equal(1);
var output = process.stdout.write.args[0];
sinon.restore();
output[0].should.containEql([ ' mycommand [options] this is my command with', ' multiple lines in description.', ' yay folks!'].join('\n'));added:test/test.command.usage.description.js+28 added
12345678910111213141516171819202122232425262728var program = require('../') , sinon = require('sinon').sandbox.create() , should = require('should');
sinon.stub(process, 'exit');sinon.stub(process.stdout, 'write');
program .command('cmd', 'usage doesn\'t work without description ;-(') .usage('[options] \\\n | xargs send --message \'yay folks!\' \\\n | wc -l');
program.parse(['node', 'cmd', '--help']);
process.exit.calledOnce.should.be.true;process.exit.calledWith(0).should.be.true;
process.stdout.write.calledOnce.should.be.true;process.stdout.write.args.length.should.equal(1);
var output = process.stdout.write.args[0];
sinon.restore();
output[0].should.containEql([ ' Usage: cmd [options] \\', ' | xargs send --message \'yay folks!\' \\', ' | wc -l'].join('\n'));modified:index.js+23 added-3 removed
871 unmodified lines872 873 // Prepend the help information874 return [pad('-h, --help', width) + ' ' + 'output usage information']875 .concat(this.options.map(function(option) {876- return pad(option.flags, width) + ' ' + option.description;876+ return pad(option.flags, width) + ' ' + offset(option.description, width + 2);877 }))878 .join('\n');879 };880 33 unmodified lines914 ''915 , ' Commands:'916 , ''917 , commands.map(function(cmd) {918- return pad(cmd[0], width) + ' ' + cmd[1];918+ return pad(cmd[0], width) + ' ' + offset(cmd[1], width + 2);919 }).join('\n').replace(/^/gm, ' ')920 , ''921 ].join('\n');922 };19 unmodified lines942 cmdName = cmdName + '|' + this._alias;943 }944 var usage = [945 ''946- ,' Usage: ' + cmdName + ' ' + this.usage()946+ ,' Usage: ' + cmdName + ' ' + offset(this.usage(), 9)947 , ''948 ];949 950 var cmds = [];65 unmodified lines1016 return str + Array(len + 1).join(' ');1017 }1018 1019 /**1020+ * Prepend to each line of `str` spaced string of `offset` length.1021+ *1022+ * @param {String} str1023+ * @param {Number} offset1024+ * @param {Boolean} skipFirstLine1025+ * @return {String}1026+ * @api private1027+ */1028+function offset(str, offset, skipFirstLine) {1029+ var res = String(str || '').replace(/^/gm, pad('', offset));1030+ if (!skipFirstLine) {1031+ res = res.replace(/^\s+/, '');1032+ }1033+ return res;1034+}1035+1036+/**1037 * Output help information if necessary1038 *1039 * @param {Command} command to output help for1040 * @param {Array} array of options to search for -h or --help5 unmodified lines1046 for (var i = 0; i < options.length; i++) {1047 if (options[i] == '--help' || options[i] == '-h') {1048 cmd.outputHelp();1049 process.exit(0);1050+1051+ // used for test flow only1052+ options.splice(i, 1);1053 }1054 }1055 }1056 15 unmodified linesmodified:package.json+2 added-1 removed
12 unmodified lines131415161717181920212211 unmodified lines12 unmodified lines "type": "git", "url": "https://github.com/tj/commander.js.git" }, "devDependencies": { "should": ">= 0.0.1" "should": ">= 0.0.1", "sinon": ">= 1.13.0" }, "scripts": { "test": "make test" },11 unmodified linesmodified:Readme.md+2 added-1 removed
167 unmodified lines168 -V, --version output the version number169 -p, --peppers Add peppers170 -P, --pineapple Add pineapple171 -b, --bbq Add bbq sauce172- -c, --cheese <type> Add the specified type of cheese [marble]172+ -c, --cheese <type> Add the specified type173+ of cheese [marble]174 -C, --no-cheese You do not want any cheese175 176 ```177 135 unmodified lines