Open In App

Command Line Arguments in ElectronJS

Improve
Improve
Like Article
Like
Save
Share
Report

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.

  • Project Structure: 
     

Project Structure

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.

  • Step 1: Follow the Steps given in Drag and Drop Files in ElectronJS to setup the basic Electron Application. Copy the Boilerplate code for the main.js file and the index.html file as provided in the article. Also perform the necessary changes mentioned for the package.json file to launch the Electron Application. We will continue building our application using the same code base. 
    package.json:
{
  "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"
  }
}
  • Create the assets folder according to the project structure. We will use this folder to store logs
    Output: At this point, our basic Electron Application is set up. Upon launching the application, we should see the following result.

  • Step 2: Now we will pass Command-line arguments to the Electron application using any of the two approaches. 
    • Approach 1: Passing Command-line arguments while launching the application. We will be using the –log-net-log=path Command-line switch. This switch enables network logging events to be recorded and writes the generated logs to the path file. We have already made the necessary changes to the package.json file to launch the application using the start script. We will modify this script to pass this Command-line switch.
      package.json: Change the following snippet in that file.
    • Output: Upon launching the application, network logging will be enabled by Chromium and a log.txt file will be automatically generated within the assets folder with initial network log entries.
    • Approach 2: Passing Command-line arguments using the CommandLine property of the app module. 
      main.js Add the following Snippet at the start of the file just after importing the app module and before the ready event is emitted.

javascript




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.

 

  • commandLine.appendSwitch(switch, value) This method appends a Switch with optional value parameter to Chromium’s Command line when invoking the application. This method does not have any return type. It takes in the following parameters. In our code, we have used this Instance method to append the –log-net-log=path Command-line switch.
    • switch: String A Command-line switch without the leading for appending to Chromium’s Command line when invoking the application. This value cannot be empty.
    • value: String (Optional) A String value for the given Switch if it supports any. In case the Switch takes in a Boolean value, we will wrap the Boolean within String for it to be passed via this method. In our code, we have specified the path to the assets folder. By default, it will take the CWD (Current working Directory).
  • commandLine.hasSwitch(switch) This method is used to check whether a Command-line switch is present when invoking the application from outside such as, the start script in the package.json file or has been appended using the commandLine.appendSwitch() method. It returns a Boolean value stating whether the Command-line switch is present. It takes in the following parameters.
    • switch: String A Command-line switch without the leading for checking the Chromium’s Command line. This value cannot be empty.
  • commandLine.getSwitchValue(switch) This method is used to return the Command-line Switch value, if any. It returns a String value. In case the Command-line switch is not present or there is no value, it will return an empty String. It takes in the following parameters.
    • switch: String A Command-line switch without the leading for checking the Chromium’s Command line. This value cannot be empty.
  • commandLine.appendArgument(argument) This method is used to append an Argument to the Chromium’s Command line when invoking the application. The argument will be quoted appropriately by the application. This method does not have any return type. This method is similar to the commandLine.appendSwitch() method but Command-line Switches will always precede the Arguments regardless of the appending order. In case we want to append a Command-line switch as an entire Argument, for example, ‘–log-net-log=assets/log.txt’ then we should not use this method. We should use the commandLine.appendSwitch() method instead. It takes in the following parameters.
    • argument: String An String argument to be appended to Chromium’s Command-line. This value cannot be empty.

 



Last Updated : 09 Nov, 2021
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads