Skip to content

Commit

Permalink
Add default values to --help output (#39)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
leo authored Nov 14, 2016
2 parents 268b367 + 6de8688 commit 6f9afbd
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 6 deletions.
17 changes: 12 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ class Args {
case parseInt:
return ['<n>', parseInt]
default:
return false
return ['']
}
}

Expand Down Expand Up @@ -197,7 +197,7 @@ class Args {

// Set option defaults
for (const option of this.details.options) {
if (!option.defaultValue) {
if (typeof option.defaultValue === 'undefined') {
continue
}

Expand Down Expand Up @@ -274,14 +274,21 @@ class Args {
})[0].usage.length

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

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

// Add some space around it as well
parts.push(' ' + chalk.yellow(usage) + ' ' + chalk.dim(item.description))
if (typeof defaultValue !== 'undefined') {
if (typeof defaultValue === 'boolean') {
description += ` (${defaultValue ? 'enabled' : 'disabled'} by default)`
} else {
description += ` (defaults to ${JSON.stringify(defaultValue)})`
}
}
parts.push(' ' + chalk.yellow(usage) + ' ' + chalk.dim(description))
}

return parts
Expand Down Expand Up @@ -334,7 +341,7 @@ class Args {

if (version) {
// If it exists, register it as a default option
this.option('version', 'Output the version number', version)
this.option('version', 'Output the version number')

// And immediately output it if used in command line
if (this.raw.v || this.raw.version) {
Expand Down
2 changes: 1 addition & 1 deletion test/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ test('command aliases', async t => {
}

result = await run('help')
const regexes = [/binary, b/, /cmd/, /-a, --abc \[value\]/]
const regexes = [/binary, b/, /cmd/, /-a, --abc \[value]/]
for (const regex of regexes) {
t.regex(result, regex)
}
Expand Down

0 comments on commit 6f9afbd

Please sign in to comment.