Make a different option to change the default format for convert command (#455)
authorSung Yoon Whang <suwhang@microsoft.com>
Thu, 5 Sep 2019 23:12:33 +0000 (16:12 -0700)
committerGitHub <noreply@github.com>
Thu, 5 Sep 2019 23:12:33 +0000 (16:12 -0700)
* 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:

src/Tools/dotnet-trace/CommandLine/Commands/ConvertCommand.cs
src/Tools/dotnet-trace/CommandLine/Options/CommonOptions.cs

index e7e36384fc9c082ce421d05bc9c1f03a8e95e4f7..cddfb890899956de0d1f5bb3452a18263e3cf8f6 100644 (file)
@@ -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<IConsole, FileInfo, TraceFileFormat, FileInfo>(ConvertFile),
index 8ef89a42ea3eb9cfea3440730f7aa3404ff38d94..0584883938b058e836919e1c5bce362935c88e84 100644 (file)
@@ -25,5 +25,12 @@ namespace Microsoft.Diagnostics.Tools.Trace
                 description: $"Sets the output format for the trace file.  Default is {DefaultTraceFileFormat}",
                 argument: new Argument<TraceFileFormat>(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<TraceFileFormat> { Name = "trace-file-format" },
+                isHidden: false);
     }
 }