CefCommandLine
Header: cef_command_line.h
Category: Core APIs
Overview
Foundational interfaces: process bootstrap, browser lifecycle, frames, DOM, requests, and data objects.
CefCommandLine
Source File: include/cef_command_line.h
Process / Thread Context: Not specified at the class level. The header explicitly notes: "This class can be used before CefInitialize() is called." Static factories CreateCommandLine / GetGlobalCommandLine are callable any time.
Purpose: "Class used to create and/or parse command line arguments." Models Chromium's command-line semantics: switches (prefixed with --, -, or / on Windows) precede non-switch arguments; -- terminates switch parsing; switch names are lowercased ASCII; switch values are UTF-8 with original case. Provides read-only inspection (GetGlobalCommandLine) and writable copies (Copy).
Methods
static CefRefPtr<CefCommandLine> CreateCommandLine()
Parameters:
- none.
Return Value: A new, empty CefCommandLine.
Usage Instruction: Create a writable instance. Initialize it via InitFromArgv / InitFromString.
Threading Constraint: Callable any time, including before CefInitialize.
static CefRefPtr<CefCommandLine> GetGlobalCommandLine()
Parameters:
- none.
Return Value: The singleton global command line. "The returned object will be read-only."
Usage Instruction: Inspect the current process's actual command line. Call IsReadOnly() / Copy() if mutation is needed.
Threading Constraint: Callable any time.
virtual bool IsValid() = 0
Return Value: "Returns true if this object is valid. Do not call any other methods if this function returns false."
Usage Instruction: Defensive check before any other call.
Threading Constraint: Not specified in header.
virtual bool IsReadOnly() = 0
Return Value: "Returns true if the values of this object are read-only. Some APIs may expose read-only objects."
Usage Instruction: Check before attempting to mutate; if read-only, call Copy() first.
Threading Constraint: Not specified in header.
virtual CefRefPtr<CefCommandLine> Copy() = 0
Return Value: "Returns a writable copy of this object."
Usage Instruction: Clone a read-only global object for mutation.
Threading Constraint: Not specified in header.
virtual void InitFromArgv(int argc, const char* const* argv) = 0
Parameters:
argc: Argument count.argv: Argument vector. "The first argument must be the name of the program."
Return Value: void.
Usage Instruction: "This method is only supported on non-Windows platforms."
Threading Constraint: Not specified in header.
virtual void InitFromString(const CefString& command_line) = 0
Parameters:
command_line: "The string returned by calling GetCommandLineW()."
Return Value: void.
Usage Instruction: "This method is only supported on Windows."
Threading Constraint: Not specified in header.
virtual void Reset() = 0
Return Value: void.
Usage Instruction: "Reset the command-line switches and arguments but leave the program component unchanged."
Threading Constraint: Not specified in header.
virtual void GetArgv(std::vector<CefString>& argv) = 0
Parameters:
argv: Out vector populated with the original argv-style sequence:{ program, [(--|-|/)switch[=value]]*, [--], [argument]* }.
Return Value: void.
Usage Instruction: Reconstruct the original token sequence.
Threading Constraint: Not specified in header.
virtual CefString GetCommandLineString() = 0
Return Value: The reconstructed command-line string.
Usage Instruction: "Use this method cautiously because quoting behavior is unclear."
Threading Constraint: Not specified in header.
virtual CefString GetProgram() = 0
Return Value: "Get the program part of the command line string (the first item)."
Threading Constraint: Not specified in header.
virtual void SetProgram(const CefString& program) = 0
Parameters:
program: New program name.
Return Value: void.
Threading Constraint: Not specified in header.
virtual bool HasSwitches() = 0
Return Value: "Returns true if the command line has switches."
Threading Constraint: Not specified in header.
virtual bool HasSwitch(const CefString& name) = 0
Parameters:
name: Switch name to query (case-insensitive; will be lowercased).
Return Value: "Returns true if the command line contains the given switch."
Threading Constraint: Not specified in header.
virtual CefString GetSwitchValue(const CefString& name) = 0
Return Value: "Returns the value associated with the given switch. If the switch has no value or isn't present this method returns the empty string."
Threading Constraint: Not specified in header.
virtual void GetSwitches(SwitchMap& switches) = 0
Parameters:
switches: OutSwitchMappopulated with all switches. "If a switch has no value an empty string is returned."
Return Value: void.
Threading Constraint: Not specified in header.
virtual void AppendSwitch(const CefString& name) = 0
Return Value: void.
Usage Instruction: "Add a switch to the end of the command line."
Threading Constraint: Not specified in header.
virtual void AppendSwitchWithValue(const CefString& name, const CefString& value) = 0
Parameters:
name: Switch name.value: Switch value. "If the switch has no value pass an empty value string."
Return Value: void.
Usage Instruction: "Remove a switch from the command line. If no such switch is present, this has no effect."
Threading Constraint: Not specified in header. #if CEF_API_ADDED(14100).
virtual bool HasArguments() = 0
Return Value: "True if there are remaining command line arguments."
Threading Constraint: Not specified in header.
virtual void GetArguments(ArgumentList& arguments) = 0
Parameters:
arguments: OutArgumentListpopulated with non-switch arguments.
Return Value: void.
Threading Constraint: Not specified in header.
virtual void AppendArgument(const CefString& argument) = 0
Return Value: void.
Usage Instruction: "Add an argument to the end of the command line."
Threading Constraint: Not specified in header.
virtual void PrependWrapper(const CefString& wrapper) = 0
Return Value: void.
Usage Instruction: "Insert a command before the current command. Common for debuggers, like 'valgrind' or 'gdb --args'."
Threading Constraint: Not specified in header.
Usage Example
// Inside CefApp::OnBeforeCommandLineProcessing:
void MyApp::OnBeforeCommandLineProcessing(
const CefString& process_type,
CefRefPtr<CefCommandLine> command_line) {
if (process_type.empty()) { // browser process
command_line->AppendSwitch("disable-extensions");
command_line->AppendSwitchWithValue("remote-debugging-port", "9222");
} else if (process_type == "renderer") {
command_line->AppendSwitch("enable-experimental-web-platform-features");
}
}
// Reading the global command line from anywhere:
CefRefPtr<CefCommandLine> cl = CefCommandLine::GetGlobalCommandLine();
if (cl->HasSwitch("my-flag")) {
CefString val = cl->GetSwitchValue("my-flag");
// ...
}