6

I'm trying to pass command line arguments to my already packaged app on execution. (Already packaged with electron-builder and installed the .dmg on my mac)

I navigated into the /Applications/myApp.app/Contents/MacOS folder where the application executable is located. Then i run. exec myApp --myNewArgument theFancyValue For some reason "myNewArgument" does not appear in my process.argv array.

Am i missing anything? I thought the arguements will automaticly be passed to my electron main process.

I am thankfull for any help.

3 Answers 3

6

Yes, the passed command line arguments appear in the process.argv array, but only from the main process.

From a renderer process, you'll need to access the main process arguments using remote.process:

require('electron').remote.process.argv
3
  • Thanks a lot :) process.argv actually returns some predefined parameters, thats why i thought it works as expected. remote.process.argvreturns the correct parameters, previously passed to the executable.
    – Lucca
    Mar 29, 2018 at 12:06
  • Well, process is sometimes difficult to understand, since it is a combination of the original Node.js process object (one for the main process, and one for each renderer process), with extensions from Electron...
    – user8022331
    Mar 29, 2018 at 15:11
  • BTW, I wrote the Vade Mecum Shelf Electron application which contains a System Information utility: it displays in a tooltip the code used to get each piece of information while hovering over each result, including from the various Process categories. Hopefully, it may be helpful to other developers too...
    – user8022331
    Mar 29, 2018 at 15:37
0

https://www.electronjs.org/docs/api/command-line states that

(commandLine.appendSwitch(...)) This will not affect process.argv. The intended usage of this function is to control Chromium's behavior.

As such, only the appp.commandLine.getSwitchValue as mentioned in other comments allows you to get these command line switches

In addition to the use of switches as command line parameters, the use of arguments is also supported. Arguments take the form of "someData" in the final command line, where switches take the form "--namedParameter=someData".

In code, commandLine.appendArgument(value) is used to add the argument. These arguments are given before all switches in the generated command line.

They are also intended to affect Chromium's behavior. Again:

(commandLine.appendArgument(value)) Note: This will not affect process.argv. The intended usage of this function is to control Chromium's behavior.

Since there is no key for the parameter, there does not appear to be a way to directly read it from code as there is with a switch.

https://www.electronjs.org/docs/api/command-line-switches gives a list of supported switches.

-1

In case of packaged source of electron app, command line arguments can be accessed using following function. Let's say if we have passed the command line argument as --myNewArgument=theFancyValue. It can be retrieved like this in the main.js:

import { app } from "electron";
app.commandLine.getSwitchValue("myNewArgument");

This works for development mode as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.