From 4d89a471a2df3116c3541ed36b8b7d99dfbc13df Mon Sep 17 00:00:00 2001 From: Sung Yoon Whang Date: Thu, 5 Sep 2019 16:12:33 -0700 Subject: [PATCH] Make a different option to change the default format for convert command (#455) * Make a different option to change the default format for convert command * Make --format a required option for convert command, Write on console instead of throwing an ArgumentException: --- .../CommandLine/Commands/ConvertCommand.cs | 37 ++++++++++++------- .../CommandLine/Options/CommonOptions.cs | 7 ++++ 2 files changed, 31 insertions(+), 13 deletions(-) diff --git a/src/Tools/dotnet-trace/CommandLine/Commands/ConvertCommand.cs b/src/Tools/dotnet-trace/CommandLine/Commands/ConvertCommand.cs index e7e36384f..cddfb8908 100644 --- a/src/Tools/dotnet-trace/CommandLine/Commands/ConvertCommand.cs +++ b/src/Tools/dotnet-trace/CommandLine/Commands/ConvertCommand.cs @@ -17,18 +17,29 @@ namespace Microsoft.Diagnostics.Tools.Trace { public static int ConvertFile(IConsole console, FileInfo inputFilename, TraceFileFormat format, FileInfo output) { - if (format == TraceFileFormat.NetTrace) - throw new ArgumentException("Cannot convert to nettrace format."); - - if (!inputFilename.Exists) - throw new FileNotFoundException($"File '{inputFilename}' does not exist."); - - if (output == null) - output = inputFilename; - - TraceFileFormatConverter.ConvertToFormat(format, inputFilename.FullName, output.FullName); - - return 0; + if ((int)format <= 0) + { + Console.Error.WriteLine("--format is required."); + return ErrorCodes.ArgumentError; + } + + if (format == TraceFileFormat.NetTrace) + { + Console.Error.WriteLine("Cannot convert a nettrace file to nettrace format."); + return ErrorCodes.ArgumentError; + } + + if (!inputFilename.Exists) + { + Console.Error.WriteLine($"File '{inputFilename}' does not exist."); + return ErrorCodes.ArgumentError; + } + + if (output == null) + output = inputFilename; + + TraceFileFormatConverter.ConvertToFormat(format, inputFilename.FullName, output.FullName); + return 0; } public static Command ConvertCommand() => @@ -40,7 +51,7 @@ namespace Microsoft.Diagnostics.Tools.Trace Description = $"Input trace file to be converted. Defaults to '{CollectCommandHandler.DefaultTraceName}'." }).ExistingOnly(), symbols: new Option[] { - CommonOptions.FormatOption(), + CommonOptions.ConvertFormatOption(), OutputOption() }, handler: System.CommandLine.Invocation.CommandHandler.Create(ConvertFile), diff --git a/src/Tools/dotnet-trace/CommandLine/Options/CommonOptions.cs b/src/Tools/dotnet-trace/CommandLine/Options/CommonOptions.cs index 8ef89a42e..058488393 100644 --- a/src/Tools/dotnet-trace/CommandLine/Options/CommonOptions.cs +++ b/src/Tools/dotnet-trace/CommandLine/Options/CommonOptions.cs @@ -25,5 +25,12 @@ namespace Microsoft.Diagnostics.Tools.Trace description: $"Sets the output format for the trace file. Default is {DefaultTraceFileFormat}", argument: new Argument(defaultValue: DefaultTraceFileFormat) { Name = "trace-file-format" }, isHidden: false); + + public static Option ConvertFormatOption() => + new Option( + aliases: new[] { "--format" }, + description: $"Sets the output format for the trace file conversion.", + argument: new Argument { Name = "trace-file-format" }, + isHidden: false); } } -- 2.34.1