Skip to content

Commit

Permalink
fix: make sure --version appears in the help message of the default c…
Browse files Browse the repository at this point in the history
…ommand
  • Loading branch information
egoist committed Oct 12, 2021
1 parent 702d41a commit 0815980
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 37 deletions.
6 changes: 5 additions & 1 deletion examples/help.js
Expand Up @@ -2,14 +2,18 @@ require('ts-node/register')
const cli = require('../src/index')()

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

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

cli.command('[...files]', 'Run files').action((files, options) => {
console.log('run', files, options)
})

// Display help message when `-h` or `--help` appears
cli.help()
// Display version number when `-v` or `--version` appears
Expand Down
2 changes: 1 addition & 1 deletion src/Command.ts
Expand Up @@ -183,7 +183,7 @@ class Command {
let options = this.isGlobalCommand
? globalOptions
: [...this.options, ...(globalOptions || [])]
if (!this.isGlobalCommand) {
if (!this.isGlobalCommand && !this.isDefaultCommand) {
options = options.filter((option) => option.name !== 'version')
}
if (options.length > 0) {
Expand Down
19 changes: 0 additions & 19 deletions src/__test__/__snapshots__/index.test.ts.snap
Expand Up @@ -14,25 +14,6 @@ exports[`basic-usage: basic-usage 1`] = `
}"
`;

exports[`help: help 1`] = `
"help.js/0.0.0
Usage:
$ help.js <command> [options]
Commands:
lint [...files] Lint files
For more info, run any command with the \`--help\` flag:
$ help.js lint --help
Options:
--type [type] Choose a project type (default: node)
--name <name> Provide your name
-h, --help Display this message
-v, --version Display version number "
`;
exports[`ignore-default-value: ignore-default-value 1`] = `
"{
\\"args\\": [],
Expand Down
43 changes: 27 additions & 16 deletions src/__test__/index.test.ts
Expand Up @@ -2,7 +2,7 @@ import path from 'path'
import execa from 'execa'
import cac from '..'

function fixture(file: string) {
function example(file: string) {
return path.relative(
process.cwd(),
path.join(__dirname, '../../examples', file)
Expand All @@ -12,40 +12,39 @@ function fixture(file: string) {
function snapshotOutput({
title,
file,
args
args,
}: {
title: string
file: string
args?: string[]
}) {
test(title, async () => {
const { stdout } = await execa('node', [fixture(file), ...(args || [])])
const { stdout } = await execa('node', [example(file), ...(args || [])])
expect(stdout).toMatchSnapshot(title)
})
}

async function getOutput(file: string, args: string[] = []) {
const { stdout } = await execa('node', [example(file), ...(args || [])])
return stdout
}

snapshotOutput({
title: 'basic-usage',
file: 'basic-usage.js',
args: ['foo', 'bar', '--type', 'ok', 'command']
})

snapshotOutput({
title: 'help',
file: 'help.js',
args: ['--help']
args: ['foo', 'bar', '--type', 'ok', 'command'],
})

snapshotOutput({
title: 'variadic-arguments',
file: 'variadic-arguments.js',
args: ['--foo', 'build', 'a', 'b', 'c', 'd']
args: ['--foo', 'build', 'a', 'b', 'c', 'd'],
})

snapshotOutput({
title: 'ignore-default-value',
file: 'ignore-default-value.js',
args: ['build']
args: ['build'],
})

test('negated option', () => {
Expand All @@ -59,7 +58,7 @@ test('negated option', () => {
expect(options).toEqual({
'--': [],
foo: 'foo',
bar: true
bar: true,
})
})

Expand All @@ -73,7 +72,7 @@ test('double dashes', () => {
'bar',
'--',
'npm',
'test'
'test',
])

expect(args).toEqual(['foo', 'bar'])
Expand Down Expand Up @@ -111,7 +110,7 @@ test('array types without transformFunction', () => {
'--externals <external>',
'Add externals(can be used for multiple times',
{
type: []
type: [],
}
)
.option('--scale [level]', 'Scaling level')
Expand Down Expand Up @@ -139,7 +138,7 @@ test('array types with transformFunction', () => {
cli
.command('build [entry]', 'Build your app')
.option('--config <configFlie>', 'Use config file for building', {
type: [String]
type: [String],
})
.option('--scale [level]', 'Scaling level')

Expand All @@ -163,3 +162,15 @@ test('throw on unknown options', () => {
cli.parse(`node bin build app.js --fooBar --a-b --xx`.split(' '))
}).toThrowError('Unknown option `--xx`')
})

describe('--version in help message', () => {
test('sub command', async () => {
const output = await getOutput('help.js', ['lint', '--help'])
expect(output).not.toContain(`--version`)
})

test('default command', async () => {
const output = await getOutput('help.js', ['--help'])
expect(output).toContain(`--version`)
})
})

0 comments on commit 0815980

Please sign in to comment.