From 27bdfad05fde357bd860fd590d4e19aba0233e58 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Tue, 1 Nov 2016 17:42:35 -0700 Subject: [PATCH 1/4] add default values to `--help` output Fixes #38. --- src/index.js | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index ff98cb0..5fcef93 100644 --- a/src/index.js +++ b/src/index.js @@ -141,7 +141,7 @@ class Args { case parseInt: return ['', parseInt] default: - return false + return [''] } } @@ -274,14 +274,17 @@ class Args { })[0].usage.length for (const item of items) { - let usage = item.usage + let { usage, description } = 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 ('undefined' !== typeof item.defaultValue) { + description += ` (defaults to ${JSON.stringify(item.defaultValue)})` + } + parts.push(' ' + chalk.yellow(usage) + ' ' + chalk.dim(description)) } return parts From 2b6f32fe7b7cda768f943e5b77d7e31812c0f2d1 Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Tue, 1 Nov 2016 18:05:09 -0700 Subject: [PATCH 2/4] fix `xo` lint --- src/index.js | 4 ++-- test/index.js | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/index.js b/src/index.js index 5fcef93..48fd7ba 100644 --- a/src/index.js +++ b/src/index.js @@ -274,14 +274,14 @@ class Args { })[0].usage.length for (const item of items) { - let { usage, description } = item + let {usage, description} = item const difference = longest - usage.length // Compensate the difference to longest property with spaces usage += ' '.repeat(difference) // Add some space around it as well - if ('undefined' !== typeof item.defaultValue) { + if (typeof item.defaultValue !== 'undefined') { description += ` (defaults to ${JSON.stringify(item.defaultValue)})` } parts.push(' ' + chalk.yellow(usage) + ' ' + chalk.dim(description)) diff --git a/test/index.js b/test/index.js index 192d5ee..9e6e7b6 100644 --- a/test/index.js +++ b/test/index.js @@ -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) } From 83df1046bb90c323ce207f11ad8dd304cfbe0fed Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Wed, 2 Nov 2016 17:15:27 -0700 Subject: [PATCH 3/4] don't set a default value for `--version` We don't want to include it in the `--help` output as a default value. --- src/index.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 48fd7ba..ae953f0 100644 --- a/src/index.js +++ b/src/index.js @@ -337,7 +337,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) { From 6de8688979d8586956cf211f3d0490c5f0026cee Mon Sep 17 00:00:00 2001 From: Nathan Rajlich Date: Wed, 2 Nov 2016 17:23:22 -0700 Subject: [PATCH 4/4] use enabled/disabled for boolean default values In --help --- src/index.js | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/index.js b/src/index.js index ae953f0..2705ae3 100644 --- a/src/index.js +++ b/src/index.js @@ -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 } @@ -274,15 +274,19 @@ class Args { })[0].usage.length for (const item of items) { - let {usage, description} = item + 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 - if (typeof item.defaultValue !== 'undefined') { - description += ` (defaults to ${JSON.stringify(item.defaultValue)})` + 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)) }