argv.ts 6.7 KB
Newer Older
J
Joao Moreno 已提交
1 2 3 4 5
/*---------------------------------------------------------------------------------------------
 *  Copyright (c) Microsoft Corporation. All rights reserved.
 *  Licensed under the MIT License. See License.txt in the project root for license information.
 *--------------------------------------------------------------------------------------------*/

J
Joao Moreno 已提交
6
import * as os from 'os';
J
Joao Moreno 已提交
7
import * as minimist from 'minimist';
J
Joao Moreno 已提交
8
import * as assert from 'assert';
J
Joao Moreno 已提交
9
import { firstIndex } from 'vs/base/common/arrays';
J
Joao Moreno 已提交
10
import { localize } from 'vs/nls';
J
Joao Moreno 已提交
11
import { ParsedArgs } from '../common/environment';
12
import product from 'vs/platform/node/product';
J
Joao Moreno 已提交
13

J
Joao Moreno 已提交
14
const options: minimist.Opts = {
15 16
	string: [
		'locale',
J
Joao Moreno 已提交
17
		'user-data-dir',
18
		'extensions-dir',
J
Joao Moreno 已提交
19 20
		'extensionDevelopmentPath',
		'extensionTestsPath',
J
Joao Moreno 已提交
21
		'install-extension',
22
		'uninstall-extension',
23
		'debugId',
J
Joao Moreno 已提交
24
		'debugPluginHost',
25
		'debugBrkPluginHost',
26 27
		'debugSearch',
		'debugBrkSearch',
28
		'open-url',
29
		'enable-proposed-api'
30 31 32 33 34 35
	],
	boolean: [
		'help',
		'version',
		'wait',
		'diff',
36
		'add',
37 38
		'goto',
		'new-window',
39
		'unity-launch',
J
Joao Moreno 已提交
40 41
		'reuse-window',
		'performance',
J
Johannes Rieken 已提交
42
		'prof-startup',
J
Joao Moreno 已提交
43 44
		'verbose',
		'logExtensionHostCommunication',
J
Joao Moreno 已提交
45
		'disable-extensions',
G
greams 已提交
46
		'list-extensions',
B
Benjamin Pasero 已提交
47
		'show-versions',
48
		'nolazy',
49
		'skip-getting-started'
50
	],
J
Joao Moreno 已提交
51
	alias: {
52
		add: 'a',
J
Joao Moreno 已提交
53 54
		help: 'h',
		version: 'v',
55 56 57 58
		wait: 'w',
		diff: 'd',
		goto: 'g',
		'new-window': 'n',
J
Joao Moreno 已提交
59 60
		'reuse-window': 'r',
		performance: 'p',
61
		'disable-extensions': 'disableExtensions',
62 63 64
		'extensions-dir': 'extensionHomePath',
		'debugPluginHost': 'inspect-extensions',
		'debugBrkPluginHost': 'inspect-brk-extensions',
65 66
		'debugSearch': 'inspect-search',
		'debugBrkSearch': 'inspect-brk-search',
J
Joao Moreno 已提交
67
	}
J
Joao Moreno 已提交
68
};
J
Joao Moreno 已提交
69

J
Joao Moreno 已提交
70 71
function validate(args: ParsedArgs): ParsedArgs {
	if (args.goto) {
72
		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))`.")));
J
Joao Moreno 已提交
73 74 75 76 77 78 79 80 81 82 83
	}

	return args;
}

function stripAppPath(argv: string[]): string[] {
	const index = firstIndex(argv, a => !/^-/.test(a));

	if (index > -1) {
		return [...argv.slice(0, index), ...argv.slice(index + 1)];
	}
M
Matt Bierner 已提交
84
	return undefined;
J
Joao Moreno 已提交
85 86
}

J
Joao Moreno 已提交
87 88 89 90
/**
 * Use this to parse raw code process.argv such as: `Electron . --verbose --wait`
 */
export function parseMainProcessArgv(processArgv: string[]): ParsedArgs {
J
Joao Moreno 已提交
91
	let [, ...args] = processArgv;
J
Joao Moreno 已提交
92 93 94

	// If dev, remove the first non-option argument: it's the app location
	if (process.env['VSCODE_DEV']) {
J
Joao Moreno 已提交
95 96 97 98 99
		args = stripAppPath(args);
	}

	return validate(parseArgs(args));
}
J
Joao Moreno 已提交
100

J
Joao Moreno 已提交
101 102 103 104
/**
 * Use this to parse raw code CLI process.argv such as: `Electron cli.js . --verbose --wait`
 */
export function parseCLIProcessArgv(processArgv: string[]): ParsedArgs {
J
Johannes Rieken 已提交
105
	let [, , ...args] = processArgv;
J
Joao Moreno 已提交
106 107 108

	if (process.env['VSCODE_DEV']) {
		args = stripAppPath(args);
J
Joao Moreno 已提交
109 110
	}

J
Joao Moreno 已提交
111
	return validate(parseArgs(args));
J
Joao Moreno 已提交
112 113 114 115 116
}

/**
 * Use this to parse code arguments such as `--verbose --wait`
 */
B
Benjamin Pasero 已提交
117
export function parseArgs(args: string[]): ParsedArgs {
118
	return minimist(args, options) as ParsedArgs;
J
Joao Moreno 已提交
119 120
}

J
Joao Moreno 已提交
121
export const optionsHelp: { [name: string]: string; } = {
B
Benjamin Pasero 已提交
122 123 124
	'-d, --diff <file> <file>': localize('diff', "Compare two files with each other."),
	'-a, --add <dir>': localize('add', "Add folder(s) to the last active window."),
	'-g, --goto <file:line[:character]>': localize('goto', "Open a file at the path on the specified line and character position."),
125
	'--locale <locale>': localize('locale', "The locale to use (e.g. en-US or zh-TW)."),
J
Joao Moreno 已提交
126
	'-n, --new-window': localize('newWindow', "Force a new instance of Code."),
D
Daniel Imms 已提交
127
	'-p, --performance': localize('performance', "Start with the 'Developer: Startup Performance' command enabled."),
J
Johannes Rieken 已提交
128
	'--prof-startup': localize('prof-startup', "Run CPU profiler during startup"),
J
Joao Moreno 已提交
129 130 131 132
	'-r, --reuse-window': localize('reuseWindow', "Force opening a file or folder in the last active window."),
	'--user-data-dir <dir>': localize('userDataDir', "Specifies the directory that user data is kept in, useful when running as root."),
	'--verbose': localize('verbose', "Print verbose output (implies --wait)."),
	'-w, --wait': localize('wait', "Wait for the window to be closed before returning."),
133
	'--extensions-dir <dir>': localize('extensionHomePath', "Set the root path for extensions."),
J
Joao Moreno 已提交
134
	'--list-extensions': localize('listExtensions', "List the installed extensions."),
J
Joao Moreno 已提交
135
	'--show-versions': localize('showVersions', "Show versions of installed extensions, when using --list-extension."),
J
Joao 已提交
136 137 138
	'--install-extension (<extension-id> | <extension-vsix-path>)': localize('installExtension', "Installs an extension."),
	'--uninstall-extension <extension-id>': localize('uninstallExtension', "Uninstalls an extension."),
	'--enable-proposed-api <extension-id>': localize('experimentalApis', "Enables proposed api features for an extension."),
J
Joao Moreno 已提交
139
	'--disable-extensions': localize('disableExtensions', "Disable all installed extensions."),
140
	'--disable-gpu': localize('disableGPU', "Disable GPU hardware acceleration."),
J
Joao Moreno 已提交
141 142 143 144
	'-v, --version': localize('version', "Print version."),
	'-h, --help': localize('help', "Print usage.")
};

145 146 147 148 149
// TODO@Ben multi root
if (product.quality === 'stable') {
	delete optionsHelp['-a, --add'];
}

D
Daniel Imms 已提交
150 151 152 153 154
export function formatOptions(options: { [name: string]: string; }, columns: number): string {
	let keys = Object.keys(options);
	let argLength = Math.max.apply(null, keys.map(k => k.length)) + 2/*left padding*/ + 1/*right padding*/;
	if (columns - argLength < 25) {
		// Use a condensed version on narrow terminals
J
Johannes Rieken 已提交
155
		return keys.reduce((r, key) => r.concat([`  ${key}`, `      ${options[key]}`]), []).join('\n');
D
Daniel Imms 已提交
156 157 158 159 160
	}
	let descriptionColumns = columns - argLength - 1;
	let result = '';
	keys.forEach(k => {
		let wrappedDescription = wrapText(options[k], descriptionColumns);
D
Daniel Imms 已提交
161 162 163 164 165
		let keyPadding = (<any>' ').repeat(argLength - k.length - 2/*left padding*/);
		if (result.length > 0) {
			result += '\n';
		}
		result += '  ' + k + keyPadding + wrappedDescription[0];
166
		for (let i = 1; i < wrappedDescription.length; i++) {
D
Daniel Imms 已提交
167
			result += '\n' + (<any>' ').repeat(argLength) + wrappedDescription[i];
D
Daniel Imms 已提交
168 169 170
		}
	});
	return result;
J
Joao Moreno 已提交
171 172
}

J
Johannes Rieken 已提交
173
function wrapText(text: string, columns: number): string[] {
B
Benjamin Pasero 已提交
174
	let lines: string[] = [];
D
Daniel Imms 已提交
175 176
	while (text.length) {
		let index = text.length < columns ? text.length : text.lastIndexOf(' ', columns);
D
Daniel Imms 已提交
177
		let line = text.slice(0, index).trim();
D
Daniel Imms 已提交
178 179 180 181 182 183
		text = text.slice(index);
		lines.push(line);
	}
	return lines;
}

J
Joao Moreno 已提交
184 185
export function buildHelpMessage(fullName: string, name: string, version: string): string {
	const columns = (<any>process.stdout).isTTY ? (<any>process.stdout).columns : 80;
J
Johannes Rieken 已提交
186
	const executable = `${name}${os.platform() === 'win32' ? '.exe' : ''}`;
D
Daniel Imms 已提交
187

J
Johannes Rieken 已提交
188
	return `${fullName} ${version}
J
Joao Moreno 已提交
189

J
Johannes Rieken 已提交
190
${ localize('usage', "Usage")}: ${executable} [${localize('options', "options")}] [${localize('paths', 'paths')}...]
J
Joao Moreno 已提交
191

J
Johannes Rieken 已提交
192
${ localize('optionsUpperCase', "Options")}:
D
Daniel Imms 已提交
193
${formatOptions(optionsHelp, columns)}`;
194
}