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

feat: improve flags indentation #863

Merged
merged 1 commit into from
Nov 13, 2023
Merged
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
14 changes: 11 additions & 3 deletions src/help/command.ts
Original file line number Diff line number Diff line change
@@ -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()}`)
@@ -142,7 +142,7 @@ export class CommandHelp extends HelpFormatter {
}
}

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

if (flag.type === 'option') {
@@ -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) {
@@ -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')
8 changes: 4 additions & 4 deletions test/help/format-command-with-options.test.ts
Original file line number Diff line number Diff line change
@@ -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
@@ -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
8 changes: 4 additions & 4 deletions test/help/format-command.test.ts
Original file line number Diff line number Diff line change
@@ -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
@@ -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