diff --git a/examples/default-command-inverted.ts b/examples/default-command-inverted.ts new file mode 100644 index 00000000..7e5ca702 --- /dev/null +++ b/examples/default-command-inverted.ts @@ -0,0 +1,11 @@ +import { cac } from '../src/index.ts' +const cli = cac() + +cli + .command('something', 'Do something') + .alias('!') + .action(() => { + console.info('Did something!') + }) + +cli.parse() diff --git a/examples/default-command.ts b/examples/default-command.ts new file mode 100644 index 00000000..f160f898 --- /dev/null +++ b/examples/default-command.ts @@ -0,0 +1,11 @@ +import { cac } from '../src/index.ts' +const cli = cac() + +cli + .command('', 'Do something') + .alias('something') + .action(() => { + console.info('Did something!') + }) + +cli.parse() diff --git a/src/cac.ts b/src/cac.ts index 673afab1..8528289a 100644 --- a/src/cac.ts +++ b/src/cac.ts @@ -217,7 +217,7 @@ export class CAC extends EventTarget { if (shouldParse) { // Search the default command for (const command of this.commands) { - if (command.name === '') { + if (command.isDefaultCommand) { shouldParse = false const parsed = this.mri(argv.slice(2), command) this.setParsedInfo(parsed, command) diff --git a/tests/index.test.ts b/tests/index.test.ts index a2bc4e71..bbb176b4 100644 --- a/tests/index.test.ts +++ b/tests/index.test.ts @@ -186,3 +186,26 @@ describe('--version in help message', () => { expect(output).toContain(`--version`) }) }) + +describe('default commands', () => { + test('simple: empty call', async () => { + const output = await getOutput('default-command.ts', []) + expect(output).toContain('Did something!') + }) + + test('simple: name alias call', async () => { + const output = await getOutput('default-command.ts', ['something']) + expect(output).toContain('Did something!') + }) + + // See https://github.com/cacjs/cac/issues/151 + test('inverted: empty call', async () => { + const output = await getOutput('default-command-inverted.ts', []) + expect(output).toContain('Did something!') + }) + + test('inverted: name alias call', async () => { + const output = await getOutput('default-command-inverted.ts', ['something']) + expect(output).toContain('Did something!') + }) +})