Path: blob/main/src/vs/platform/environment/node/argvHelper.ts
3296 views
/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import assert from 'assert';6import { IProcessEnvironment } from '../../../base/common/platform.js';7import { localize } from '../../../nls.js';8import { NativeParsedArgs } from '../common/argv.js';9import { ErrorReporter, NATIVE_CLI_COMMANDS, OPTIONS, parseArgs } from './argv.js';1011function parseAndValidate(cmdLineArgs: string[], reportWarnings: boolean): NativeParsedArgs {12const onMultipleValues = (id: string, val: string) => {13console.warn(localize('multipleValues', "Option '{0}' is defined more than once. Using value '{1}'.", id, val));14};15const onEmptyValue = (id: string) => {16console.warn(localize('emptyValue', "Option '{0}' requires a non empty value. Ignoring the option.", id));17};18const onDeprecatedOption = (deprecatedOption: string, message: string) => {19console.warn(localize('deprecatedArgument', "Option '{0}' is deprecated: {1}", deprecatedOption, message));20};21const getSubcommandReporter = (command: string) => ({22onUnknownOption: (id: string) => {23if (!(NATIVE_CLI_COMMANDS as readonly string[]).includes(command)) {24console.warn(localize('unknownSubCommandOption', "Warning: '{0}' is not in the list of known options for subcommand '{1}'", id, command));25}26},27onMultipleValues,28onEmptyValue,29onDeprecatedOption,30getSubcommandReporter: (NATIVE_CLI_COMMANDS as readonly string[]).includes(command) ? getSubcommandReporter : undefined31});32const errorReporter: ErrorReporter = {33onUnknownOption: (id) => {34console.warn(localize('unknownOption', "Warning: '{0}' is not in the list of known options, but still passed to Electron/Chromium.", id));35},36onMultipleValues,37onEmptyValue,38onDeprecatedOption,39getSubcommandReporter40};4142const args = parseArgs(cmdLineArgs, OPTIONS, reportWarnings ? errorReporter : undefined);43if (args.goto) {44args._.forEach(arg => assert(/^(\w:)?[^:]+(:\d*){0,2}:?$/.test(arg), localize('gotoValidation', "Arguments in `--goto` mode should be in the format of `FILE(:LINE(:CHARACTER))`.")));45}4647return args;48}4950function stripAppPath(argv: string[]): string[] | undefined {51const index = argv.findIndex(a => !/^-/.test(a));5253if (index > -1) {54return [...argv.slice(0, index), ...argv.slice(index + 1)];55}56return undefined;57}5859/**60* Use this to parse raw code process.argv such as: `Electron . --verbose --wait`61*/62export function parseMainProcessArgv(processArgv: string[]): NativeParsedArgs {63let [, ...args] = processArgv;6465// If dev, remove the first non-option argument: it's the app location66if (process.env['VSCODE_DEV']) {67args = stripAppPath(args) || [];68}6970// If called from CLI, don't report warnings as they are already reported.71const reportWarnings = !isLaunchedFromCli(process.env);72return parseAndValidate(args, reportWarnings);73}7475/**76* Use this to parse raw code CLI process.argv such as: `Electron cli.js . --verbose --wait`77*/78export function parseCLIProcessArgv(processArgv: string[]): NativeParsedArgs {79let [, , ...args] = processArgv; // remove the first non-option argument: it's always the app location8081// If dev, remove the first non-option argument: it's the app location82if (process.env['VSCODE_DEV']) {83args = stripAppPath(args) || [];84}8586return parseAndValidate(args, true);87}8889export function addArg(argv: string[], ...args: string[]): string[] {90const endOfArgsMarkerIndex = argv.indexOf('--');91if (endOfArgsMarkerIndex === -1) {92argv.push(...args);93} else {94// if the we have an argument "--" (end of argument marker)95// we cannot add arguments at the end. rather, we add96// arguments before the "--" marker.97argv.splice(endOfArgsMarkerIndex, 0, ...args);98}99100return argv;101}102103export function isLaunchedFromCli(env: IProcessEnvironment): boolean {104return env['VSCODE_CLI'] === '1';105}106107108