From cd29865dbe493cc1f72829eab5dcfb0a76490633 Mon Sep 17 00:00:00 2001 From: LitoMore Date: Wed, 12 Jun 2019 14:08:49 +0800 Subject: [PATCH] Only consider enabling autoHelp/autoVersion in case there is only one argument in `process.argv` (#114) Co-Authored-By: Sindre Sorhus --- index.d.ts | 4 ++++ index.js | 12 +++++++----- readme.md | 4 ++++ test.js | 25 +++++++++++++++++++++++-- 4 files changed, 38 insertions(+), 7 deletions(-) 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/index.js b/index.js index 5bcaf90..4120dfa 100644 --- a/index.js +++ b/index.js @@ -96,12 +96,14 @@ const meow = (helpText, options) => { process.exit(); }; - if (argv.version && options.autoVersion) { - showVersion(); - } + if (argv._.length === 0 && options.argv.length === 1) { + if (argv.version === true && options.autoVersion) { + showVersion(); + } - if (argv.help && options.autoHelp) { - showHelp(0); + if (argv.help === true && options.autoHelp) { + showHelp(0); + } } const input = argv._; diff --git a/readme.md b/readme.md index 107ca9e..50d7c27 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 is only considered 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 is only considered when there is only one argument in `process.argv`. + ##### pkg Type: `object`
diff --git a/test.js b/test.js index 38f2ef4..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'); @@ -219,3 +234,9 @@ test('grouped flags work', t => { t.is(flags.c, undefined); t.is(flags.l, undefined); }); + +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'); +});