Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • NodeJS Tutorial
  • NodeJS Exercises
  • NodeJS Assert
  • NodeJS Buffer
  • NodeJS Console
  • NodeJS Crypto
  • NodeJS DNS
  • NodeJS File System
  • NodeJS Globals
  • NodeJS HTTP
  • NodeJS HTTP2
  • NodeJS OS
  • NodeJS Path
  • NodeJS Process
  • NodeJS Query String
  • NodeJS Stream
  • NodeJS String Decoder
  • NodeJS Timers
  • NodeJS URL
  • NodeJS Interview Questions
  • NodeJS Questions
  • Web Technology
Open In App
Next Article:
Keyboard Shortcuts in ElectronJS
Next article icon

Command Line Arguments in ElectronJS

Last Updated : 09 Nov, 2021
Comments
Improve
Suggest changes
Like Article
Like
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.


 



Next Article
Keyboard Shortcuts in ElectronJS
author
radheshkhanna
Improve
Article Tags :
  • CSS
  • HTML
  • JavaScript
  • Node.js
  • Web Technologies
  • ElectronJS

Similar Reads

  • Introduction to ElectronJS
    Creating cross-platform applications often requires learning multiple programming languages and frameworks. However, Electron.js eliminates this complexity by allowing developers to build desktop applications using familiar web technologies like JavaScript, HTML, and CSS. In this article, we will le
    5 min read
  • How to Create a Desktop App Using JavaScript?
    Building a JavaScript desktop application is possible using various frameworks and technologies. One popular approach is to use Electron, which is an open-source framework developed by GitHub. Electron allows you to build cross-platform desktop applications using web technologies such as HTML, CSS,
    2 min read
  • Environment Variables 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.
    7 min read
  • Manage Staging Environments 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.
    6 min read
  • Integrate Angular 7 with 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.
    8 min read
  • 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.
    6 min read
  • Keyboard Shortcuts 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.U
    6 min read
  • Windows Taskbar Operations 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.
    6 min read
  • File Upload 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.
    8 min read
  • Managing Themes 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.
    9 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences