fix: simplify option help output, fix default value display

This commit is contained in:
2024-01-30 01:18:23 +02:00
committed by Chen Asraf
parent cd83a3484c
commit b3075a03b3
2 changed files with 28 additions and 37 deletions

View File

@@ -423,46 +423,30 @@ function generateHelpTable<T extends GenerateTableCommandConfig | GenerateTableO
const nameStyle = (name: string) => format(name, config.nameStyle)
const descStyle = (desc: string) => format(desc, config.descriptionStyle)
const table = rows.map((row) => {
const nameLines = row.name.split('\n').map((l) => nameStyle(l).padEnd(maxNameLength! + 2))
let description = descStyle(row.description)
if (displayDefaultValue && row.defaultValue != null) {
description += ` ${format(`(default: ${row.defaultValue})`, config.defaultValueStyle)}`
}
const firstNameLine = nameStyle(stripStyle(nameLines[0]).padEnd(maxNameLength! + 2))
const length =
stripStyle(firstNameLine).padEnd(maxNameLength! + 2).length + stripStyle(description).length
if (length <= lineLength) {
let line = `${firstNameLine}${description}`
if (nameLines.length > 1) {
line += `\n${nameLines.slice(1).join('\n')}`
}
if (!compact) {
return `${line}\n`
}
return line
}
const nameLines = row.name.split('\n').map((l) => nameStyle(l.padEnd(maxNameLength! + 2)))
const descLines = wrap(row.description, lineLength - maxNameLength!)
.split('\n')
.map(descStyle)
const max = Math.max(nameLines.length, descLines.length)
const subRows: string[] = []
const words = description.split(' ')
let currentRow = firstNameLine
let rowCount = 0
for (const word of words) {
if (stripStyle(currentRow).length + stripStyle(word).length + 1 > lineLength) {
subRows.push(currentRow)
rowCount++
if (rowCount > 0 && rowCount < nameLines.length) {
currentRow = nameStyle(stripStyle(nameLines[rowCount]).padEnd(maxNameLength! + 2))
currentRow += format('', { ...config.descriptionStyle, reset: false })
} else {
currentRow = ' '.repeat(maxNameLength! + 2)
}
}
currentRow += `${word} `
for (let i = 0; i < max - 1; i++) {
subRows.push(`${(nameLines[i] ?? ' ').padEnd(maxNameLength! + 2)}${descLines[i] ?? ''}`)
}
if (rowCount > 0 && rowCount < nameLines.length) {
currentRow += ` ${nameLines[rowCount].padEnd(maxNameLength! + 2)}`
const defaultText =
displayDefaultValue && row.defaultValue != null
? format(`(default: ${row.defaultValue})`, config.defaultValueStyle)
: ''
if (subRows.length + 1 + stripStyle(defaultText).length <= lineLength) {
subRows.push(
`${(nameLines[max - 1] ?? ' ').padEnd(maxNameLength! + 2)}${descLines[max - 1] ?? ''}${defaultText}`,
)
} else {
subRows.push(
`${(nameLines[max - 1] ?? ' ').padEnd(maxNameLength! + 2)}${descLines[max - 1] ?? ''}`,
)
subRows.push(defaultText.padStart(maxNameLength! + 2))
}
subRows.push(currentRow)
if (!compact) {
subRows.push('')

View File

@@ -109,11 +109,18 @@ const main = massarg<A>({
.flag({
name: 'bool2',
description:
'Ad consequat eiusmod officia aliqua. Eiusmod officia aliqua amet et laboris. Aliqua amet et laboris officia proident. Et, laboris officia proident minim duis officia. Proident minim, duis officia.',
'Ad consequat eiusmod officia aliqua. Eiusmod officia aliqua amet et laboris. Aliqua amet et laboris officia proident. Et, laboris officia proident minim duis officia. Proident minim, duis officia. Non commodo ...',
aliases: ['e'],
negatable: true,
defaultValue: false,
})
.flag({
name: 'bool3',
description: 'Ad consequat eiusmod officia aliqua. Eiusmod officia aliqua ',
aliases: ['e22'],
negatable: true,
defaultValue: false,
})
.option({
name: 'string',
description: