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

Stand-alone executable with nested commands not getting triggered #2317

Open
andygrunwald opened this issue Jan 23, 2025 · 2 comments
Open

Comments

@andygrunwald
Copy link

The programm

import { Command } from 'commander';
const program = new Command();

program.name('pm')
const brew = program.command('brew').description('Brew your drinks');;
brew.command('tea', 'Brew your tea');
brew.command('coffee', 'Brew your coffee');

program.parse(process.argv);

The file structure:

ls -l src

- index.ts
- pm-brew-coffee.ts
- pm-brew-tea.ts
- pm-brew.ts

pm-brew-coffee.ts looks like

console.log("pm-brew-coffee");

The call node -r ts-node/register src/index.ts brew coffeeends up with

/my/app/path/node_modules/commander/lib/command.js:1175
    throw new Error(executableMissing);
          ^
Error: 'brew-coffee' does not exist
 - if 'coffee' is not meant to be an executable command, remove description parameter from '.command()' and use '.description()' instead
 - if the default executable name is not suitable, use the executableFile option to supply a custom name or path
 - no directory for search for local subcommand, use .executableDir() to supply a custom directory
    at Command._checkForMissingExecutable (/my/app/path/node_modules/commander/lib/command.js:1175:11)
    at ChildProcess.<anonymous> (/my/app/path/node_modules/commander/lib/command.js:1306:14)
    at ChildProcess.emit (node:events:507:28)
    at ChildProcess.emit (node:domain:489:12)
    at Process.ChildProcess._handle.onexit (node:internal/child_process:292:12)
    at onErrorNT (node:internal/child_process:484:16)
    at processTicksAndRejections (node:internal/process/task_queues:90:21)

Even if I rename the file pm-brew-coffee.ts to brew-coffee.ts, it throws the same error.

I would have expected that the file pm-brew-coffee.ts is getting executed and pm-brew-coffee is shown.

Am I am missing something?

@shadowspawn
Copy link
Collaborator

shadowspawn commented Jan 24, 2025

This declares a built-in command named brew:

const brew = program.command('brew').description('Brew your drinks');

For a stand-alone command implemented in pm-brew.ts you want:

const brew = program.command('brew', 'Brew your drinks');

and you would add the tea and coffee subcommands in pm-brew.ts not in index.ts.

@shadowspawn
Copy link
Collaborator

The confusion between commands with an action handler and ones implemented as stand-alone executable files was more common before the README got updated, but can still cause confusion: #938

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants