Command-Line Interface (CLI)
Relevant source files
The following files were used as context for generating this wiki page:The Visual Studio Code Command-Line Interface (CLI) provides a way for users to interact with the editor from the terminal. It is used for a variety of tasks, including opening files and folders, installing extensions, managing remote tunnels, and outputting diagnostic information. The CLI has a hybrid architecture, combining a native Rust executable for initial command handling and a more extensive TypeScript-based interface within the main Electron application for richer interactions.
This architecture allows for fast execution of simple commands (like version or tunnel) directly from the native client, while delegating more complex, UI-dependent operations to the full VS Code application. The entry point for users on Linux and macOS is typically the code.sh shell script, which sets up the necessary environment before invoking the native executable.
Architecture Overview
The CLI's execution flow involves multiple stages, starting from a shell script wrapper, moving to a native Rust application, and finally to the main TypeScript-based application logic.
This diagram illustrates the high-level flow of a CLI command.
Sources: scripts/code.sh, cli/src/commands.rs, src/cli.ts
Entry Points
Shell Script (code.sh)
On Linux and macOS, the code command is a shell script that acts as the primary entry point. Its main responsibilities are:
- Determining the application path (
VSCODE_PATH). - Setting the
ELECTRON_RUN_AS_NODEenvironment variable if necessary for certain commands. - Executing the native CLI application with all forwarded arguments.
This script ensures that the application is launched with the correct context and environment variables.
Sources: scripts/code.sh:35-58
Native CLI (Rust)
The native CLI, written in Rust, is the first executable to process user commands. It is responsible for handling a specific subset of commands that can be executed without launching the full Electron application. This improves performance for common, lightweight tasks.
Key responsibilities include:
- Parsing initial arguments to identify the command.
- Handling commands such as
tunnel,serve-web,status, andversion. - Launching the main VS Code application for all other commands or when files/folders are passed as arguments.
The following diagram shows the decision-making process within the native CLI.
Sources: cli/src/commands.rs:100-125
The native CLI defines its commands in an enum called Commands.
| Command | Description |
|---|---|
tunnel | Manages tunnels for remote connections. |
serve-web | Starts a local server for the web UI. |
status | Shows status of running VS Code processes. |
version | Prints version information. |
install-extension | Installs an extension. |
uninstall-extension | Uninstalls an extension. |
list-extensions | Lists installed extensions. |
locate-extension | Finds the location of an extension. |
telemetry | Manages telemetry settings. |
login | Authenticates to a service. |
Sources: cli/src/commands.rs:25-54
Main Application CLI (TypeScript)
When the native CLI determines that a command requires the full application, it launches the Electron app. The src/cli.ts file then takes over to handle the command-line arguments. This is the entry point for most traditional CLI flags like opening files, showing differences, or navigating to specific lines.
A separate entry point, src/vs/code/node/cli.ts, handles CLI commands that can run in a Node.js context without a full UI, such as printing help text or managing extensions.
Sources: src/cli.ts, src/vs/code/node/cli.ts
Command and Argument Parsing
Argument parsing is split between the native CLI and the main application. The main application's parsing logic is defined in src/vs/platform/environment/node/argv.ts.
Common Application Arguments
The following table lists some of the key arguments handled by the main application.
| Argument | Alias | Description |
|---|---|---|
--diff | -d | Compare two files with each other. |
--goto | -g | Open a file at the specified line and character position. |
--install-extension | Installs or updates an extension. | |
--uninstall-extension | Uninstalls an extension. | |
--list-extensions | List the installed extensions. | |
--enable-proposed-api | Enables proposed API features for an extension. | |
--version | -v | Print version. |
--help | -h | Print usage. |
--wait | -w | Wait for the files to be closed before returning. |
--new-window | -n | Force to open a new window. |
--reuse-window | -r | Force to open a file or folder in an already opened window. |
--user-data-dir | Specifies the directory that user data is kept in. | |
--extensions-dir | Set the root path for extensions. | |
--verbose | Print verbose output (implies --wait). | |
--status | -s | Print process usage and diagnostics information. |
--prof-startup | Run CPU profiler during startup. | |
--log | Log level to use. |
Sources: src/vs/platform/environment/node/argv.ts:25-180
Parsing Sequence
The following sequence diagram shows how the code --goto command is processed.
Key Operations
Extension Management
The CLI provides several flags for managing extensions directly from the command line. These operations are handled by both the native CLI and the main application, depending on the context.
--install-extension <ext>: Installs an extension by its ID.--uninstall-extension <ext>: Uninstalls an extension.--list-extensions: Shows a list of all installed extensions.
These commands are defined in the Rust CLI and also handled within the main application's logic.
Sources: cli/src/commands.rs:37-42, src/vs/code/node/cli.ts:111-131
Diagnostics and Information
The CLI is a crucial tool for diagnostics and retrieving information about the editor's state.
--status: Provides a detailed report of running processes, including CPU and memory usage. This is useful for troubleshooting performance issues.--version: Prints the version of VS Code, the commit hash, and details about the underlying Electron and Node.js versions.--verbose: Enables detailed logging, which is helpful for debugging the editor's behavior.
Sources: cli/src/commands.rs:33, src/vs/platform/environment/node/argv.ts:74-76