command-line-args
command-line-args copied to clipboard
Support Electron process.argv structure, prevent first arg being lost
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.
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.
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.