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

Issue parsing nested commands with electron app #1894

Closed
BeAliAslam opened this issue Jun 18, 2023 · 5 comments
Closed

Issue parsing nested commands with electron app #1894

BeAliAslam opened this issue Jun 18, 2023 · 5 comments

Comments

@BeAliAslam
Copy link

BeAliAslam commented Jun 18, 2023

The nestedcommands.js file in examples seem to work fine if launched directly from node, but fails if called from packaged app like electron (pretty similar to #512)

The issue seems to be that the parsing logic seems to be expecting two entries (node.exe path followed by script file path) in process.argv passed to program.parse before actual command arguments. For node, both entries exist, whereas in electron, there is only one entry i.e. name of executable.

The problem can be reproduced even without electron with steps below

In the nestedcommands.js file linked above in your examples folder, try passing hard coded array to program.parse line (line 42 currently). I added !!! to console.logs in action handlers for clarity.

console.log ("parsing with", ["abc","def", "brew", "coffee"])
program.parse(["abc","def", "brew", "coffee"]);
$ node nestedCommands.js 
parsing with [ 'abc', 'def', 'brew', 'coffee' ]
brew coffee!!!

Now remove any of the first two arguments abc or def

$ node nestedCommands.js 
parsing with [ 'abc', 'brew', 'coffee' ]
error: unknown command 'coffee'

Similar case with process.argv with second entry (file path) removed and only executable (say node.exe) path present in call to program.parse.

@shadowspawn
Copy link
Collaborator

There is code to auto-detect Electron to change the expected argument conventions, or you can explicitly specify the convention when needed. See https://github.com/tj/commander.js#parse-and-parseasync

Can you give some reproduce steps for the problem you are seeing with Electron?

@BeAliAslam
Copy link
Author

BeAliAslam commented Jun 19, 2023

Thanks for the helpful and prompt reply. Calling parse without any arguments solved the issue as implicit electron handling kicked in.

Two follow-up question thoughs (which might be answered but could not find on my own), first the program displays help if there are no commands and i need to avoid that, and other issue being there is still issue if I have to use options meant for the calling exe (electron.exe) e.g

$myprogram.exe --inspect-brk brew coffee

If i do this, it correctly passes inspect-brk to the electron app and i can debug the script from chrome://inspect, however, during parse, i get the error below. Please let me know if you are aware of a way to get around this issue.
error: unknown option '--inspect-brk'

Extra info: For steps, please follow Electron quickstart basically copy pasting code for all files required + adding latest commander js. Then copy paste code from examples/nestedcommands.js file at the start of main/index.js and do the top level require to 'normal include'. That is it!!!

@shadowspawn
Copy link
Collaborator

Node strips the node specific command-line arguments like --inspect-brk from process.argv, but apparently Electron does not. I can think of a couple of ways of hacking around it in your own code.

You could add a dummy argument and strip the earlier arguments before passing the remaining arguments to Commander. e.g.

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

const magicArg = 'START-ARGS';
const index = process.argv.indexOf(magicArg);
if (index > 0) {
  const userArgs = process.argv.slice(index);
  program.parse(userArgs, { from: 'user'});
} else {
  program.parse();
}
$ myprogram.exe --inspect-brk START-ARGS  brew coffee

Another approach would be to add the node options you intent to use as known options, so Commander silently consumes them:

const { Command, Option } = require('commander');
const program = new Command();

program
  .addOption(new Option('--inspect-brk [port]', 'node option to ignore').hideHelp());

@shadowspawn
Copy link
Collaborator

Two follow-up question thoughs (which might be answered but could not find on my own), first the program displays help if there are no commands

One way of handling this is to add a default command. Look for isDefault in the README. A default command has the advantage that you can have custom options which don't affect the other subcommands.

A slightly simpler approach if you aren't using options for the no-subcommand case is just to add an action handler to the program. This gets called if no subcommand is specified.

@BeAliAslam
Copy link
Author

Thanks for quick help and support in resolving the issues. The empty action handler did fix most of the issues. I used the allow UnknownOption as well to get past the inspect-brk problem. Closing the issue now.

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