Get dotnet-sos syntax up to spec.
authorMike McLaughlin <mikem@microsoft.com>
Sat, 23 Feb 2019 20:23:38 +0000 (12:23 -0800)
committerMike McLaughlin <mikem@microsoft.com>
Fri, 1 Mar 2019 18:51:52 +0000 (10:51 -0800)
src/Tools/dotnet-sos/Program.cs
src/Tools/dotnet-sos/dotnet-sos.csproj

index ddbb64f282eebbd6456834b89fe3aba60f53a633..27dbd50c5bbd600367f5f9bb9602d4022a8f40fd 100644 (file)
@@ -1,64 +1,58 @@
 // Copyright (c) .NET Foundation. All rights reserved.
 // Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
 
-using McMaster.Extensions.CommandLineUtils;
 using SOS;
 using System;
+using System.CommandLine;
+using System.CommandLine.Builder;
+using System.CommandLine.Invocation;
 using System.Threading.Tasks;
 
 namespace Microsoft.Diagnostics.Tools.SOS
 {
-    [Command(Name = "dotnet-sos", Description = "Install and configure SOS")]
-    internal class Program
+    public class Program
     {
-        [Option("--install", Description = "Install and configure SOS.")]
-        public bool InstallSOS { get; set; }
+        public static Task<int> Main(string[] args)
+        {
+            var parser = new CommandLineBuilder()
+                .AddCommand(InstallCommand())
+                .AddCommand(UninstallCommand())
+                .UseDefaults()
+                .Build();
 
-        [Option("--uninstall", Description = "Uninstall SOS.")]
-        public bool UninstallSOS { get; set; }
+            return parser.InvokeAsync(args);
+        }
 
-        [Option("--source", Description = "SOS binaries source path.")]
-        public string SOSSourcePath { get; set; }
+        private static Command InstallCommand() =>
+            new Command(
+                "install", 
+                "Installs SOS and configures LLDB to load it on startup.", 
+                handler: CommandHandler.Create<IConsole>((console) => InvokeAsync(console, install: true)));
 
-        public int OnExecute(IConsole console, CommandLineApplication app)
-        {
-            if (InstallSOS || UninstallSOS)
-            {
-                try
-                {
-                    var sosInstaller = new InstallHelper((message) => console.WriteLine(message));
-                    if (SOSSourcePath != null)
-                    {
-                        sosInstaller.SOSSourcePath = SOSSourcePath;
-                    }
-                    if (UninstallSOS)
-                    {
-                        sosInstaller.Uninstall();
-                    }
-                    else 
-                    {
-                        sosInstaller.Install();
-                    }
-                }
-                catch (SOSInstallerException ex)
-                {
-                    console.Error.WriteLine(ex.Message);
-                    return 1;
-                }
-            }
-            return 0;
-        }
+        private static Command UninstallCommand() =>
+            new Command(
+                "uninstall",
+                "Uninstalls SOS and reverts any configuration changes to LLDB.",
+                handler: CommandHandler.Create<IConsole>((console) => InvokeAsync(console, install: false)));
 
-        private static int Main(string[] args)
+        private static Task<int> InvokeAsync(IConsole console, bool install)
         {
             try
             {
-                return CommandLineApplication.Execute<Program>(args);
+                var sosInstaller = new InstallHelper((message) => console.Out.WriteLine(message));
+                if (install) {
+                    sosInstaller.Install();
+                }
+                else {
+                    sosInstaller.Uninstall();
+                }
             }
-            catch (OperationCanceledException)
+            catch (SOSInstallerException ex)
             {
-                return 0;
+                console.Error.WriteLine(ex.Message);
+                return Task.FromResult(1);
             }
+            return Task.FromResult(0);
         }
     }
 }
index cb4238a60d49f109edad18b764590aa2db8cc757..3a0ed60bff28149fb8d163570bc255401ed49a06 100644 (file)
   </PropertyGroup>
 
   <ItemGroup>
-    <PackageReference Include="McMaster.Extensions.CommandLineUtils" Version="2.2.5" />
+    <ProjectReference Include="..\..\SOS\SOS.InstallHelper\SOS.InstallHelper.csproj" />
   </ItemGroup>
-
+  
   <ItemGroup>
-    <ProjectReference Include="..\..\SOS\SOS.InstallHelper\SOS.InstallHelper.csproj" />
+    <PackageReference Include="System.CommandLine.Experimental" Version="0.1.0-alpha-63807-01" />
   </ItemGroup>
-
+  
   <ItemGroup>
     <_PackageFiles Include="$(SOSNETCoreBinaries)">
       <BuildAction>None</BuildAction>