Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements for extending & help template overwriting #1338

Closed
wants to merge 2 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
216 changes: 141 additions & 75 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,19 @@ class Option {
this.defaultValue = undefined;
}

/**
* Return option full description.
*
* @return {string}
* @api public
*/

fullDescription() {
return this.description + (
(!this.negate && this.defaultValue !== undefined) ? ' (default: ' + JSON.stringify(this.defaultValue) + ')' : ''
);
};

/**
* Return option name.
*
Expand Down Expand Up @@ -1499,34 +1512,7 @@ Read more on https://git.io/JJc0W`);
*/

optionHelp() {
const width = this.padWidth();
const columns = process.stdout.columns || 80;
const descriptionWidth = columns - width - 4;
function padOptionDetails(flags, description) {
return pad(flags, width) + ' ' + optionalWrap(description, descriptionWidth, width + 2);
};

// Explicit options (including version)
const help = this.options.map((option) => {
const fullDesc = option.description +
((!option.negate && option.defaultValue !== undefined) ? ' (default: ' + JSON.stringify(option.defaultValue) + ')' : '');
return padOptionDetails(option.flags, fullDesc);
});

// Implicit help
const showShortHelpFlag = this._hasHelpOption && this._helpShortFlag && !this._findOption(this._helpShortFlag);
const showLongHelpFlag = this._hasHelpOption && !this._findOption(this._helpLongFlag);
if (showShortHelpFlag || showLongHelpFlag) {
let helpFlags = this._helpFlags;
if (!showShortHelpFlag) {
helpFlags = this._helpLongFlag;
} else if (!showLongHelpFlag) {
helpFlags = this._helpShortFlag;
}
help.push(padOptionDetails(helpFlags, this._helpDescription));
}

return help.join('\n');
return this.helpData().options.join('\n');
};

/**
Expand All @@ -1539,51 +1525,106 @@ Read more on https://git.io/JJc0W`);
commandHelp() {
if (!this.commands.length && !this._lazyHasImplicitHelpCommand()) return '';

const commands = this.prepareCommands();
const width = this.padWidth();

const columns = process.stdout.columns || 80;
const descriptionWidth = columns - width - 4;

return [
'Commands:',
commands.map((cmd) => {
const desc = cmd[1] ? ' ' + cmd[1] : '';
return (desc ? pad(cmd[0], width) : cmd[0]) + optionalWrap(desc, descriptionWidth, width + 2);
}).join('\n').replace(/^/gm, ' '),
this.helpData().commands.join('\n'),
''
].join('\n');
};

/**
* Return program help documentation.
*
* @param {Object} params
* @return {string}
* @api public
*/

helpInformation() {
let desc = [];
if (this._description) {
desc = [
this._description,
helpInformation(params) {
params = params || {};

const help = this.helpData(params);

let output = [
'Usage: ' + help.usage,
''
];

if (help.description) {
output = output.concat([
help.description,
''
];
]);
}

const argsDescription = this._argsDescription;
if (argsDescription && this._args.length) {
const width = this.padWidth();
const columns = process.stdout.columns || 80;
const descriptionWidth = columns - width - 5;
desc.push('Arguments:');
desc.push('');
this._args.forEach((arg) => {
desc.push(' ' + pad(arg.name, width) + ' ' + wrap(argsDescription[arg.name] || '', descriptionWidth, width + 4));
});
desc.push('');
}
if (help.args.length) {
output = output.concat([
'Arguments:',
help.args.join('\n'),
''
]);
}

if (help.options.length) {
output = output.concat([
'Options:',
help.options.join('\n'),
''
]);
}

if (help.commands.length) {
output = output.concat([
'Commands:',
help.commands.join('\n'),
''
]);
}

const baseIndent = params.baseIndent || 0;
const baseIndentStr = Array(baseIndent + 1).join(' ');

return output.join('\n').replace(/^/gm, baseIndentStr).trimEnd() + '\n';
};

/**
* Return program help data.
*
* @param {Object} params
* @return {Object}
* @api public
*/

helpData(params) {
params = params || {};

const baseIndent = params.baseIndent || 0;
const valueIndent = params.valueIndent || 2;
const separatorWidth = params.separatorWidth || 2;

const width = this.padWidth() + baseIndent;
const columns = process.stdout.columns || 80;
const descriptionWidth = columns - width - baseIndent - (valueIndent + separatorWidth);

const valueIndentStr = Array(valueIndent + 1).join(' ');
const separatorStr = Array(separatorWidth + 1).join(' ');

const utils = {
pad: val => pad(val, width),
wrap: val => optionalWrap(val, descriptionWidth, width + separatorWidth)
};

const renderDetails = params.renderDetails || ((value, description, utils) => {
const data = description ? [utils.pad(value), utils.wrap(description)] : [value];
return data.join(separatorStr).replace(/^/gm, valueIndentStr);
});

function prepareDetails(value, description) {
return renderDetails(value, description, utils);
};

// USAGE

let cmdName = this._name;
if (this._aliases[0]) {
cmdName = cmdName + '|' + this._aliases[0];
Expand All @@ -1592,29 +1633,54 @@ Read more on https://git.io/JJc0W`);
for (let parentCmd = this.parent; parentCmd; parentCmd = parentCmd.parent) {
parentCmdNames = parentCmd.name() + ' ' + parentCmdNames;
}
const usage = [
'Usage: ' + parentCmdNames + cmdName + ' ' + this.usage(),
''
];
const usage = parentCmdNames + cmdName + ' ' + this.usage();

let cmds = [];
const commandHelp = this.commandHelp();
if (commandHelp) cmds = [commandHelp];
// DESCRIPTION

let options = [];
if (this._hasHelpOption || this.options.length > 0) {
options = [
'Options:',
'' + this.optionHelp().replace(/^/gm, ' '),
''
];
const description = this._description ? optionalWrap(this._description, width + descriptionWidth) : '';

// ARGUMENTS DESCRIPTION

const args = [];
if (this._argsDescription && this._args.length) {
this._args.forEach((arg) => {
args.push(prepareDetails(arg.name, this._argsDescription[arg.name] || ''));
});
}

// OPTIONS

// Explicit options (including version)
const options = this.options.map((option) => {
return prepareDetails(option.flags, option.fullDescription());
});

// Implicit help
const showShortHelpFlag = this._hasHelpOption && this._helpShortFlag && !this._findOption(this._helpShortFlag);
const showLongHelpFlag = this._hasHelpOption && !this._findOption(this._helpLongFlag);
if (showShortHelpFlag || showLongHelpFlag) {
let helpFlags = this._helpFlags;
if (!showShortHelpFlag) {
helpFlags = this._helpLongFlag;
} else if (!showLongHelpFlag) {
helpFlags = this._helpShortFlag;
}
options.push(prepareDetails(helpFlags, this._helpDescription));
}

return usage
.concat(desc)
.concat(options)
.concat(cmds)
.join('\n');
// COMMANDS

const commands = this.prepareCommands().map((cmd) => {
return prepareDetails(cmd[0], cmd[1]);
});

return {
usage,
description,
args,
options,
commands
};
};

/**
Expand Down
4 changes: 2 additions & 2 deletions tests/helpwrap.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ Options:
-h, --help display help for command

Commands:
alpha Lorem mollit quis dolor ex do eu quis ad
insa a commodo esse.
alpha Lorem mollit quis dolor ex do eu quis ad insa
a commodo esse.
help [command] display help for command
`;

Expand Down