Skip to content
Permalink
Browse files

Add `autoVersion` and `autoHelp` options (#68)

  • Loading branch information...
angeloashmore authored and sindresorhus committed Nov 26, 2017
1 parent 4f7ded3 commit 59dda7a9caf8400ce602d2b1cf663aa1b35bdfca
Showing with 39 additions and 9 deletions.
  1. +3 −1 fixture.js
  2. +11 −4 index.js
  3. +14 −3 readme.md
  4. +11 −1 test.js
@@ -7,7 +7,9 @@ const cli = meow({
help: `
Usage
foo <input>
`,
`,
autoVersion: process.argv.indexOf('--no-auto-version') === -1,
autoHelp: process.argv.indexOf('--no-auto-help') === -1,
flags: {
unicorn: {alias: 'u'},
meow: {default: 'dog'},
@@ -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
};
};
@@ -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,16 +127,26 @@ 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`<br>
Default: The package.json `"version"` property

Set a custom version output.

Set it to `false` to disable it altogether.
##### autoHelp

Type: `boolean`<br>
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`<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.

##### pkg

12 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 <input>\n', 2));
t.is(stdout, indentString('\nCustom description\n\nUsage\n foo <input>\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 => {

0 comments on commit 59dda7a

Please sign in to comment.
You can’t perform that action at this time.