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 @@ module.exports = (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
7 changes: 6 additions & 1 deletion readme.md
Expand Up @@ -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.
LitoMore marked this conversation as resolved.
Show resolved Hide resolved

## 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.
Expand All @@ -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)


LitoMore marked this conversation as resolved.
Show resolved Hide resolved
## License

MIT © [Sindre Sorhus](https://sindresorhus.com)
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');
});