Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass command line argument in electron

I just started using electron. I have a doubt about how to pass command line arguments in electron when I'm using npm start to run electron.

In Node.js I am using: node server.js one two=three four command prompt for :

var arguments = process.argv.slice(2);
arguments.forEach(function(val,index, array) {
  console.log(index + ': ' + val);
}); 

In Node.js is working. I need to know how can I make this work in electron.

Can someone please give a solution for this?

like image 299
Hussian Shaik Avatar asked Jun 13 '15 05:06

Hussian Shaik


People also ask

Can you pass command line arguments?

It is possible to pass some values from the command line to your C programs when they are executed. These values are called command line arguments and many times they are important for your program especially when you want to control your program from outside instead of hard coding those values inside the code.

How do I run an electron from the command line?

Locate Command Prompt by entering cmd into the search bar. Click cmd in the search results to open the Command Prompt. Enter the following command, then press Enter to create a file named test-node. Type node followed by the name of the application, which is test-node.


1 Answers

The way of passing arguments will be same, the only thing you have to take care 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

and 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 include remote and use this sharedObject

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

console.log(arguments);

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

like image 63
Abhishek Tripathi Avatar answered Sep 30 '22 10:09

Abhishek Tripathi