argv.ts 4.8 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 { localize } from 'vs/nls';
J
Joao Moreno 已提交
9 10

export interface ParsedArgs extends minimist.ParsedArgs {
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33
	help?: boolean;
	version?: boolean;
	wait?: boolean;
	diff?: boolean;
	goto?: boolean;
	'new-window'?: boolean;
	'reuse-window'?: boolean;
	locale?: string;
	'user-data-dir'?: string;
	performance?: boolean;
	verbose?: boolean;
	logExtensionHostCommunication?: boolean;
	debugBrkFileWatcherPort?: string;
	'disable-extensions'?: boolean;
	extensionHomePath?: string;
	extensionDevelopmentPath?: string;
	extensionTestsPath?: string;
	timestamp?: string;
	debugBrkPluginHost?: string;
	debugPluginHost?: string;
	'list-extensions'?: boolean;
	'install-extension'?: string | string[];
	'uninstall-extension'?: string | string[];
J
Joao Moreno 已提交
34 35
}

J
Joao Moreno 已提交
36
const options: minimist.Opts = {
37 38
	string: [
		'locale',
J
Joao Moreno 已提交
39 40 41 42
		'user-data-dir',
		'extensionHomePath',
		'extensionDevelopmentPath',
		'extensionTestsPath',
J
Joao Moreno 已提交
43
		'timestamp',
J
Joao Moreno 已提交
44 45
		'install-extension',
		'uninstall-extension'
46 47 48 49 50 51 52 53
	],
	boolean: [
		'help',
		'version',
		'wait',
		'diff',
		'goto',
		'new-window',
J
Joao Moreno 已提交
54 55 56 57
		'reuse-window',
		'performance',
		'verbose',
		'logExtensionHostCommunication',
J
Joao Moreno 已提交
58 59
		'disable-extensions',
		'list-extensions'
60
	],
J
Joao Moreno 已提交
61 62 63
	alias: {
		help: 'h',
		version: 'v',
64 65 66 67
		wait: 'w',
		diff: 'd',
		goto: 'g',
		'new-window': 'n',
J
Joao Moreno 已提交
68 69 70
		'reuse-window': 'r',
		performance: 'p',
		'disable-extensions': 'disableExtensions'
J
Joao Moreno 已提交
71
	}
J
Joao Moreno 已提交
72
};
J
Joao Moreno 已提交
73

B
Benjamin Pasero 已提交
74
export function parseArgs(args: string[]): ParsedArgs {
J
Joao Moreno 已提交
75
	return minimist(args, options) as ParsedArgs;
J
Joao Moreno 已提交
76 77 78
}

const executable = 'code' + (os.platform() === 'win32' ? '.exe' : '');
J
Joao Moreno 已提交
79 80 81 82 83

export const optionsHelp: { [name: string]: string; } = {
	'-d, --diff': localize('diff', "Open a diff editor. Requires to pass two file paths as arguments."),
	'--disable-extensions': localize('disableExtensions', "Disable all installed extensions."),
	'-g, --goto': localize('goto', "Open the file at path at the line and column (add :line[:column] to path)."),
84
	'--locale=<locale>': localize('locale', "The locale to use (e.g. en-US or zh-TW)."),
J
Joao Moreno 已提交
85
	'-n, --new-window': localize('newWindow', "Force a new instance of Code."),
D
Daniel Imms 已提交
86
	'-p, --performance': localize('performance', "Start with the 'Developer: Startup Performance' command enabled."),
J
Joao Moreno 已提交
87 88 89 90
	'-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."),
J
Joao Moreno 已提交
91
	'--extensionHomePath': localize('extensionHomePath', "Set the root path for extensions."),
J
Joao Moreno 已提交
92
	'--list-extensions': localize('listExtensions', "List the installed extensions."),
D
Daniel Imms 已提交
93 94
	'--install-extension <ext>': localize('installExtension', "Installs an extension."),
	'--uninstall-extension <ext>': localize('uninstallExtension', "Uninstalls an extension."),
J
Joao Moreno 已提交
95 96 97 98
	'-v, --version': localize('version', "Print version."),
	'-h, --help': localize('help', "Print usage.")
};

D
Daniel Imms 已提交
99 100 101 102 103 104 105 106 107 108 109
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
		return keys.reduce((r, key) => r.concat([`  ${ key }`, `      ${ options[key] }`]), []).join('\n');
	}
	let descriptionColumns = columns - argLength - 1;
	let result = '';
	keys.forEach(k => {
		let wrappedDescription = wrapText(options[k], descriptionColumns);
D
Daniel Imms 已提交
110 111 112 113 114
		let keyPadding = (<any>' ').repeat(argLength - k.length - 2/*left padding*/);
		if (result.length > 0) {
			result += '\n';
		}
		result += '  ' + k + keyPadding + wrappedDescription[0];
D
Daniel Imms 已提交
115
		for (var i = 1; i < wrappedDescription.length; i++) {
D
Daniel Imms 已提交
116
			result += '\n' + (<any>' ').repeat(argLength) + wrappedDescription[i];
D
Daniel Imms 已提交
117 118 119
		}
	});
	return result;
J
Joao Moreno 已提交
120 121
}

D
Daniel Imms 已提交
122 123 124 125
function wrapText(text: string, columns: number) : string[] {
	let lines = [];
	while (text.length) {
		let index = text.length < columns ? text.length : text.lastIndexOf(' ', columns);
D
Daniel Imms 已提交
126
		let line = text.slice(0, index).trim();
D
Daniel Imms 已提交
127 128 129 130 131 132
		text = text.slice(index);
		lines.push(line);
	}
	return lines;
}

D
Daniel Imms 已提交
133
export function buildHelpMessage(version: string): string {
D
Daniel Imms 已提交
134
	let columns = (<any>process.stdout).isTTY ? (<any>process.stdout).columns : 80;
D
Daniel Imms 已提交
135
	return `Visual Studio Code v${ version }
D
Daniel Imms 已提交
136

J
Joao Moreno 已提交
137 138 139 140

Usage: ${ executable } [arguments] [paths...]

Options:
D
Daniel Imms 已提交
141
${formatOptions(optionsHelp, columns)}`;
142
}