Skip to content

Commit cd29865

Browse files
LitoMoresindresorhus
andcommittedJun 12, 2019
Only consider enabling autoHelp/autoVersion in case there is only one argument in process.argv (#114)
Co-Authored-By: Sindre Sorhus <sindresorhus@gmail.com>
1 parent 54e1f22 commit cd29865

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed
 

‎index.d.ts

+4
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,15 @@ declare namespace meow {
5252

5353
/**
5454
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.
55+
56+
This option is only considered when there is only one argument in `process.argv`.
5557
*/
5658
readonly autoHelp?: boolean;
5759

5860
/**
5961
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.
62+
63+
This option is only considered when there is only one argument in `process.argv`.
6064
*/
6165
readonly autoVersion?: boolean;
6266

‎index.js

+7-5
Original file line numberDiff line numberDiff line change
@@ -96,12 +96,14 @@ const meow = (helpText, options) => {
9696
process.exit();
9797
};
9898

99-
if (argv.version && options.autoVersion) {
100-
showVersion();
101-
}
99+
if (argv._.length === 0 && options.argv.length === 1) {
100+
if (argv.version === true && options.autoVersion) {
101+
showVersion();
102+
}
102103

103-
if (argv.help && options.autoHelp) {
104-
showHelp(0);
104+
if (argv.help === true && options.autoHelp) {
105+
showHelp(0);
106+
}
105107
}
106108

107109
const input = argv._;

‎readme.md

+4
Original file line numberDiff line numberDiff line change
@@ -148,13 +148,17 @@ Default: `true`
148148

149149
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.
150150

151+
This option is only considered when there is only one argument in `process.argv`.
152+
151153
##### autoVersion
152154

153155
Type: `boolean`<br>
154156
Default: `true`
155157

156158
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.
157159

160+
This option is only considered when there is only one argument in `process.argv`.
161+
158162
##### pkg
159163

160164
Type: `object`<br>

‎test.js

+23-2
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,36 @@ test('spawn cli and show version', async t => {
4040
t.is(stdout, pkg.version);
4141
});
4242

43-
test('spawn cli and not show version', async t => {
43+
test('spawn cli and disabled autoVersion and autoHelp', async t => {
44+
const {stdout} = await execa('./fixture.js', ['--version', '--help']);
45+
t.is(stdout, 'version\nhelp\nmeow\ncamelCaseOption');
46+
});
47+
48+
test('spawn cli and disabled autoVersion', async t => {
4449
const {stdout} = await execa('./fixture.js', ['--version', '--no-auto-version']);
4550
t.is(stdout, 'version\nautoVersion\nmeow\ncamelCaseOption');
4651
});
4752

53+
test('spawn cli and not show version', async t => {
54+
const {stdout} = await execa('./fixture.js', ['--version=beta']);
55+
t.is(stdout, 'version\nmeow\ncamelCaseOption');
56+
});
57+
4858
test('spawn cli and show help screen', async t => {
4959
const {stdout} = await execa('./fixture.js', ['--help']);
5060
t.is(stdout, indentString('\nCustom description\n\nUsage\n foo <input>\n\n', 2));
5161
});
5262

53-
test('spawn cli and not show help screen', async t => {
63+
test('spawn cli and disabled autoHelp', async t => {
5464
const {stdout} = await execa('./fixture.js', ['--help', '--no-auto-help']);
5565
t.is(stdout, 'help\nautoHelp\nmeow\ncamelCaseOption');
5666
});
5767

68+
test('spawn cli and not show help', async t => {
69+
const {stdout} = await execa('./fixture.js', ['--help=all']);
70+
t.is(stdout, 'help\nmeow\ncamelCaseOption');
71+
});
72+
5873
test('spawn cli and test input', async t => {
5974
const {stdout} = await execa('./fixture.js', ['-u', 'cat']);
6075
t.is(stdout, 'unicorn\nmeow\ncamelCaseOption');
@@ -219,3 +234,9 @@ test('grouped flags work', t => {
219234
t.is(flags.c, undefined);
220235
t.is(flags.l, undefined);
221236
});
237+
238+
test('disable autoVersion/autoHelp if `cli.input.length > 0`', t => {
239+
t.is(meow({argv: ['bar', '--version']}).input[0], 'bar');
240+
t.is(meow({argv: ['bar', '--help']}).input[0], 'bar');
241+
t.is(meow({argv: ['bar', '--version', '--help']}).input[0], 'bar');
242+
});

0 commit comments

Comments
 (0)
Please sign in to comment.