Skip to content

Commit

Permalink
Only consider enabling autoHelp/autoVersion in case there is only one…
Browse files Browse the repository at this point in the history
… argument in `process.argv` (#114)

Co-Authored-By: Sindre Sorhus <sindresorhus@gmail.com>
  • Loading branch information
LitoMore and sindresorhus committed Jun 12, 2019
1 parent 54e1f22 commit cd29865
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 7 deletions.
4 changes: 4 additions & 0 deletions index.d.ts
Expand Up @@ -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;

Expand Down
12 changes: 7 additions & 5 deletions index.js
Expand Up @@ -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._;
Expand Down
4 changes: 4 additions & 0 deletions readme.md
Expand Up @@ -148,13 +148,17 @@ 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`<br>
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`<br>
Expand Down
25 changes: 23 additions & 2 deletions test.js
Expand Up @@ -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 <input>\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');
Expand Down Expand Up @@ -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');
});

0 comments on commit cd29865

Please sign in to comment.