Implemented --help.

This commit is contained in:
Daniel Rosenwasser
2014-08-01 17:12:29 -07:00
parent 616dae20f0
commit 2dd9763bad
6 changed files with 320 additions and 116 deletions

View File

@@ -4,44 +4,128 @@
/// <reference path="scanner.ts"/>
module ts {
var shortOptionNames: Map<string> = {
"d": "declaration",
"h": "help",
"m": "module",
"o": "out",
"t": "target",
"v": "version",
"w": "watch",
};
var optionDeclarations: CommandLineOption[] = [
{ name: "charset", type: "string" },
{ name: "codepage", type: "number" },
{ name: "declaration", type: "boolean" },
{ name: "diagnostics", type: "boolean" },
{ name: "help", type: "boolean" },
{ name: "locale", type: "string" },
{ name: "mapRoot", type: "string" },
{ name: "module", type: { "commonjs": ModuleKind.CommonJS, "amd": ModuleKind.AMD }, error: Diagnostics.Argument_for_module_option_must_be_commonjs_or_amd },
{ name: "noImplicitAny", type: "boolean" },
{ name: "noLib", type: "boolean" },
{ name: "noLibCheck", type: "boolean" },
{ name: "noResolve", type: "boolean" },
{ name: "out", type: "string" },
{ name: "outDir", type: "string" },
{ name: "removeComments", type: "boolean" },
{ name: "sourceMap", type: "boolean" },
{ name: "sourceRoot", type: "string" },
{ name: "target", type: { "es3": ScriptTarget.ES3, "es5": ScriptTarget.ES5 }, error: Diagnostics.Argument_for_target_option_must_be_es3_or_es5 },
{ name: "version", type: "boolean" },
{ name: "watch", type: "boolean" },
export var optionDeclarations: CommandLineOption[] = [
{
name: "charset",
type: "string",
},
{
name: "codepage",
type: "number",
},
{
name: "declaration",
shortName: "d",
type: "boolean",
description: Diagnostics.Generates_corresponding_d_ts_file,
},
{
name: "diagnostics",
type: "boolean",
},
{
name: "help",
shortName: "h",
type: "boolean",
description: Diagnostics.Print_this_message,
},
{
name: "locale",
type: "string",
},
{
name: "mapRoot",
type: "string",
description: Diagnostics.Specifies_the_location_where_debugger_should_locate_map_files_instead_of_generated_locations,
paramType: Diagnostics.LOCATION,
},
{
name: "module",
shortName: "m",
type: {
"commonjs": ModuleKind.CommonJS,
"amd": ModuleKind.AMD
},
description: Diagnostics.Specify_module_code_generation_Colon_commonjs_or_amd,
paramType: Diagnostics.KIND,
error: Diagnostics.Argument_for_module_option_must_be_commonjs_or_amd
},
{
name: "noImplicitAny",
type: "boolean",
description: Diagnostics.Warn_on_expressions_and_declarations_with_an_implied_any_type,
},
{
name: "noLib",
type: "boolean",
},
{
name: "noLibCheck",
type: "boolean",
},
{
name: "noResolve",
type: "boolean",
},
{
name: "out",
type: "string",
description: Diagnostics.Concatenate_and_emit_output_to_single_file,
paramType: Diagnostics.FILE,
},
{
name: "outDir",
type: "string",
description: Diagnostics.Redirect_output_structure_to_the_directory,
paramType: Diagnostics.DIRECTORY,
},
{
name: "removeComments",
type: "boolean",
description: Diagnostics.Do_not_emit_comments_to_output,
},
{
name: "sourcemap",
type: "boolean",
description: Diagnostics.Generates_corresponding_map_file,
},
{
name: "sourceRoot",
type: "string",
description: Diagnostics.Specifies_the_location_where_debugger_should_locate_TypeScript_files_instead_of_source_locations,
paramType: Diagnostics.LOCATION,
},
{
name: "target",
shortName: "t",
type: { "es3": ScriptTarget.ES3, "es5": ScriptTarget.ES5 },
description: Diagnostics.Specify_ECMAScript_target_version_Colon_ES3_default_or_ES5,
paramType: Diagnostics.VERSION,
error: Diagnostics.Argument_for_target_option_must_be_es3_or_es5
},
{
name: "version",
shortName: "v",
type: "boolean",
description: Diagnostics.Print_the_compiler_s_version,
},
{
name: "watch",
shortName: "w",
type: "boolean",
description: Diagnostics.Watch_input_files,
},
];
// Map command line switches to compiler options' property descriptors. Keys must be lower case spellings of command line switches.
// The 'name' property specifies the property name in the CompilerOptions type. The 'type' property specifies the type of the option.
var optionMap: Map<CommandLineOption> = {};
var shortOptionNames: Map<string> = {};
var optionNameMap: Map<CommandLineOption> = {};
forEach(optionDeclarations, option => {
optionMap[option.name.toLowerCase()] = option;
optionNameMap[option.name.toLowerCase()] = option;
if (option.shortName) {
shortOptionNames[option.shortName] = option.name;
}
});
export function parseCommandLine(commandLine: string[]): ParsedCommandLine {
@@ -75,8 +159,8 @@ module ts {
s = shortOptionNames[s];
}
if (hasProperty(optionMap, s)) {
var opt = optionMap[s];
if (hasProperty(optionNameMap, s)) {
var opt = optionNameMap[s];
// Check to see if no argument was provided (e.g. "--locale" is the last command-line argument).
if (!args[i] && opt.type !== "boolean") {

View File

@@ -247,7 +247,7 @@ module ts {
};
}
function compareValues(a: any, b: any): number {
export function compareValues<T>(a: T, b: T): number {
if (a === b) return 0;
if (a === undefined) return -1;
if (b === undefined) return 1;

View File

@@ -217,23 +217,23 @@ module ts {
Option_mapRoot_cannot_be_specified_without_specifying_sourcemap_option: { code: 5038, category: DiagnosticCategory.Error, key: "Option mapRoot cannot be specified without specifying sourcemap option." },
Option_sourceRoot_cannot_be_specified_without_specifying_sourcemap_option: { code: 5039, category: DiagnosticCategory.Error, key: "Option sourceRoot cannot be specified without specifying sourcemap option." },
Concatenate_and_emit_output_to_single_file: { code: 6001, category: DiagnosticCategory.Message, key: "Concatenate and emit output to single file." },
Generates_corresponding_0_file: { code: 6002, category: DiagnosticCategory.Message, key: "Generates corresponding {0} file." },
Generates_corresponding_d_ts_file: { code: 6002, category: DiagnosticCategory.Message, key: "Generates corresponding '.d.ts' file." },
Specifies_the_location_where_debugger_should_locate_map_files_instead_of_generated_locations: { code: 6003, category: DiagnosticCategory.Message, key: "Specifies the location where debugger should locate map files instead of generated locations." },
Specifies_the_location_where_debugger_should_locate_TypeScript_files_instead_of_source_locations: { code: 6004, category: DiagnosticCategory.Message, key: "Specifies the location where debugger should locate TypeScript files instead of source locations." },
Watch_input_files: { code: 6005, category: DiagnosticCategory.Message, key: "Watch input files." },
Redirect_output_structure_to_the_directory: { code: 6006, category: DiagnosticCategory.Message, key: "Redirect output structure to the directory." },
Do_not_emit_comments_to_output: { code: 6009, category: DiagnosticCategory.Message, key: "Do not emit comments to output." },
Skip_resolution_and_preprocessing: { code: 6010, category: DiagnosticCategory.Message, key: "Skip resolution and preprocessing." },
Specify_ECMAScript_target_version_Colon_0_default_or_1: { code: 6015, category: DiagnosticCategory.Message, key: "Specify ECMAScript target version: '{0}' (default), or '{1}'" },
Specify_module_code_generation_Colon_0_or_1: { code: 6016, category: DiagnosticCategory.Message, key: "Specify module code generation: '{0}' or '{1}'" },
Specify_ECMAScript_target_version_Colon_ES3_default_or_ES5: { code: 6015, category: DiagnosticCategory.Message, key: "Specify ECMAScript target version: 'ES3' (default), or 'ES5'" },
Specify_module_code_generation_Colon_commonjs_or_amd: { code: 6016, category: DiagnosticCategory.Message, key: "Specify module code generation: 'commonjs' or 'amd'" },
Print_this_message: { code: 6017, category: DiagnosticCategory.Message, key: "Print this message." },
Print_the_compiler_s_version_Colon_0: { code: 6019, category: DiagnosticCategory.Message, key: "Print the compiler's version: {0}" },
Print_the_compiler_s_version: { code: 6019, category: DiagnosticCategory.Message, key: "Print the compiler's version." },
Allow_use_of_deprecated_0_keyword_when_referencing_an_external_module: { code: 6021, category: DiagnosticCategory.Message, key: "Allow use of deprecated '{0}' keyword when referencing an external module." },
Specify_locale_for_errors_and_messages_For_example_0_or_1: { code: 6022, category: DiagnosticCategory.Message, key: "Specify locale for errors and messages. For example '{0}' or '{1}'" },
Syntax_Colon_0: { code: 6023, category: DiagnosticCategory.Message, key: "Syntax: {0}" },
Syntax_Colon_0: { code: 6023, category: DiagnosticCategory.Message, key: "Syntax: {0}" },
options: { code: 6024, category: DiagnosticCategory.Message, key: "options" },
file: { code: 6025, category: DiagnosticCategory.Message, key: "file" },
Examples_Colon: { code: 6026, category: DiagnosticCategory.Message, key: "Examples:" },
Examples_Colon_0: { code: 6026, category: DiagnosticCategory.Message, key: "Examples: {0}" },
Options_Colon: { code: 6027, category: DiagnosticCategory.Message, key: "Options:" },
Insert_command_line_options_and_files_from_a_file: { code: 6030, category: DiagnosticCategory.Message, key: "Insert command line options and files from a file." },
Version_0: { code: 6029, category: DiagnosticCategory.Message, key: "Version {0}" },
@@ -249,6 +249,17 @@ module ts {
Specify_the_codepage_to_use_when_opening_source_files: { code: 6040, category: DiagnosticCategory.Message, key: "Specify the codepage to use when opening source files." },
Additional_locations_Colon: { code: 6041, category: DiagnosticCategory.Message, key: "Additional locations:" },
Compile_complete_Listening_for_changed_files: { code: 6042, category: DiagnosticCategory.Message, key: "Compile complete. Listening for changed files." },
Generates_corresponding_map_file: { code: 6043, category: DiagnosticCategory.Message, key: "Generates corresponding '.map' file." },
Compiler_option_0_expects_an_argument: { code: 6044, category: DiagnosticCategory.Error, key: "Compiler option '{0}' expects an argument." },
Unterminated_quoted_string_in_response_file_0: { code: 6045, category: DiagnosticCategory.Error, key: "Unterminated quoted string in response file '{0}'." },
Argument_for_module_option_must_be_commonjs_or_amd: { code: 6045, category: DiagnosticCategory.Error, key: "Argument for '--module' option must be 'commonjs' or 'amd'." },
Argument_for_target_option_must_be_es3_or_es5: { code: 6046, category: DiagnosticCategory.Error, key: "Argument for '--target' option must be 'es3' or 'es5'." },
Locale_must_be_of_the_form_language_or_language_territory_For_example_0_or_1: { code: 6047, category: DiagnosticCategory.Error, key: "Locale must be of the form <language> or <language>-<territory>. For example '{0}' or '{1}'." },
Unsupported_locale_0: { code: 6048, category: DiagnosticCategory.Error, key: "Unsupported locale '{0}'." },
Unable_to_open_file_0: { code: 6049, category: DiagnosticCategory.Error, key: "Unable to open file '{0}'." },
Corrupted_locale_file_0: { code: 6050, category: DiagnosticCategory.Error, key: "Corrupted locale file {0}." },
No_input_files_specified: { code: 6051, category: DiagnosticCategory.Error, key: "No input files specified." },
Warn_on_expressions_and_declarations_with_an_implied_any_type: { code: 7004, category: DiagnosticCategory.Message, key: "Warn on expressions and declarations with an implied 'any' type." },
Variable_0_implicitly_has_an_1_type: { code: 7005, category: DiagnosticCategory.Error, key: "Variable '{0}' implicitly has an '{1}' type." },
Parameter_0_implicitly_has_an_1_type: { code: 7006, category: DiagnosticCategory.Error, key: "Parameter '{0}' implicitly has an '{1}' type." },
Member_0_implicitly_has_an_1_type: { code: 7008, category: DiagnosticCategory.Error, key: "Member '{0}' implicitly has an '{1}' type." },
@@ -334,14 +345,5 @@ module ts {
Import_declaration_conflicts_with_local_declaration_of_0: { code: -9999999, category: DiagnosticCategory.Error, key: "Import declaration conflicts with local declaration of '{0}'" },
Module_0_is_hidden_by_a_local_declaration_with_the_same_name: { code: -9999999, category: DiagnosticCategory.Error, key: "Module '{0}' is hidden by a local declaration with the same name" },
Filename_0_differs_from_already_included_filename_1_only_in_casing: { code: -9999999, category: DiagnosticCategory.Error, key: "Filename '{0}' differs from already included filename '{1}' only in casing" },
Argument_for_module_option_must_be_commonjs_or_amd: { code: -9999999, category: DiagnosticCategory.Error, key: "Argument for '--module' option must be 'commonjs' or 'amd'." },
Argument_for_target_option_must_be_es3_or_es5: { code: -9999999, category: DiagnosticCategory.Error, key: "Argument for '--target' option must be 'es3' or 'es5'." },
Compiler_option_0_expects_an_argument: { code: -9999999, category: DiagnosticCategory.Error, key: "Compiler option '{0}' expects an argument." },
Unterminated_quoted_string_in_response_file_0: { code: -9999999, category: DiagnosticCategory.Error, key: "Unterminated quoted string in response file '{0}'." },
Locale_must_be_of_the_form_language_or_language_territory_For_example_0_or_1: { code: -9999999, category: DiagnosticCategory.Error, key: "Locale must be of the form <language> or <language>-<territory>. For example '{0}' or '{1}'." },
Unsupported_locale_0: { code: -9999999, category: DiagnosticCategory.Error, key: "Unsupported locale {0}." },
Unable_to_open_file_0: { code: -9999999, category: DiagnosticCategory.Error, key: "Unable to open file {0}." },
Corrupted_locale_file_0: { code: -9999999, category: DiagnosticCategory.Error, key: "Corrupted locale file {0}." },
No_input_files_specified: { code: -9999999, category: DiagnosticCategory.Error, key: "No input files specified." },
};
}

View File

@@ -862,7 +862,7 @@
"category": "Message",
"code": 6001
},
"Generates corresponding {0} file.": {
"Generates corresponding '.d.ts' file.": {
"category": "Message",
"code": 6002
},
@@ -890,11 +890,11 @@
"category": "Message",
"code": 6010
},
"Specify ECMAScript target version: '{0}' (default), or '{1}'": {
"Specify ECMAScript target version: 'ES3' (default), or 'ES5'": {
"category": "Message",
"code": 6015
},
"Specify module code generation: '{0}' or '{1}'": {
"Specify module code generation: 'commonjs' or 'amd'": {
"category": "Message",
"code": 6016
},
@@ -902,7 +902,7 @@
"category": "Message",
"code": 6017
},
"Print the compiler's version: {0}": {
"Print the compiler's version.": {
"category": "Message",
"code": 6019
},
@@ -914,7 +914,7 @@
"category": "Message",
"code": 6022
},
"Syntax: {0}": {
"Syntax: {0}": {
"category": "Message",
"code": 6023
},
@@ -926,7 +926,7 @@
"category": "Message",
"code": 6025
},
"Examples:": {
"Examples: {0}": {
"category": "Message",
"code": 6026
},
@@ -994,11 +994,54 @@
"category": "Message",
"code": 6042
},
"Generates corresponding '.map' file.": {
"category": "Message",
"code": 6043
},
"Compiler option '{0}' expects an argument.": {
"category": "Error",
"code": 6044
},
"Unterminated quoted string in response file '{0}'.": {
"category": "Error",
"code": 6045
},
"Argument for '--module' option must be 'commonjs' or 'amd'.": {
"category": "Error",
"code": 6045
},
"Argument for '--target' option must be 'es3' or 'es5'.": {
"category": "Error",
"code": 6046
},
"Locale must be of the form <language> or <language>-<territory>. For example '{0}' or '{1}'.": {
"category": "Error",
"code": 6047
},
"Unsupported locale '{0}'.": {
"category": "Error",
"code": 6048
},
"Unable to open file '{0}'.": {
"category": "Error",
"code": 6049
},
"Corrupted locale file {0}.": {
"category": "Error",
"code": 6050
},
"No input files specified.": {
"category": "Error",
"code": 6051
},
"Warn on expressions and declarations with an implied 'any' type.": {
"category": "Message",
"code": 7004
},
"Variable '{0}' implicitly has an '{1}' type.": {
"category": "Error",
"code": 7005
},
"Parameter '{0}' implicitly has an '{1}' type.": {
"category": "Error",
"code": 7006
@@ -1358,41 +1401,5 @@
"Filename '{0}' differs from already included filename '{1}' only in casing": {
"category": "Error",
"code": -9999999
},
"Argument for '--module' option must be 'commonjs' or 'amd'.": {
"category": "Error",
"code": -9999999
},
"Argument for '--target' option must be 'es3' or 'es5'.": {
"category": "Error",
"code": -9999999
},
"Compiler option '{0}' expects an argument.": {
"category": "Error",
"code": -9999999
},
"Unterminated quoted string in response file '{0}'.": {
"category": "Error",
"code": -9999999
},
"Locale must be of the form <language> or <language>-<territory>. For example '{0}' or '{1}'.": {
"category": "Error",
"code": -9999999
},
"Unsupported locale {0}.": {
"category": "Error",
"code": -9999999
},
"Unable to open file {0}.": {
"category": "Error",
"code": -9999999
},
"Corrupted locale file {0}.": {
"category": "Error",
"code": -9999999
},
"No input files specified.": {
"category": "Error",
"code": -9999999
}
}

View File

@@ -78,19 +78,24 @@ module ts {
return count;
}
function reportDiagnostic(error: Diagnostic) {
if (error.file) {
var loc = error.file.getLineAndCharacterFromPosition(error.start);
sys.write(error.file.filename + "(" + loc.line + "," + loc.character + "): " + error.messageText + sys.newLine);
function getDiagnosticText(message: DiagnosticMessage, ...args: any[]): string {
var diagnostic: Diagnostic = createCompilerDiagnostic.apply(undefined, arguments);
return diagnostic.messageText;
}
function reportDiagnostic(diagnostic: Diagnostic) {
if (diagnostic.file) {
var loc = diagnostic.file.getLineAndCharacterFromPosition(diagnostic.start);
sys.write(diagnostic.file.filename + "(" + loc.line + "," + loc.character + "): " + diagnostic.messageText + sys.newLine);
}
else {
sys.write(error.messageText + sys.newLine);
sys.write(diagnostic.messageText + sys.newLine);
}
}
function reportDiagnostics(errors: Diagnostic[]) {
for (var i = 0; i < errors.length; i++) {
reportDiagnostic(errors[i]);
function reportDiagnostics(diagnostics: Diagnostic[]) {
for (var i = 0; i < diagnostics.length; i++) {
reportDiagnostic(diagnostics[i]);
}
}
@@ -179,29 +184,33 @@ module ts {
}
export function executeCommandLine(args: string[]): void {
var exitCode = 0;
var commandLine = parseCommandLine(args);
if (commandLine.options.locale) {
validateLocaleAndSetLanguage(commandLine.options.locale, commandLine.errors);
}
// Report all errors at this point, even if there are none.
if (commandLine.errors.length > 0) {
reportDiagnostics(commandLine.errors);
exitCode = 1;
}
if (commandLine.options.version) {
reportDiagnostic(createCompilerDiagnostic(Diagnostics.Version_0, version));
sys.exit(0);
sys.exit(exitCode);
}
if (commandLine.options.help) {
// TODO (drosen): Usage.
sys.exit(0);
if (commandLine.options.help || commandLine.filenames.length === 0) {
printVersion();
printHelp();
sys.exit(exitCode);
}
if (commandLine.filenames.length === 0) {
commandLine.errors.push(createCompilerDiagnostic(Diagnostics.No_input_files_specified));
}
if (commandLine.errors.length) {
reportDiagnostics(commandLine.errors);
sys.exit(1);
// If we encountered an error before we even started compiling, just bail out.
if (exitCode !== 0) {
sys.exit(exitCode);
}
var defaultCompilerHost = createCompilerHost(commandLine.options);
@@ -338,6 +347,105 @@ module ts {
return { program: program, errors: errors };
}
function printVersion() {
sys.write(getDiagnosticText(Diagnostics.Version_0, version) + sys.newLine);
}
function printHelp() {
var output = "";
// We want to align our "syntax" and "examples" commands to a certain margin.
var syntaxLength = getDiagnosticText(Diagnostics.Syntax_Colon_0, "").length
var examplesLength = getDiagnosticText(Diagnostics.Examples_Colon_0, "").length
var marginLength = Math.max(syntaxLength, examplesLength);
// Build up the syntactic skeleton.
var syntax = makePadding(marginLength - syntaxLength);
syntax += "tsc [" + getDiagnosticText(Diagnostics.options) + "] [" + getDiagnosticText(Diagnostics.file) + " ...]";
output += getDiagnosticText(Diagnostics.Syntax_Colon_0, syntax);
output += sys.newLine + sys.newLine;
// Build up the examples.
var padding = makePadding(marginLength);
output += getDiagnosticText(Diagnostics.Examples_Colon_0, makePadding(marginLength - examplesLength) + "tsc hello.ts") + sys.newLine;
output += padding + "tsc --out foo.js foo.ts" + sys.newLine;
output += padding + "tsc @args.txt" + sys.newLine;
output += sys.newLine;
// Sort our options by their command names, (e.g. "--noImplicitAny" comes before "--watch")
output += getDiagnosticText(Diagnostics.Options_Colon) + sys.newLine;
var optsList = optionDeclarations.slice().sort((a, b) => {
var aName = a.name.toLowerCase();
var bName = b.name.toLowerCase();
return compareValues(aName, bName);
});
// We want our descriptions to align at the same column in our output,
// so we keep track of the longest option usage string.
var marginLength = 0;
var usageColumn: string[] = []; // Things like "-d, --declaration" go in here.
var descriptionColumn: string[] = [];
for (var i = 0; i < optsList.length; i++) {
var option = optsList[i];
// If an option lacks a description,
// it is not officially supported.
if (!option.description) {
continue;
}
var usageText = " ";
if (option.shortName) {
usageText += "-" + option.shortName;
usageText += getParamName(option);
usageText += ", ";
}
usageText += "--" + option.name;
usageText += getParamName(option);
usageColumn.push(usageText);
descriptionColumn.push(getDiagnosticText(option.description));
// Set the new margin for the description column if necessary.
if (usageText.length > marginLength) {
marginLength = usageText.length;
}
}
// Special case that can't fit in the loop.
var usageText = " @<" + getDiagnosticText(Diagnostics.file) + ">";
usageColumn.push(usageText);
descriptionColumn.push(getDiagnosticText(Diagnostics.Insert_command_line_options_and_files_from_a_file));
if (usageText.length > marginLength) {
marginLength = usageText.length;
}
// Print out each row, aligning all the descriptions on the same column.
for (var i = 0; i < usageColumn.length; i++) {
var usage = usageColumn[i];
var description = descriptionColumn[i];
output += usage + makePadding(marginLength - usage.length + 2) + description + sys.newLine;
}
sys.write(output);
return;
function getParamName(option: CommandLineOption) {
if (option.paramName !== undefined) {
return " " + getDiagnosticText(option.paramName);
}
return "";
}
function makePadding(paddingLength: number): string {
return Array(paddingLength + 1).join(" ");
}
}
}
ts.executeCommandLine(sys.args);

View File

@@ -953,7 +953,10 @@ module ts {
export interface CommandLineOption {
name: string;
type: any;
error?: DiagnosticMessage;
shortName?: string;
description?: DiagnosticMessage;
paramName?: DiagnosticMessage; // The name to be used for a non-boolean option's parameter.
error?: DiagnosticMessage; // The error given when the argument does not fit a customized 'type'.
}
export enum CharacterCodes {