From 82cdb289706f90af405e292a2825a6a9293bb81a Mon Sep 17 00:00:00 2001 From: Livar Cunha Date: Wed, 16 Dec 2015 15:45:24 -0800 Subject: [PATCH] System.CommandLine uses Environment.FailFast when help or ReportError is detected. This causes the debugger to get invoked in windows and is not desirable. The fix here is to handle error and help ourselves, but still use the getHelpText from System.CommandLine to generate the help content. --- .../Program.cs | 24 ++++++++++++-- .../ArgValues.cs | 3 ++ .../ArgumentsParser.cs | 32 +++++++++++++++++-- .../Program.cs | 3 ++ 4 files changed, 57 insertions(+), 5 deletions(-) diff --git a/src/Microsoft.DotNet.Tools.Compiler.Csc/Program.cs b/src/Microsoft.DotNet.Tools.Compiler.Csc/Program.cs index f387d001f..6633ecb44 100644 --- a/src/Microsoft.DotNet.Tools.Compiler.Csc/Program.cs +++ b/src/Microsoft.DotNet.Tools.Compiler.Csc/Program.cs @@ -32,11 +32,17 @@ namespace Microsoft.DotNet.Tools.Compiler.Csc IReadOnlyList resources = Array.Empty(); IReadOnlyList sources = Array.Empty(); string outputName = null; + var help = false; + var returnCode = 0; + string helpText = null; try { ArgumentSyntax.Parse(args, syntax => { + syntax.HandleHelp = false; + syntax.HandleErrors = false; + commonOptions = CommonCompilerOptionsExtensions.Parse(syntax); assemblyInfoOptions = AssemblyInfoOptions.Parse(syntax); @@ -49,16 +55,30 @@ namespace Microsoft.DotNet.Tools.Compiler.Csc syntax.DefineOptionList("resource", ref resources, "Resources to embed"); + syntax.DefineOption("h|help", ref help, "Help for compile native."); + syntax.DefineParameterList("source-files", ref sources, "Compilation sources"); + + helpText = syntax.GetHelpText(); + if (tempOutDir == null) { syntax.ReportError("Option '--temp-output' is required"); } }); } - catch (ArgumentSyntaxException) + catch (ArgumentSyntaxException exception) { - return ExitFailed; + Console.Error.WriteLine(exception.Message); + help = true; + returnCode = ExitFailed; + } + + if (help) + { + Console.WriteLine(helpText); + + return returnCode; } var translated = TranslateCommonOptions(commonOptions); diff --git a/src/Microsoft.DotNet.Tools.Compiler.Native/ArgValues.cs b/src/Microsoft.DotNet.Tools.Compiler.Native/ArgValues.cs index 161a4e495..27f4ad8c8 100644 --- a/src/Microsoft.DotNet.Tools.Compiler.Native/ArgValues.cs +++ b/src/Microsoft.DotNet.Tools.Compiler.Native/ArgValues.cs @@ -17,6 +17,9 @@ namespace Microsoft.DotNet.Tools.Compiler.Native public string AppDepSDKPath { get; set; } public string IlcPath { get; set; } + public bool IsHelp { get; set; } + public int ReturnCode { get; set; } + public NativeCompileSettings GetNativeCompileSettings() { var config = NativeCompileSettings.Default; diff --git a/src/Microsoft.DotNet.Tools.Compiler.Native/ArgumentsParser.cs b/src/Microsoft.DotNet.Tools.Compiler.Native/ArgumentsParser.cs index 3189f6978..3a81ecdcc 100644 --- a/src/Microsoft.DotNet.Tools.Compiler.Native/ArgumentsParser.cs +++ b/src/Microsoft.DotNet.Tools.Compiler.Native/ArgumentsParser.cs @@ -19,14 +19,20 @@ namespace Microsoft.DotNet.Tools.Compiler.Native string ilcPath = null; string appDepSdk = null; string logPath = null; + var help = false; + string helpText = null; + var returnCode = 0; IReadOnlyList references = Array.Empty(); - IReadOnlyList linklib = Array.Empty(); + IReadOnlyList linklib = Array.Empty(); try { ArgumentSyntax.Parse(args, syntax => { + syntax.HandleHelp = false; + syntax.HandleErrors = false; + syntax.DefineOption("output", ref outputDirectory, "Output Directory for native executable."); syntax.DefineOption("temp-output", ref temporaryOutputDirectory, "Directory for intermediate files."); @@ -48,12 +54,17 @@ namespace Microsoft.DotNet.Tools.Compiler.Native // Optional Log Path syntax.DefineOption("logpath", ref logPath, "Use to dump Native Compilation Logs to a file."); + syntax.DefineOption("h|help", ref help, "Help for compile native."); + syntax.DefineParameter("INPUT_ASSEMBLY", ref inputAssembly, "The managed input assembly to compile to native."); + helpText = syntax.GetHelpText(); + if (string.IsNullOrWhiteSpace(inputAssembly)) { syntax.ReportError("Input Assembly is a required parameter."); + help = true; } if (!string.IsNullOrEmpty(configuration)) @@ -65,6 +76,7 @@ namespace Microsoft.DotNet.Tools.Compiler.Native catch (ArgumentException) { syntax.ReportError($"Invalid Configuration Option: {configuration}"); + help = true; } } @@ -77,13 +89,27 @@ namespace Microsoft.DotNet.Tools.Compiler.Native catch (ArgumentException) { syntax.ReportError($"Invalid Mode Option: {mode}"); + help = true; } } }); } - catch (ArgumentSyntaxException) + catch (ArgumentSyntaxException exception) { - //return ExitFailed; + Console.Error.WriteLine(exception.Message); + help = true; + returnCode = 1; + } + + if (help) + { + Console.WriteLine(helpText); + + return new ArgValues + { + IsHelp = help, + ReturnCode = returnCode + }; } Console.WriteLine($"Input Assembly: {inputAssembly}"); diff --git a/src/Microsoft.DotNet.Tools.Compiler.Native/Program.cs b/src/Microsoft.DotNet.Tools.Compiler.Native/Program.cs index 491ac6c92..33fb7daa8 100644 --- a/src/Microsoft.DotNet.Tools.Compiler.Native/Program.cs +++ b/src/Microsoft.DotNet.Tools.Compiler.Native/Program.cs @@ -32,6 +32,9 @@ namespace Microsoft.DotNet.Tools.Compiler.Native try { var cmdLineArgs = ArgumentsParser.Parse(args); + + if (cmdLineArgs.IsHelp) return cmdLineArgs.ReturnCode; + var config = cmdLineArgs.GetNativeCompileSettings(); DirectoryExtensions.CleanOrCreateDirectory(config.OutputDirectory);