跳转至

命令行

类:CommandLine

操作 Chromium 为你的应用读取的命令行参数。

进程:主进程
此类不会从 'electron' 模块导出。它只能作为 Electron API 中其他方法的返回值使用。

以下示例演示如何检查是否设置了 --disable-gpu 标志。

const { app } = require('electron')

app.commandLine.hasSwitch('disable-gpu')

有关你可以使用的标志(flags)和开关(switches)类型的更多信息,请参阅 Command Line Switches 文档。

实例方法

commandLine.appendSwitch(switch[, value])

  • switch string - 一个命令行开关,不带前导 --。
  • value string (optional) - 给定开关的值。

将一个开关(value 可选)追加到 Chromium 的命令行中。

[!NOTE] 这不会影响 process.argv。此函数的预期用途是控制 Chromium 的行为。

const { app } = require('electron')

app.commandLine.appendSwitch('remote-debugging-port', '8315')

commandLine.appendArgument(value)

  • value string - 要追加到命令行中的参数。

将一个参数追加到 Chromium 的命令行中。该参数会被正确添加引号。无论追加顺序如何,开关(switches)都会在参数之前。

如果你要追加像 --switch=value 这样的参数,请考虑改用 appendSwitch('switch', 'value')。

const { app } = require('electron')

app.commandLine.appendArgument('--enable-experimental-web-platform-features')

[!NOTE] 这不会影响 process.argv。此函数的预期用途是控制 Chromium 的行为。

commandLine.hasSwitch(switch)

  • switch string - 一个命令行开关。

返回 boolean - 该命令行开关是否存在。

const { app } = require('electron')

app.commandLine.appendSwitch('remote-debugging-port', '8315')
const hasPort = app.commandLine.hasSwitch('remote-debugging-port')
console.log(hasPort) // true

commandLine.getSwitchValue(switch)

  • switch string - 一个命令行开关。

返回 string - 命令行开关的值。

此函数用于获取 Chromium 命令行开关。它并非用于获取应用特定的命令行参数。对于后者,请使用 process.argv。

const { app } = require('electron')

app.commandLine.appendSwitch('remote-debugging-port', '8315')
const portValue = app.commandLine.getSwitchValue('remote-debugging-port')
console.log(portValue) // '8315'

[!NOTE] 当开关不存在或没有值时,它会返回空字符串。

commandLine.removeSwitch(switch)

  • switch string - 一个命令行开关。

从 Chromium 的命令行中移除指定的开关。

const { app } = require('electron')

app.commandLine.appendSwitch('remote-debugging-port', '8315')
console.log(app.commandLine.hasSwitch('remote-debugging-port')) // true

app.commandLine.removeSwitch('remote-debugging-port')
console.log(app.commandLine.hasSwitch('remote-debugging-port')) // false

[!NOTE] 这不会影响 process.argv。此函数的预期用途是控制 Chromium 的行为。

本页原文 Markdown:在 AtomGit 查看·内容源自开源项目 el/electron