From e566755383daf22ac244ea14cd3cf820fdde3b2c Mon Sep 17 00:00:00 2001 From: LitoMore Date: Fri, 22 Feb 2019 15:57:28 +0800 Subject: [PATCH 1/8] Disable version/help output when cli.input is present --- index.js | 4 ++-- test.js | 10 ++++++++++ 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 8db102e..4c24b84 100644 --- a/index.js +++ b/index.js @@ -96,11 +96,11 @@ module.exports = (helpText, options) => { process.exit(); }; - if (argv.version && options.autoVersion) { + if (argv.version && argv._.length === 0 && options.autoVersion) { showVersion(); } - if (argv.help && options.autoHelp) { + if (argv.help && argv._.length === 0 && options.autoHelp) { showHelp(0); } diff --git a/test.js b/test.js index 38f2ef4..65a027d 100644 --- a/test.js +++ b/test.js @@ -45,6 +45,16 @@ test('spawn cli and not show version', async t => { t.is(stdout, 'version\nautoVersion\nmeow\ncamelCaseOption'); }); +test('spwan cli test input without version output', async t => { + const {stdout} = await execa('./fixture.js', ['bar', '--version']); + t.is(stdout, 'version\nmeow\ncamelCaseOption'); +}); + +test('spwan cli test input without help output', async t => { + const {stdout} = await execa('./fixture.js', ['bar', '--help']); + t.is(stdout, 'help\nmeow\ncamelCaseOption'); +}); + test('spawn cli and show help screen', async t => { const {stdout} = await execa('./fixture.js', ['--help']); t.is(stdout, indentString('\nCustom description\n\nUsage\n foo \n\n', 2)); From 0d10d200b0a98b86e8b336940049ab3935280cee Mon Sep 17 00:00:00 2001 From: LitoMore Date: Sun, 24 Feb 2019 12:07:19 +0800 Subject: [PATCH 2/8] Update tests --- test.js | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/test.js b/test.js index 65a027d..1934b8f 100644 --- a/test.js +++ b/test.js @@ -45,16 +45,6 @@ test('spawn cli and not show version', async t => { t.is(stdout, 'version\nautoVersion\nmeow\ncamelCaseOption'); }); -test('spwan cli test input without version output', async t => { - const {stdout} = await execa('./fixture.js', ['bar', '--version']); - t.is(stdout, 'version\nmeow\ncamelCaseOption'); -}); - -test('spwan cli test input without help output', async t => { - const {stdout} = await execa('./fixture.js', ['bar', '--help']); - t.is(stdout, 'help\nmeow\ncamelCaseOption'); -}); - test('spawn cli and show help screen', async t => { const {stdout} = await execa('./fixture.js', ['--help']); t.is(stdout, indentString('\nCustom description\n\nUsage\n foo \n\n', 2)); @@ -229,3 +219,9 @@ test('grouped flags work', t => { t.is(flags.c, undefined); t.is(flags.l, undefined); }); + +test('only show verson/help if `cli.input.length === 0`', t => { + t.is(meow({argv: ['bar', '--version']}).input[0], 'bar'); + t.is(meow({argv: ['bar', '--help']}).input[0], 'bar'); + t.is(meow({argv: ['bar', '--version', '--help']}).input[0], 'bar'); +}); From a8f06503975b67d413f7cfb2c6a9ac9e941cc67c Mon Sep 17 00:00:00 2001 From: LitoMore Date: Sun, 24 Feb 2019 12:20:22 +0800 Subject: [PATCH 3/8] Update readme --- readme.md | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/readme.md b/readme.md index a0725a5..01e4d37 100644 --- a/readme.md +++ b/readme.md @@ -10,8 +10,8 @@ - Parses arguments - Converts flags to [camelCase](https://github.com/sindresorhus/camelcase) - Negates flags when using the `--no-` prefix -- Outputs version when `--version` -- Outputs description and supplied help text when `--help` +- Outputs version when only the `--version` +- Outputs description and supplied help text when only the `--help` - Makes unhandled rejected promises [fail hard](https://github.com/sindresorhus/hard-rejection) instead of the default silent fail - Sets the process title to the binary name defined in package.json From 98cbae9c15e286c1e26f91acb0aa4ccccc6b83da Mon Sep 17 00:00:00 2001 From: LitoMore Date: Thu, 28 Feb 2019 01:41:42 +0800 Subject: [PATCH 4/8] Only enable autoVersion/autoHelp when there is only one argument --- index.js | 12 +++++++----- readme.md | 11 ++++++++--- test.js | 21 ++++++++++++++++++--- 3 files changed, 33 insertions(+), 11 deletions(-) diff --git a/index.js b/index.js index 4c24b84..468896c 100644 --- a/index.js +++ b/index.js @@ -96,12 +96,14 @@ module.exports = (helpText, options) => { process.exit(); }; - if (argv.version && argv._.length === 0 && options.autoVersion) { - showVersion(); - } + if (argv._.length === 0 && options.argv.length === 1) { + if (argv.version === true && options.autoVersion) { + showVersion(); + } - if (argv.help && argv._.length === 0 && options.autoHelp) { - showHelp(0); + if (argv.help === true && options.autoHelp) { + showHelp(0); + } } const input = argv._; diff --git a/readme.md b/readme.md index 01e4d37..c03b1e0 100644 --- a/readme.md +++ b/readme.md @@ -10,8 +10,8 @@ - Parses arguments - Converts flags to [camelCase](https://github.com/sindresorhus/camelcase) - Negates flags when using the `--no-` prefix -- Outputs version when only the `--version` -- Outputs description and supplied help text when only the `--help` +- Outputs version when `--version` +- Outputs description and supplied help text when `--help` - Makes unhandled rejected promises [fail hard](https://github.com/sindresorhus/hard-rejection) instead of the default silent fail - Sets the process title to the binary name defined in package.json @@ -260,6 +260,12 @@ Meow will make unhandled rejected promises [fail hard](https://github.com/sindre ## Tips +### `autoVersion` and `autoHelp` + +The `autoVersion` and `autoHelp` option will only enabled when there is only one argument in argv. + +## Related + See [`chalk`](https://github.com/chalk/chalk) if you want to colorize the terminal output. See [`get-stdin`](https://github.com/sindresorhus/get-stdin) if you want to accept input from stdin. @@ -270,7 +276,6 @@ See [`update-notifier`](https://github.com/yeoman/update-notifier) if you want u [More useful CLI utilities…](https://github.com/sindresorhus/awesome-nodejs#command-line-utilities) - ## License MIT © [Sindre Sorhus](https://sindresorhus.com) diff --git a/test.js b/test.js index 1934b8f..ef60764 100644 --- a/test.js +++ b/test.js @@ -40,21 +40,36 @@ test('spawn cli and show version', async t => { t.is(stdout, pkg.version); }); -test('spawn cli and not show version', async t => { +test('spawn cli and disabled autoVersion and autoHelp', async t => { + const {stdout} = await execa('./fixture.js', ['--version', '--help']); + t.is(stdout, 'version\nhelp\nmeow\ncamelCaseOption'); +}); + +test('spawn cli and disabled autoVersion', async t => { const {stdout} = await execa('./fixture.js', ['--version', '--no-auto-version']); t.is(stdout, 'version\nautoVersion\nmeow\ncamelCaseOption'); }); +test('spawn cli and not show version', async t => { + const {stdout} = await execa('./fixture.js', ['--version=beta']); + t.is(stdout, 'version\nmeow\ncamelCaseOption'); +}); + test('spawn cli and show help screen', async t => { const {stdout} = await execa('./fixture.js', ['--help']); t.is(stdout, indentString('\nCustom description\n\nUsage\n foo \n\n', 2)); }); -test('spawn cli and not show help screen', async t => { +test('spawn cli and disabled autoHelp', async t => { const {stdout} = await execa('./fixture.js', ['--help', '--no-auto-help']); t.is(stdout, 'help\nautoHelp\nmeow\ncamelCaseOption'); }); +test('spawn cli and not show help', async t => { + const {stdout} = await execa('./fixture.js', ['--help=all']); + t.is(stdout, 'help\nmeow\ncamelCaseOption'); +}); + test('spawn cli and test input', async t => { const {stdout} = await execa('./fixture.js', ['-u', 'cat']); t.is(stdout, 'unicorn\nmeow\ncamelCaseOption'); @@ -220,7 +235,7 @@ test('grouped flags work', t => { t.is(flags.l, undefined); }); -test('only show verson/help if `cli.input.length === 0`', t => { +test('disable autoVersion/autoHelp if `cli.input.length > 0`', t => { t.is(meow({argv: ['bar', '--version']}).input[0], 'bar'); t.is(meow({argv: ['bar', '--help']}).input[0], 'bar'); t.is(meow({argv: ['bar', '--version', '--help']}).input[0], 'bar'); From adbad2df2eba6c4ac390981faa63bf2c1c02620b Mon Sep 17 00:00:00 2001 From: LitoMore Date: Tue, 4 Jun 2019 13:16:54 +0800 Subject: [PATCH 5/8] Update readme.md Co-Authored-By: Sindre Sorhus --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index c03b1e0..41ac8f8 100644 --- a/readme.md +++ b/readme.md @@ -262,7 +262,7 @@ Meow will make unhandled rejected promises [fail hard](https://github.com/sindre ### `autoVersion` and `autoHelp` -The `autoVersion` and `autoHelp` option will only enabled when there is only one argument in argv. +The `autoVersion` and `autoHelp` option will only enabled when there is only one argument in `process.argv`. ## Related From 110beabdf8d650b973296c033d62fe1d01b59420 Mon Sep 17 00:00:00 2001 From: LitoMore Date: Tue, 4 Jun 2019 13:27:27 +0800 Subject: [PATCH 6/8] Update readme --- readme.md | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/readme.md b/readme.md index 06b56a7..572f914 100644 --- a/readme.md +++ b/readme.md @@ -148,6 +148,8 @@ Default: `true` Automatically show the help text when the `--help` flag is present. Useful to set this value to `false` when a CLI manages child CLIs with their own help text. +This option will only enabled when there is only one argument in `process.argv`. + ##### autoVersion Type: `boolean`
@@ -155,6 +157,8 @@ Default: `true` Automatically show the version text when the `--version` flag is present. Useful to set this value to `false` when a CLI manages child CLIs with their own version text. + This option will only enabled when there is only one argument in `process.argv`. + ##### pkg Type: `object`
@@ -262,12 +266,6 @@ Meow will make unhandled rejected promises [fail hard](https://github.com/sindre ## Tips -### `autoVersion` and `autoHelp` - -The `autoVersion` and `autoHelp` option will only enabled when there is only one argument in `process.argv`. - -## Related - See [`chalk`](https://github.com/chalk/chalk) if you want to colorize the terminal output. See [`get-stdin`](https://github.com/sindresorhus/get-stdin) if you want to accept input from stdin. From e99f2335d2fcaed2d9993d9c4d012eab477cff5f Mon Sep 17 00:00:00 2001 From: LitoMore Date: Wed, 12 Jun 2019 13:19:28 +0800 Subject: [PATCH 7/8] Update readme.md Co-Authored-By: Sindre Sorhus --- readme.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/readme.md b/readme.md index 572f914..ed0eea3 100644 --- a/readme.md +++ b/readme.md @@ -148,7 +148,7 @@ Default: `true` Automatically show the help text when the `--help` flag is present. Useful to set this value to `false` when a CLI manages child CLIs with their own help text. -This option will only enabled when there is only one argument in `process.argv`. +This option is only considered when there is only one argument in `process.argv`. ##### autoVersion From 03a629f83e2066babe2e40cf83791b5908566f1f Mon Sep 17 00:00:00 2001 From: LitoMore Date: Wed, 12 Jun 2019 13:24:28 +0800 Subject: [PATCH 8/8] Update docs --- index.d.ts | 4 ++++ readme.md | 3 ++- 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/index.d.ts b/index.d.ts index ec1834b..9df36f8 100644 --- a/index.d.ts +++ b/index.d.ts @@ -52,11 +52,15 @@ declare namespace meow { /** Automatically show the help text when the `--help` flag is present. Useful to set this value to `false` when a CLI manages child CLIs with their own help text. + + This option is only considered when there is only one argument in `process.argv`. */ readonly autoHelp?: boolean; /** Automatically show the version text when the `--version` flag is present. Useful to set this value to `false` when a CLI manages child CLIs with their own version text. + + This option is only considered when there is only one argument in `process.argv`. */ readonly autoVersion?: boolean; diff --git a/readme.md b/readme.md index ed0eea3..50d7c27 100644 --- a/readme.md +++ b/readme.md @@ -157,7 +157,7 @@ Default: `true` Automatically show the version text when the `--version` flag is present. Useful to set this value to `false` when a CLI manages child CLIs with their own version text. - This option will only enabled when there is only one argument in `process.argv`. + This option is only considered when there is only one argument in `process.argv`. ##### pkg @@ -276,6 +276,7 @@ See [`update-notifier`](https://github.com/yeoman/update-notifier) if you want u [More useful CLI utilities…](https://github.com/sindresorhus/awesome-nodejs#command-line-utilities) + ---