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

Program stops if no command is passed #2296

Open
matthme opened this issue Dec 14, 2024 · 2 comments
Open

Program stops if no command is passed #2296

matthme opened this issue Dec 14, 2024 · 2 comments

Comments

@matthme
Copy link

matthme commented Dec 14, 2024

In the following example the program never logs the last line if it is ran without commands but instead the help output is shown. This is unexpected behavior for me and since my actual CLI is an electron app I need it to run without commands too.

import { program, Command } from "commander";

const someCommand = new Command();

someCommand.name('name').argument("<string>").action((name) => console.log(`Hello ${name}`));

program.name("test").option("--irrelevant").addCommand(someCommand);

program.parse();

console.log(
  "I would like to be logged if no command is passed to the CLI but unfortunately I'm not'."
);
@shadowspawn
Copy link
Collaborator

The default Commander behaviour when there are no arguments and you have subcommands and there is not a default command and there is not an action handler, is to show the help.

The simplest and most direct approach might be to test for this case yourself. In Electron, you can check defaultApp to work out what arguments were passed:

const args = process.defaultApp ? process.argv.slice(2) : process.argv.slice(1);
if (args.length > 0) program.parse(args);

@matthme
Copy link
Author

matthme commented Dec 14, 2024

Thanks for the quick response! In my case I do need to parse the program because I have options (although no arguments). But adding an empty action handler to the top level command did the trick :).

I'm still not sure though whether this default behavior is what I would expect...

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