Skip to content

Commit 6f9afbd

Browse files
authored
Add default values to --help output (#39)
* add default values to `--help` output Fixes #38. * fix `xo` lint * don't set a default value for `--version` We don't want to include it in the `--help` output as a default value. * use enabled/disabled for boolean default values In --help
2 parents 268b367 + 6de8688 commit 6f9afbd

2 files changed

Lines changed: 13 additions & 6 deletions

File tree

src/index.js

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,7 @@ class Args {
141141
case parseInt:
142142
return ['<n>', parseInt]
143143
default:
144-
return false
144+
return ['']
145145
}
146146
}
147147

@@ -197,7 +197,7 @@ class Args {
197197

198198
// Set option defaults
199199
for (const option of this.details.options) {
200-
if (!option.defaultValue) {
200+
if (typeof option.defaultValue === 'undefined') {
201201
continue
202202
}
203203

@@ -274,14 +274,21 @@ class Args {
274274
})[0].usage.length
275275

276276
for (const item of items) {
277-
let usage = item.usage
277+
let {usage, description, defaultValue} = item
278278
const difference = longest - usage.length
279279

280280
// Compensate the difference to longest property with spaces
281281
usage += ' '.repeat(difference)
282282

283283
// Add some space around it as well
284-
parts.push(' ' + chalk.yellow(usage) + ' ' + chalk.dim(item.description))
284+
if (typeof defaultValue !== 'undefined') {
285+
if (typeof defaultValue === 'boolean') {
286+
description += ` (${defaultValue ? 'enabled' : 'disabled'} by default)`
287+
} else {
288+
description += ` (defaults to ${JSON.stringify(defaultValue)})`
289+
}
290+
}
291+
parts.push(' ' + chalk.yellow(usage) + ' ' + chalk.dim(description))
285292
}
286293

287294
return parts
@@ -334,7 +341,7 @@ class Args {
334341

335342
if (version) {
336343
// If it exists, register it as a default option
337-
this.option('version', 'Output the version number', version)
344+
this.option('version', 'Output the version number')
338345

339346
// And immediately output it if used in command line
340347
if (this.raw.v || this.raw.version) {

test/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ test('command aliases', async t => {
8989
}
9090

9191
result = await run('help')
92-
const regexes = [/binary, b/, /cmd/, /-a, --abc \[value\]/]
92+
const regexes = [/binary, b/, /cmd/, /-a, --abc \[value]/]
9393
for (const regex of regexes) {
9494
t.regex(result, regex)
9595
}

0 commit comments

Comments
 (0)