From 67b4012e4509a4af3e3ccfedd2dd5656d19b65f6 Mon Sep 17 00:00:00 2001 From: Austin Wise Date: Thu, 4 Feb 2016 12:41:50 -0800 Subject: [PATCH] Move the help command it it's own class. Also fix the help command to work with intrinsic commands. --- src/dotnet/Program.cs | 70 ++----------------- .../commands/dotnet-help/HelpCommand.cs | 65 +++++++++++++++++ test/EndToEnd/EndToEndTest.cs | 14 ++++ .../Commands/HelpCommand.cs | 21 ++++++ 4 files changed, 107 insertions(+), 63 deletions(-) create mode 100644 src/dotnet/commands/dotnet-help/HelpCommand.cs create mode 100644 test/Microsoft.DotNet.Tools.Tests.Utilities/Commands/HelpCommand.cs diff --git a/src/dotnet/Program.cs b/src/dotnet/Program.cs index e2de0504c..9adc2bfca 100644 --- a/src/dotnet/Program.cs +++ b/src/dotnet/Program.cs @@ -4,7 +4,6 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Reflection; using Microsoft.DotNet.Cli.Utils; using Microsoft.DotNet.ProjectModel.Server; using Microsoft.DotNet.Tools.Build; @@ -12,6 +11,7 @@ using Microsoft.DotNet.Tools.Compiler; using Microsoft.DotNet.Tools.Compiler.Csc; using Microsoft.DotNet.Tools.Compiler.Fsc; using Microsoft.DotNet.Tools.Compiler.Native; +using Microsoft.DotNet.Tools.Help; using Microsoft.DotNet.Tools.New; using Microsoft.DotNet.Tools.Publish; using Microsoft.Extensions.PlatformAbstractions; @@ -26,32 +26,6 @@ namespace Microsoft.DotNet.Cli { public class Program { - private const string ProductLongName = ".NET Command Line Tools"; - private const string UsageText = @"Usage: dotnet [common-options] [command] [arguments] - -Arguments: - [command] The command to execute - [arguments] Arguments to pass to the command - -Common Options (passed before the command): - -v|--verbose Enable verbose output - --version Display .NET CLI Version Info - -Common Commands: - new Initialize a basic .NET project - restore Restore dependencies specified in the .NET project - build Builds a .NET project - publish Publishes a .NET project for deployment (including the runtime) - run Compiles and immediately executes a .NET project - repl Launch an interactive session (read, eval, print, loop) - pack Creates a NuGet package"; - private static readonly string ProductVersion = GetProductVersion(); - - private static string GetProductVersion() - { - var attr = typeof(Program).GetTypeInfo().Assembly.GetCustomAttribute(); - return attr?.InformationalVersion; - } public static int Main(string[] args) { @@ -91,7 +65,7 @@ Common Commands: } else if (IsArg(args[lastArg], "h", "help")) { - PrintHelp(); + HelpCommand.PrintHelp(); return 0; } else if (args[lastArg].StartsWith("-")) @@ -108,7 +82,7 @@ Common Commands: } if (!success) { - PrintHelp(); + HelpCommand.PrintHelp(); return 1; } @@ -119,9 +93,9 @@ Common Commands: Environment.SetEnvironmentVariable(CommandContext.Variables.Verbose, verbose.ToString()); } - if (string.IsNullOrEmpty(command) || command.Equals("help", StringComparison.OrdinalIgnoreCase)) + if (string.IsNullOrEmpty(command)) { - return RunHelpCommand(appArgs); + command = "help"; } var builtIns = new Dictionary> @@ -131,6 +105,7 @@ Common Commands: ["compile-csc"] = CompileCscCommand.Run, ["compile-fsc"] = CompileFscCommand.Run, ["compile-native"] = CompileNativeCommand.Run, + ["help"] = HelpCommand.Run, ["new"] = NewCommand.Run, ["pack"] = PackCommand.Run, ["projectmodel-server"] = ProjectModelServerCommand.Run, @@ -155,40 +130,9 @@ Common Commands: .ExitCode; } - private static int RunHelpCommand(IEnumerable appArgs) - { - if (appArgs.Any()) - { - return Command.Create("dotnet-" + appArgs.First(), new string[] { "--help" }) - .ForwardStdErr() - .ForwardStdOut() - .Execute() - .ExitCode; - } - else - { - PrintHelp(); - return 0; - } - } - - private static void PrintHelp() - { - PrintVersionHeader(); - Reporter.Output.WriteLine(UsageText); - } - - private static void PrintVersionHeader() - { - var versionString = string.IsNullOrEmpty(ProductVersion) ? - string.Empty : - $" ({ProductVersion})"; - Reporter.Output.WriteLine(ProductLongName + versionString); - } - private static void PrintVersionInfo() { - PrintVersionHeader(); + HelpCommand.PrintVersionHeader(); var runtimeEnvironment = PlatformServices.Default.Runtime; Reporter.Output.WriteLine("Runtime Environment:"); diff --git a/src/dotnet/commands/dotnet-help/HelpCommand.cs b/src/dotnet/commands/dotnet-help/HelpCommand.cs new file mode 100644 index 000000000..3440a73bb --- /dev/null +++ b/src/dotnet/commands/dotnet-help/HelpCommand.cs @@ -0,0 +1,65 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using System.Reflection; +using Microsoft.DotNet.Cli.Utils; + +namespace Microsoft.DotNet.Tools.Help +{ + public class HelpCommand + { + private const string ProductLongName = ".NET Command Line Tools"; + private const string UsageText = @"Usage: dotnet [common-options] [command] [arguments] + +Arguments: + [command] The command to execute + [arguments] Arguments to pass to the command + +Common Options (passed before the command): + -v|--verbose Enable verbose output + --version Display .NET CLI Version Info + +Common Commands: + new Initialize a basic .NET project + restore Restore dependencies specified in the .NET project + build Builds a .NET project + publish Publishes a .NET project for deployment (including the runtime) + run Compiles and immediately executes a .NET project + repl Launch an interactive session (read, eval, print, loop) + pack Creates a NuGet package"; + private static readonly string ProductVersion = GetProductVersion(); + + private static string GetProductVersion() + { + var attr = typeof(HelpCommand).GetTypeInfo().Assembly.GetCustomAttribute(); + return attr?.InformationalVersion; + } + + public static int Run(string[] args) + { + if (args.Length == 0) + { + PrintHelp(); + return 0; + } + else + { + return Cli.Program.Main(new[] { args[0], "--help" }); + } + } + + public static void PrintHelp() + { + PrintVersionHeader(); + Reporter.Output.WriteLine(UsageText); + } + + public static void PrintVersionHeader() + { + var versionString = string.IsNullOrEmpty(ProductVersion) ? + string.Empty : + $" ({ProductVersion})"; + Reporter.Output.WriteLine(ProductLongName + versionString); + } + } +} diff --git a/test/EndToEnd/EndToEndTest.cs b/test/EndToEnd/EndToEndTest.cs index 5dba2ca02..d01fdde8b 100644 --- a/test/EndToEnd/EndToEndTest.cs +++ b/test/EndToEnd/EndToEndTest.cs @@ -169,6 +169,20 @@ namespace Microsoft.DotNet.Tests.EndToEnd TestExecutable(OutputDirectory, publishCommand.GetOutputExecutable(), s_expectedOutput); } + [Fact] + public void TestDotnetHelp() + { + var helpCommand = new HelpCommand(); + helpCommand.Execute().Should().Pass(); + } + + [Fact] + public void TestDotnetHelpBuild() + { + var helpCommand = new HelpCommand(); + helpCommand.Execute("build").Should().Pass(); + } + private void TestInstanceSetup() { var root = Temp.CreateDirectory(); diff --git a/test/Microsoft.DotNet.Tools.Tests.Utilities/Commands/HelpCommand.cs b/test/Microsoft.DotNet.Tools.Tests.Utilities/Commands/HelpCommand.cs new file mode 100644 index 000000000..d7d78a7d4 --- /dev/null +++ b/test/Microsoft.DotNet.Tools.Tests.Utilities/Commands/HelpCommand.cs @@ -0,0 +1,21 @@ +// Copyright (c) .NET Foundation and contributors. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. + +using Microsoft.DotNet.Cli.Utils; + +namespace Microsoft.DotNet.Tools.Test.Utilities +{ + public sealed class HelpCommand : TestCommand + { + public HelpCommand() + : base("dotnet") + { + } + + public override CommandResult Execute(string args = "") + { + args = $"help {args}"; + return base.Execute(args); + } + } +}