Skip to content

Commit

Permalink
feat: indent all flags with no char below each other (#863) (#872)
Browse files Browse the repository at this point in the history
Co-authored-by: Allan Oricil <55927613+AllanOricil@users.noreply.github.com>
  • Loading branch information
mdonnalley and AllanOricil authored Nov 13, 2023
1 parent 38629b0 commit 0fcd6ed
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
14 changes: 11 additions & 3 deletions src/help/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export class CommandHelp extends HelpFormatter {

if (!label) {
const labels = []
if (flag.char) labels.push(`-${flag.char[0]}`)
labels.push(flag.char ? `-${flag.char[0]}` : ' ')
if (flag.name) {
if (flag.type === 'boolean' && flag.allowNo) {
labels.push(`--[no-]${flag.name.trim()}`)
Expand All @@ -142,7 +142,7 @@ export class CommandHelp extends HelpFormatter {
}
}

label = labels.join(', ')
label = labels.join(flag.char ? ', ' : ' ')
}

if (flag.type === 'option') {
Expand All @@ -162,8 +162,12 @@ export class CommandHelp extends HelpFormatter {
protected flags(flags: Array<Command.Flag.Any>): [string, string | undefined][] | undefined {
if (flags.length === 0) return

const noChar = flags.reduce((previous, current) => previous && current.char === undefined, true)

return flags.map((flag) => {
const left = this.flagHelpLabel(flag)
let left = this.flagHelpLabel(flag)

if (noChar) left = left.replace(' ', '')

let right = flag.summary || flag.description || ''
if (flag.type === 'option' && flag.default) {
Expand All @@ -189,10 +193,14 @@ export class CommandHelp extends HelpFormatter {
// Guaranteed to be set because of the filter above, but make ts happy
const summary = flag.summary || ''
let flagHelp = this.flagHelpLabel(flag, true)

if (!flag.char) flagHelp = flagHelp.replace(' ', '')

flagHelp +=
flagHelp.length + summary.length + 2 < this.opts.maxWidth
? ' ' + summary
: '\n\n' + this.indent(this.wrap(summary, this.indentSpacing * 2))

return `${flagHelp}\n\n${this.indent(this.wrap(flag.description || '', this.indentSpacing * 2))}`
})
.join('\n\n')
Expand Down
8 changes: 4 additions & 4 deletions test/help/format-command-with-options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,10 @@ OPTIONS
barfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobar
-l=label
-r, --remote=remote
--force force it force it force it force it force it force
--force force it force it force it force it force it force
it force it force it force it force it force it
force it force it force it force it
--ss newliney
--ss newliney
newliney
newliney
newliney
Expand Down Expand Up @@ -106,10 +106,10 @@ OPTIONS
-f, --foo=foo foobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoo
barfoobarfoobarfoobarfoobarfoobar
-r, --remote=remote
--force force it force it force it force it force it force
--force force it force it force it force it force it force
it force it force it force it force it force it
force it force it force it force it
--ss newliney
--ss newliney
newliney
newliney
newliney
Expand Down
8 changes: 4 additions & 4 deletions test/help/format-command.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,10 @@ FLAGS
obarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobar
-l=<value>
-r, --remote=<value>
--force force it force it force it force it force it force
--force force it force it force it force it force it force
it force it force it force it force it force it
force it force it force it force it
--ss newliney
--ss newliney
newliney
newliney
newliney
Expand Down Expand Up @@ -115,10 +115,10 @@ FLAGS
-f, --foo=<value> foobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfoobarfo
obarfoobarfoobarfoobarfoobarfoobar
-r, --remote=<value>
--force force it force it force it force it force it force
--force force it force it force it force it force it force
it force it force it force it force it force it
force it force it force it force it
--ss newliney
--ss newliney
newliney
newliney
newliney
Expand Down

0 comments on commit 0fcd6ed

Please sign in to comment.