Skip to main content

类:CommandLine

类:CommandLine

🌐 Class: CommandLine

操控 Chromium 读取的应用命令行参数

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

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

🌐 The following example shows how to check if the --disable-gpu flag is set.

const { app } = require('electron')

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

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

🌐 For more information on what kinds of flags and switches you can use, check out the Command Line Switches document.

实例方法

🌐 Instance Methods

commandLine.appendSwitch(switch[, value])

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

在 Chromium 的命令行中添加一个开关(可选 value)。

🌐 Append a switch (with optional value) to Chromium's command line.

note

这不会影响 process.argv。该函数的预期用途是用于控制 Chromium 的行为。

const { app } = require('electron')

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

commandLine.appendArgument(value)

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

向 Chromium 的命令行追加一个参数。参数将被正确引用。开关将位于参数之前,无论追加顺序如何。

🌐 Append an argument to Chromium's command line. The argument will be quoted correctly. Switches will precede arguments regardless of appending order.

如果你正在附加像 --switch=value 这样的参数,考虑改用 appendSwitch('switch', 'value')

🌐 If you're appending an argument like --switch=value, consider using appendSwitch('switch', 'value') instead.

const { app } = require('electron')

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

这不会影响 process.argv。该函数的预期用途是用于控制 Chromium 的行为。

commandLine.hasSwitch(switch)

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

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

🌐 Returns boolean - Whether the command-line switch is present.

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

🌐 Returns string - The command-line switch value.

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

🌐 This function is meant to obtain Chromium command line switches. It is not meant to be used for application-specific command line arguments. For the latter, please use 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 字符串 - 一个命令行开关。

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

🌐 Removes the specified switch from Chromium's command line.

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 的行为。