2017-11-03 02:09:34 +02:00
|
|
|
const { wrap } = require('lib/string-utils.js');
|
2017-12-14 20:12:14 +02:00
|
|
|
const Setting = require('lib/models/Setting.js');
|
2019-07-30 09:35:42 +02:00
|
|
|
const { _ } = require('lib/locale.js');
|
2017-08-04 19:11:10 +02:00
|
|
|
|
|
|
|
const MAX_WIDTH = 78;
|
|
|
|
const INDENT = ' ';
|
|
|
|
|
2017-10-20 00:02:13 +02:00
|
|
|
function renderTwoColumnData(options, baseIndent, width) {
|
2020-03-14 01:46:14 +02:00
|
|
|
const output = [];
|
2017-08-04 19:11:10 +02:00
|
|
|
const optionColWidth = getOptionColWidth(options);
|
|
|
|
|
|
|
|
for (let i = 0; i < options.length; i++) {
|
2020-03-14 01:46:14 +02:00
|
|
|
const option = options[i];
|
2017-08-04 19:11:10 +02:00
|
|
|
const flag = option[0];
|
|
|
|
const indent = baseIndent + INDENT + ' '.repeat(optionColWidth + 2);
|
2019-07-30 09:35:42 +02:00
|
|
|
|
2017-10-09 00:34:01 +02:00
|
|
|
let r = wrap(option[1], indent, width);
|
2017-08-04 19:11:10 +02:00
|
|
|
r = r.substr(flag.length + (baseIndent + INDENT).length);
|
|
|
|
r = baseIndent + INDENT + flag + r;
|
|
|
|
output.push(r);
|
|
|
|
}
|
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
return output.join('\n');
|
2017-08-04 19:11:10 +02:00
|
|
|
}
|
|
|
|
|
2017-10-09 00:34:01 +02:00
|
|
|
function renderCommandHelp(cmd, width = null) {
|
|
|
|
if (width === null) width = MAX_WIDTH;
|
|
|
|
|
2017-08-04 19:11:10 +02:00
|
|
|
const baseIndent = '';
|
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const output = [];
|
2017-08-04 19:11:10 +02:00
|
|
|
output.push(baseIndent + cmd.usage());
|
|
|
|
output.push('');
|
2017-10-09 00:34:01 +02:00
|
|
|
output.push(wrap(cmd.description(), baseIndent + INDENT, width));
|
2017-08-04 19:11:10 +02:00
|
|
|
|
2017-10-20 00:02:13 +02:00
|
|
|
const optionString = renderTwoColumnData(cmd.options(), baseIndent, width);
|
2017-08-04 19:11:10 +02:00
|
|
|
|
|
|
|
if (optionString) {
|
|
|
|
output.push('');
|
|
|
|
output.push(optionString);
|
|
|
|
}
|
2017-10-20 00:02:13 +02:00
|
|
|
|
|
|
|
if (cmd.name() === 'config') {
|
2019-07-30 09:35:42 +02:00
|
|
|
const renderMetadata = md => {
|
2020-03-14 01:46:14 +02:00
|
|
|
const desc = [];
|
2017-10-24 22:22:57 +02:00
|
|
|
|
2017-10-20 00:02:13 +02:00
|
|
|
if (md.label) {
|
|
|
|
let label = md.label();
|
|
|
|
if (label.length && label[label.length - 1] !== '.') label += '.';
|
|
|
|
desc.push(label);
|
|
|
|
}
|
|
|
|
|
2018-03-02 20:16:48 +02:00
|
|
|
const description = Setting.keyDescription(md.key, 'cli');
|
|
|
|
if (description) desc.push(description);
|
2017-10-24 22:22:57 +02:00
|
|
|
|
2017-10-20 00:02:13 +02:00
|
|
|
desc.push(_('Type: %s.', md.isEnum ? _('Enum') : Setting.typeToString(md.type)));
|
|
|
|
if (md.isEnum) desc.push(_('Possible values: %s.', Setting.enumOptionsDoc(md.key, '%s (%s)')));
|
|
|
|
|
|
|
|
let defaultString = null;
|
|
|
|
|
|
|
|
if ('value' in md) {
|
|
|
|
if (md.type === Setting.TYPE_STRING) {
|
2019-09-19 23:51:18 +02:00
|
|
|
defaultString = md.value ? `"${md.value}"` : null;
|
2017-10-20 00:02:13 +02:00
|
|
|
} else if (md.type === Setting.TYPE_INT) {
|
|
|
|
defaultString = (md.value ? md.value : 0).toString();
|
|
|
|
} else if (md.type === Setting.TYPE_BOOL) {
|
2019-07-30 09:35:42 +02:00
|
|
|
defaultString = md.value === true ? 'true' : 'false';
|
2017-10-20 00:02:13 +02:00
|
|
|
}
|
|
|
|
}
|
2019-07-30 09:35:42 +02:00
|
|
|
|
2017-10-20 00:02:13 +02:00
|
|
|
if (defaultString !== null) desc.push(_('Default: %s', defaultString));
|
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
return [md.key, desc.join('\n')];
|
2017-10-20 00:02:13 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
output.push('');
|
|
|
|
output.push(_('Possible keys/values:'));
|
|
|
|
output.push('');
|
|
|
|
|
2020-03-14 01:46:14 +02:00
|
|
|
const keysValues = [];
|
2017-10-20 00:02:13 +02:00
|
|
|
const keys = Setting.keys(true, 'cli');
|
|
|
|
for (let i = 0; i < keys.length; i++) {
|
2019-07-30 09:35:42 +02:00
|
|
|
if (keysValues.length) keysValues.push(['', '']);
|
2017-10-20 00:02:13 +02:00
|
|
|
const md = Setting.settingMetadata(keys[i]);
|
|
|
|
if (!md.label) continue;
|
|
|
|
keysValues.push(renderMetadata(md));
|
|
|
|
}
|
|
|
|
|
|
|
|
output.push(renderTwoColumnData(keysValues, baseIndent, width));
|
|
|
|
}
|
2019-07-30 09:35:42 +02:00
|
|
|
|
|
|
|
return output.join('\n');
|
2017-08-04 19:11:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
function getOptionColWidth(options) {
|
|
|
|
let output = 0;
|
|
|
|
for (let j = 0; j < options.length; j++) {
|
|
|
|
const option = options[j];
|
|
|
|
if (option[0].length > output) output = option[0].length;
|
|
|
|
}
|
|
|
|
return output;
|
|
|
|
}
|
|
|
|
|
2019-07-30 09:35:42 +02:00
|
|
|
module.exports = { renderCommandHelp };
|