Skip to content

Commit

Permalink
Add autoVersion and autoHelp options (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
angeloashmore authored and sindresorhus committed Nov 26, 2017
1 parent 4f7ded3 commit 59dda7a
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 9 deletions.
4 changes: 3 additions & 1 deletion fixture.js
Expand Up @@ -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'},
Expand Down
15 changes: 11 additions & 4 deletions index.js
Expand Up @@ -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({
Expand Down Expand Up @@ -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);
}

Expand All @@ -84,6 +90,7 @@ module.exports = (helpMessage, opts) => {
flags,
pkg,
help,
showHelp
showHelp,
showVersion
};
};
17 changes: 14 additions & 3 deletions readme.md
Expand Up @@ -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

Expand Down Expand Up @@ -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

Expand Down
12 changes: 11 additions & 1 deletion test.js
Expand Up @@ -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 => {
Expand Down

0 comments on commit 59dda7a

Please sign in to comment.