How to pass Command arguments in Electron?


#1

Is it possible to pass CL arguments to an electron?


How to pass command line argument in electron
How to pass command line argument in electron
#2

Are you asking about Atom? Or Electron? Because you mention Electron in the title and category of the post, but Atom in the body.


#3

I’ve changed the category to match the title. Given that atom already handle a bunch of CL arguments that are documented with the classical --help option. But maybe I was too hasty in making the change.


#4

Passing CL arguments to electron


#5

So, now, to answer the question, yes you can of course pass arguments to your app. As with every node process the command line arguments are available in process.argv.

But note that in an electron app, the options have to handled in the main script. You can take inspiration in how Atom handle that:


Running Node.js commands in Electron
#6

yep! worked great! there are many cmd line option parsers (e.g. commander, nomnom) to make this easier too, which many surely are already aware. but if you’re new to hacking in atom apps, don’t hesitate to give one of them a peek!


#7

The way of passing arguments will be same, the only thing you have to take is path of electron. In package.json its written npm start will perform electron main.js. So you will have to execute this command explicitly and pass arguments with “proper path of electron” i.e ./node_modules/.bin/electron. Then the command will be

./node_modules/.bin/electron main.js argv1 argv2

and these arguments you can access by process.argv in main.js

If wish you to access these parameters in your app then there are following things to do :

1.In your main.js define a variable like

     global.sharedObject = {prop1: process.argv}

2.In your app just linclude remote and use this sharedObject

    var remote = require('electron').remote,
      arguments = remote.getGlobal('sharedObject').prop1;

    console.log(arguments);

3.Output will be ["argv1", "argv2"]


#8

If I start my Electron app by electron -r babel-register . arg1 arg2 I get the following for console.dir(process.argv)

[ '{path-to-project}\\node_modules\\electron\\dist\\electron.exe',
  '-r',
  'babel-register',
  '.',
  'arg1',
  'arg2'
   ]

That’s not very useful for command line parsing as one has always take into account Electron options and args!


#9

I am using the ‘electron-winstaller’ node module to create the standalone exe. I am not getting the cmd line parameters to work on the stand alone exe. here is my code

var argv = require(‘minimist’)(process.argv);

cmd:- AutoFileSender.exe 58748009341a7d5840a51414 sambit@bc.com Sambit Swain 59c9d0c64a4ed3599a59df0b_58748009341a7d5840a51414 sender C:\ConnectUs-SFTSend\ C:\ConnectUs-SFTReceive\ rmrqaF

But if i run the same cmd with the basic unpacked exe (in the win32-ia32 folder) it gets the parameters.