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

Support Electron process.argv structure, prevent first arg being lost #103

Open
mattdwen opened this issue May 30, 2019 · 2 comments
Open

Comments

@mattdwen
Copy link

mattdwen commented May 30, 2019

I'm running v5.1.1 in an Electron app.

I have the following code

const optionDefinitions = [
  { name: 'debug', type: Boolean },
  { name: 'divafsDisable', type: Boolean },
  { name: 'serve', type: Boolean }
];
const options = commandLineArgs(optionDefinitions);

console.error('options', options);
console.error('argv', process.argv);

I am then executing:

C:\Projects\diva4\dist\client\win-unpacked\DIVAClient.exe --debug --divafsDisable

And I get the following output:

options { divafsDisable: true }
argv [ 'C:\\Projects\\diva4\\dist\\client\\win-unpacked\\DIVAClient.exe',
  '--debug',
  '--divafsDisable' ]

If I pass just a single argument then the options has no properties defined.

I've also tried directly passing in process.argv with the same result.

@mattdwen
Copy link
Author

mattdwen commented May 30, 2019

If I use:

const options = commandLineArgs(optionDefinitions, {
  argv: process.argv.slice(1)
});

then it's working.

I wonder if it's something around https://github.com/75lb/command-line-args/blob/master/lib/argv-tools.mjs#L40 which is causing it with the default argv?
When I check process.execArgv it's empty, which would cause those first two arguments to be spliced out.

@75lb
Copy link
Owner

75lb commented May 31, 2019

yes, it's probably to do with that line.

It's because process.argv almost always contains node and the script name as the first two items, e.g. [ 'node', 'example.js', '--help' ]. I think you have found a case (Electron) where this is not true.

As you've discovered, passing in your own argv array works around this issue and I'll take a look into other options, too. If there is a reliable way of detecting command-line-args is running within an Electron app, and Electron apps only ever need one arg trimming from the front of process.argv and not two then we could put a permanent fix in.

@75lb 75lb added the bug label May 31, 2019
@75lb 75lb changed the title First argument is being skipped Electron app: first argument is being skipped May 31, 2019
@75lb 75lb changed the title Electron app: first argument is being skipped Support Electron process.argv structure, prevent first arg being lost Nov 3, 2019
@75lb 75lb removed the help wanted label Apr 17, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants