Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only consider enabling autoHelp/autoVersion in case there is only one argument in process.argv #114

Merged
merged 9 commits into from Jun 12, 2019
Merged
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
5 changes: 4 additions & 1 deletion 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 will only enabled when there is only one argument in `process.argv`.
LitoMore marked this conversation as resolved.
Show resolved Hide resolved
LitoMore marked this conversation as resolved.
Show resolved Hide resolved

##### 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 will only enabled when there is only one argument in `process.argv`.

##### pkg

Type: `object`<br>
Expand Down Expand Up @@ -272,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)


LitoMore marked this conversation as resolved.
Show resolved Hide resolved
---

<div align="center">
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');
});