Open In App

Command Line Arguments in ElectronJS

ElectronJS is an Open Source Framework used for building Cross-Platform native desktop applications using web technologies such as HTML, CSS, and JavaScript which are capable of running on Windows, macOS, and Linux operating systems. It combines the Chromium engine and NodeJS into a Single Runtime.

The Command-line arguments are parameters that are passed to the program when it is invoked. Different programming languages and frameworks use different methods to parse these arguments. Command-line arguments are important because they can be used to control the behavior of the application. These arguments are passed to the main function of any application. Electron also supports Command-line arguments. We can pass Command-line arguments to Electron from outside the application on invoking it or we can simply hard code these values using the Instance methods of the built-in CommandLine Property of the app Module of Electron. This tutorial will demonstrate how to add Command-line arguments in Electron.



We assume that you are familiar with the prerequisites as covered in the above-mentioned link. For Electron to work, node and npm need to be pre-installed in the system.



Command-line arguments in Electron The app Module and the CommandLine Property are part of the Main Process. We need to append the Command-Line arguments to the applications Main Process before the ready Event of the app Module is emitted. In Electron, the CommandLine property is used to read and manipulate the command-line arguments that Chromium reads and uses. They can be used to control and override the default behaviour of Chromium. For a detailed list of supported Command-Line flags and switches that can be used in Electron. We will also be using some of these flags and switches for demonstration. The CommandLine property only supports Instance methods. It does not have any Instance events and properties associated with it.

Example: Follow the give Steps to pass Command-line arguments in Electron.

{
  "name": "electron-command",
  "version": "1.0.0",
  "description": "Command Line Switches in Electron",
  "main": "main.js",
  "scripts": {
    "start": "electron ."
  },
  "keywords": [
    "electron"
  ],
  "author": "Radhesh Khanna",
  "license": "ISC",
  "dependencies": {
    "electron": "^8.3.0"
  }
}




const { app, BrowserWindow } = require("electron");
app.commandLine.appendSwitch("log-net-log", "assets/log.txt");
console.log(app.commandLine.hasSwitch("log-net-log"));
console.log(app.commandLine.getSwitchValue("log-net-log"));

 
 

A detailed Explanation of all the Instance methods of the CommandLine property used in the code are explained below. For more detailed Information on the CommandLine property.

 

 


Article Tags :