跳转到主要内容

类:CommandLine

类:CommandLine

操作你的应用程序的命令行参数,Chromium 会读取这些参数

进程:主进程
此类未从 'electron' 模块导出。它仅作为 Electron API 中其他方法的返回值可用。

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

const { app } = require('electron')
app.commandLine.hasSwitch('disable-gpu')

有关你可以使用的标志和开关类型的更多信息,请查看 命令行开关 文档。

实例方法

commandLine.appendSwitch(switch[, value])

  • switch 字符串 - 一个命令行开关,不带开头的 --
  • value 字符串(可选)- 指定开关的值。

向 Chromium 的命令行追加一个开关(可选带 value)。

注意

这不会影响 process.argv。此函数 intended usage 是控制 Chromium 的行为。

const { app } = require('electron')

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

commandLine.appendArgument(value)

  • value 字符串 - 要追加到命令行的参数。

向 Chromium 的命令行追加参数。该参数将被正确引用。开关将优先于参数,无论追加顺序如何。

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

const { app } = require('electron')

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

这不会影响 process.argv。此函数 intended usage 是控制 Chromium 的行为。

commandLine.hasSwitch(switch)

  • switch 字符串 - 一个命令行开关。

返回 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 - 命令行开关的值。

此函数用于获取 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'
注意

当开关不存在或没有值时,它将返回空字符串。

commandLine.removeSwitch(switch)

  • switch 字符串 - 一个命令行开关。

从 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
注意

这不会影响 process.argv。此函数 intended usage 是控制 Chromium 的行为。