Skip to content

Commit

Permalink
added basic optionSeparator() implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
efrancis-intuit committed May 19, 2016
1 parent ad2e539 commit 1adcfce
Showing 1 changed file with 34 additions and 4 deletions.
38 changes: 34 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ function Option(flags, description) {
this.description = description || '';
}

function OptionSeparator(index, title) {
this.index = index;
this.title = title;
}

/**
* Return option name.
*
Expand Down Expand Up @@ -82,6 +87,7 @@ Option.prototype.is = function(arg) {
function Command(name) {
this.commands = [];
this.options = [];
this.optionSeparators = [];
this._execs = {};
this._allowUnknownOption = false;
this._args = [];
Expand Down Expand Up @@ -369,7 +375,7 @@ Command.prototype.option = function(flags, description, fn, defaultValue) {
fn = function(val, def) {
var m = regex.exec(val);
return m ? m[0] : def;
}
};
}
else {
defaultValue = fn;
Expand Down Expand Up @@ -415,6 +421,11 @@ Command.prototype.option = function(flags, description, fn, defaultValue) {
return this;
};

Command.prototype.optionSeparator = function(title) {
this.optionSeparators.push(new OptionSeparator(this.options.length, title));
return this;
}

/**
* Allow unknown options on the command line.
*
Expand Down Expand Up @@ -903,6 +914,16 @@ Command.prototype.largestOptionLength = function() {
}, 0);
};


Command.prototype.findOptionSeparator = function(index) {
for (var i = 0; i < this.optionSeparators.length; i++) {
if (this.optionSeparators[i].index === index) {
return this.optionSeparators[i];
}
}
return null;
}

/**
* Return help for options.
*
Expand All @@ -913,10 +934,19 @@ Command.prototype.largestOptionLength = function() {
Command.prototype.optionHelp = function() {
var width = this.largestOptionLength();

var self = this;

// Prepend the help information
return [pad('-h, --help', width) + ' ' + 'output usage information']
.concat(this.options.map(function(option) {
return pad(option.flags, width) + ' ' + option.description;
.concat(this.options.map(function(option, index) {
let optionRes = pad(option.flags, width) + ' ' + option.description;
var separator = self.findOptionSeparator(index);
let res = '';
if (separator) {
res += '------------------------------- ' + separator.title + ' -------------------------------\r\n';
}
res += optionRes;
return res;
}))
.join('\n');
};
Expand Down Expand Up @@ -1018,7 +1048,7 @@ Command.prototype.outputHelp = function(cb) {
if (!cb) {
cb = function(passthru) {
return passthru;
}
};
}
process.stdout.write(cb(this.helpInformation()));
this.emit('--help');
Expand Down

0 comments on commit 1adcfce

Please sign in to comment.