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

Add autoVersion and autoHelp options #68

Merged
merged 8 commits into from
Nov 26, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion fixture.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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