Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/platform/environment/node/argvHelper.ts
3296 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import assert from 'assert';
7
import { IProcessEnvironment } from '../../../base/common/platform.js';
8
import { localize } from '../../../nls.js';
9
import { NativeParsedArgs } from '../common/argv.js';
10
import { ErrorReporter, NATIVE_CLI_COMMANDS, OPTIONS, parseArgs } from './argv.js';
11
12
function parseAndValidate(cmdLineArgs: string[], reportWarnings: boolean): NativeParsedArgs {
13
const onMultipleValues = (id: string, val: string) => {
14
console.warn(localize('multipleValues', "Option '{0}' is defined more than once. Using value '{1}'.", id, val));
15
};
16
const onEmptyValue = (id: string) => {
17
console.warn(localize('emptyValue', "Option '{0}' requires a non empty value. Ignoring the option.", id));
18
};
19
const onDeprecatedOption = (deprecatedOption: string, message: string) => {
20
console.warn(localize('deprecatedArgument', "Option '{0}' is deprecated: {1}", deprecatedOption, message));
21
};
22
const getSubcommandReporter = (command: string) => ({
23
onUnknownOption: (id: string) => {
24
if (!(NATIVE_CLI_COMMANDS as readonly string[]).includes(command)) {
25
console.warn(localize('unknownSubCommandOption', "Warning: '{0}' is not in the list of known options for subcommand '{1}'", id, command));
26
}
27
},
28
onMultipleValues,
29
onEmptyValue,
30
onDeprecatedOption,
31
getSubcommandReporter: (NATIVE_CLI_COMMANDS as readonly string[]).includes(command) ? getSubcommandReporter : undefined
32
});
33
const errorReporter: ErrorReporter = {
34
onUnknownOption: (id) => {
35
console.warn(localize('unknownOption', "Warning: '{0}' is not in the list of known options, but still passed to Electron/Chromium.", id));
36
},
37
onMultipleValues,
38
onEmptyValue,
39
onDeprecatedOption,
40
getSubcommandReporter
41
};
42
43
const args = parseArgs(cmdLineArgs, OPTIONS, reportWarnings ? errorReporter : undefined);
44
if (args.goto) {
45
args._.forEach(arg => assert(/^(\w:)?[^:]+(:\d*){0,2}:?$/.test(arg), localize('gotoValidation', "Arguments in `--goto` mode should be in the format of `FILE(:LINE(:CHARACTER))`.")));
46
}
47
48
return args;
49
}
50
51
function stripAppPath(argv: string[]): string[] | undefined {
52
const index = argv.findIndex(a => !/^-/.test(a));
53
54
if (index > -1) {
55
return [...argv.slice(0, index), ...argv.slice(index + 1)];
56
}
57
return undefined;
58
}
59
60
/**
61
* Use this to parse raw code process.argv such as: `Electron . --verbose --wait`
62
*/
63
export function parseMainProcessArgv(processArgv: string[]): NativeParsedArgs {
64
let [, ...args] = processArgv;
65
66
// If dev, remove the first non-option argument: it's the app location
67
if (process.env['VSCODE_DEV']) {
68
args = stripAppPath(args) || [];
69
}
70
71
// If called from CLI, don't report warnings as they are already reported.
72
const reportWarnings = !isLaunchedFromCli(process.env);
73
return parseAndValidate(args, reportWarnings);
74
}
75
76
/**
77
* Use this to parse raw code CLI process.argv such as: `Electron cli.js . --verbose --wait`
78
*/
79
export function parseCLIProcessArgv(processArgv: string[]): NativeParsedArgs {
80
let [, , ...args] = processArgv; // remove the first non-option argument: it's always the app location
81
82
// If dev, remove the first non-option argument: it's the app location
83
if (process.env['VSCODE_DEV']) {
84
args = stripAppPath(args) || [];
85
}
86
87
return parseAndValidate(args, true);
88
}
89
90
export function addArg(argv: string[], ...args: string[]): string[] {
91
const endOfArgsMarkerIndex = argv.indexOf('--');
92
if (endOfArgsMarkerIndex === -1) {
93
argv.push(...args);
94
} else {
95
// if the we have an argument "--" (end of argument marker)
96
// we cannot add arguments at the end. rather, we add
97
// arguments before the "--" marker.
98
argv.splice(endOfArgsMarkerIndex, 0, ...args);
99
}
100
101
return argv;
102
}
103
104
export function isLaunchedFromCli(env: IProcessEnvironment): boolean {
105
return env['VSCODE_CLI'] === '1';
106
}
107
108