diff --git a/fixture.js b/fixture.js index 7ea1c82..c60d421 100755 --- a/fixture.js +++ b/fixture.js @@ -7,7 +7,9 @@ const cli = meow({ help: ` Usage foo - `, + `, + autoVersion: process.argv.indexOf('--no-auto-version') === -1, + autoHelp: process.argv.indexOf('--no-auto-help') === -1, flags: { unicorn: {alias: 'u'}, meow: {default: 'dog'}, diff --git a/index.js b/index.js index 691dfe6..4673c43 100644 --- a/index.js +++ b/index.js @@ -30,7 +30,9 @@ module.exports = (helpMessage, opts) => { argv: process.argv.slice(2), inferType: false, input: 'string', - help: helpMessage + help: helpMessage, + autoHelp: true, + autoVersion: true }, opts); let minimistOpts = Object.assign({ @@ -65,12 +67,16 @@ module.exports = (helpMessage, opts) => { process.exit(typeof code === 'number' ? code : 2); }; - if (argv.version && opts.version !== false) { + const showVersion = () => { console.log(typeof opts.version === 'string' ? opts.version : pkg.version); process.exit(); + }; + + if (argv.version && opts.autoVersion) { + showVersion(); } - if (argv.help && opts.help !== false) { + if (argv.help && opts.autoHelp) { showHelp(0); } @@ -84,6 +90,7 @@ module.exports = (helpMessage, opts) => { flags, pkg, help, - showHelp + showHelp, + showVersion }; }; diff --git a/readme.md b/readme.md index d0da906..78f5a13 100644 --- a/readme.md +++ b/readme.md @@ -75,6 +75,7 @@ Returns an `Object` with: - `pkg` *(Object)* - The `package.json` object - `help` *(string)* - The help text used with `--help` - `showHelp([code=2])` *(Function)* - Show the help text and exit with `code` +- `showVersion()` *(Function)* - Show the version text and exit #### options @@ -126,8 +127,6 @@ The input is reindented and starting/ending newlines are trimmed which means you The description will be shown above your help text automatically. -Set it to `false` to disable it altogether. - ##### version Type: `string` `boolean`
@@ -135,7 +134,19 @@ Default: The package.json `"version"` property Set a custom version output. -Set it to `false` to disable it altogether. +##### autoHelp + +Type: `boolean`
+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. + +##### autoVersion + +Type: `boolean`
+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. ##### pkg diff --git a/test.js b/test.js index 0c3aa05..e6130ee 100644 --- a/test.js +++ b/test.js @@ -40,9 +40,19 @@ test('spawn cli and show version', async t => { t.is(stdout, pkg.version); }); +test('spawn cli and not show version', async t => { + const {stdout} = await execa('./fixture.js', ['--version', '--no-auto-version']); + t.is(stdout, 'version\nautoVersion\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', 2)); + t.is(stdout, indentString('\nCustom description\n\nUsage\n foo \n\n', 2)); +}); + +test('spawn cli and not show help screen', async t => { + const {stdout} = await execa('./fixture.js', ['--help', '--no-auto-help']); + t.is(stdout, 'help\nautoHelp\nmeow\ncamelCaseOption'); }); test('spawn cli and test input', async t => {