类:CommandLine
类:CommandLine
¥Class: CommandLine
操作 Chromium 读取的应用的命令行参数
¥Manipulate the command line arguments for your app that Chromium reads
进程:主进程
该类不是从 'electron' 模块导出的。它仅可用作 Electron API 中其他方法的返回值。
¥Process: Main
This class is not exported from the 'electron' module. It is only available as a return value of other methods in the 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字符串 - 命令行开关,不带前导--。¥
switchstring - A command-line switch, without the leading--. -
value字符串(可选) - 给定开关的值。¥
valuestring (optional) - A value for the given switch.
将一个开关(带有可选的 value)附加到 Chromium 的命令行。
¥Append a switch (with optional value) to Chromium's command line.
[!NOTE] 这不会影响
process.argv。该函数的预期用途是控制 Chromium 的行为。¥[!NOTE] This will not affect
process.argv. The intended usage of this function is to control Chromium's behavior.
const { app } = require('electron')
app.commandLine.appendSwitch('remote-debugging-port', '8315')
commandLine.appendArgument(value)
-
value字符串 - 要附加到命令行的参数。¥
valuestring - The argument to append to the command line.
将参数附加到 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 的行为。¥[!NOTE] This will not affect
process.argv. The intended usage of this function is to control Chromium's behavior.
commandLine.hasSwitch(switch)
-
switch字符串 - 命令行开关。¥
switchstring - A command-line 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字符串 - 命令行开关。¥
switchstring - A command-line 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] 当开关不存在或没有值时,它将返回空字符串。
¥[!NOTE] When the switch is not present or has no value, it returns empty string.
commandLine.removeSwitch(switch)
-
switch字符串 - 命令行开关。¥
switchstring - A command-line 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 的行为。¥[!NOTE] This will not affect
process.argv. The intended usage of this function is to control Chromium's behavior.