Skip to content

Commit 0815980

Browse files
committed
fix: make sure --version appears in the help message of the default command
1 parent 702d41a commit 0815980

File tree

4 files changed

+33
-37
lines changed

4 files changed

+33
-37
lines changed

examples/help.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@ require('ts-node/register')
22
const cli = require('../src/index')()
33

44
cli.option('--type [type]', 'Choose a project type', {
5-
default: 'node'
5+
default: 'node',
66
})
77
cli.option('--name <name>', 'Provide your name')
88

99
cli.command('lint [...files]', 'Lint files').action((files, options) => {
1010
console.log(files, options)
1111
})
1212

13+
cli.command('[...files]', 'Run files').action((files, options) => {
14+
console.log('run', files, options)
15+
})
16+
1317
// Display help message when `-h` or `--help` appears
1418
cli.help()
1519
// Display version number when `-v` or `--version` appears

src/Command.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,7 @@ class Command {
183183
let options = this.isGlobalCommand
184184
? globalOptions
185185
: [...this.options, ...(globalOptions || [])]
186-
if (!this.isGlobalCommand) {
186+
if (!this.isGlobalCommand && !this.isDefaultCommand) {
187187
options = options.filter((option) => option.name !== 'version')
188188
}
189189
if (options.length > 0) {

src/__test__/__snapshots__/index.test.ts.snap

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -14,25 +14,6 @@ exports[`basic-usage: basic-usage 1`] = `
1414
}"
1515
`;
1616

17-
exports[`help: help 1`] = `
18-
"help.js/0.0.0
19-
20-
Usage:
21-
$ help.js <command> [options]
22-
23-
Commands:
24-
lint [...files] Lint files
25-
26-
For more info, run any command with the \`--help\` flag:
27-
$ help.js lint --help
28-
29-
Options:
30-
--type [type] Choose a project type (default: node)
31-
--name <name> Provide your name
32-
-h, --help Display this message
33-
-v, --version Display version number "
34-
`;
35-
3617
exports[`ignore-default-value: ignore-default-value 1`] = `
3718
"{
3819
\\"args\\": [],

src/__test__/index.test.ts

Lines changed: 27 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import path from 'path'
22
import execa from 'execa'
33
import cac from '..'
44

5-
function fixture(file: string) {
5+
function example(file: string) {
66
return path.relative(
77
process.cwd(),
88
path.join(__dirname, '../../examples', file)
@@ -12,40 +12,39 @@ function fixture(file: string) {
1212
function snapshotOutput({
1313
title,
1414
file,
15-
args
15+
args,
1616
}: {
1717
title: string
1818
file: string
1919
args?: string[]
2020
}) {
2121
test(title, async () => {
22-
const { stdout } = await execa('node', [fixture(file), ...(args || [])])
22+
const { stdout } = await execa('node', [example(file), ...(args || [])])
2323
expect(stdout).toMatchSnapshot(title)
2424
})
2525
}
2626

27+
async function getOutput(file: string, args: string[] = []) {
28+
const { stdout } = await execa('node', [example(file), ...(args || [])])
29+
return stdout
30+
}
31+
2732
snapshotOutput({
2833
title: 'basic-usage',
2934
file: 'basic-usage.js',
30-
args: ['foo', 'bar', '--type', 'ok', 'command']
31-
})
32-
33-
snapshotOutput({
34-
title: 'help',
35-
file: 'help.js',
36-
args: ['--help']
35+
args: ['foo', 'bar', '--type', 'ok', 'command'],
3736
})
3837

3938
snapshotOutput({
4039
title: 'variadic-arguments',
4140
file: 'variadic-arguments.js',
42-
args: ['--foo', 'build', 'a', 'b', 'c', 'd']
41+
args: ['--foo', 'build', 'a', 'b', 'c', 'd'],
4342
})
4443

4544
snapshotOutput({
4645
title: 'ignore-default-value',
4746
file: 'ignore-default-value.js',
48-
args: ['build']
47+
args: ['build'],
4948
})
5049

5150
test('negated option', () => {
@@ -59,7 +58,7 @@ test('negated option', () => {
5958
expect(options).toEqual({
6059
'--': [],
6160
foo: 'foo',
62-
bar: true
61+
bar: true,
6362
})
6463
})
6564

@@ -73,7 +72,7 @@ test('double dashes', () => {
7372
'bar',
7473
'--',
7574
'npm',
76-
'test'
75+
'test',
7776
])
7877

7978
expect(args).toEqual(['foo', 'bar'])
@@ -111,7 +110,7 @@ test('array types without transformFunction', () => {
111110
'--externals <external>',
112111
'Add externals(can be used for multiple times',
113112
{
114-
type: []
113+
type: [],
115114
}
116115
)
117116
.option('--scale [level]', 'Scaling level')
@@ -139,7 +138,7 @@ test('array types with transformFunction', () => {
139138
cli
140139
.command('build [entry]', 'Build your app')
141140
.option('--config <configFlie>', 'Use config file for building', {
142-
type: [String]
141+
type: [String],
143142
})
144143
.option('--scale [level]', 'Scaling level')
145144

@@ -163,3 +162,15 @@ test('throw on unknown options', () => {
163162
cli.parse(`node bin build app.js --fooBar --a-b --xx`.split(' '))
164163
}).toThrowError('Unknown option `--xx`')
165164
})
165+
166+
describe('--version in help message', () => {
167+
test('sub command', async () => {
168+
const output = await getOutput('help.js', ['lint', '--help'])
169+
expect(output).not.toContain(`--version`)
170+
})
171+
172+
test('default command', async () => {
173+
const output = await getOutput('help.js', ['--help'])
174+
expect(output).toContain(`--version`)
175+
})
176+
})

0 commit comments

Comments
 (0)