Merge pull request #1231 from AustinWise/fixHelp

Fix the help command to work with intrinsic commands.
This commit is contained in:
David Fowler 2016-02-15 23:50:03 -08:00
commit 1e18150194
4 changed files with 107 additions and 63 deletions

View file

@ -4,7 +4,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Reflection;
using Microsoft.DotNet.Cli.Utils; using Microsoft.DotNet.Cli.Utils;
using Microsoft.DotNet.ProjectModel.Server; using Microsoft.DotNet.ProjectModel.Server;
using Microsoft.DotNet.Tools.Build; 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.Csc;
using Microsoft.DotNet.Tools.Compiler.Fsc; using Microsoft.DotNet.Tools.Compiler.Fsc;
using Microsoft.DotNet.Tools.Compiler.Native; using Microsoft.DotNet.Tools.Compiler.Native;
using Microsoft.DotNet.Tools.Help;
using Microsoft.DotNet.Tools.New; using Microsoft.DotNet.Tools.New;
using Microsoft.DotNet.Tools.Publish; using Microsoft.DotNet.Tools.Publish;
using Microsoft.Extensions.PlatformAbstractions; using Microsoft.Extensions.PlatformAbstractions;
@ -26,32 +26,6 @@ namespace Microsoft.DotNet.Cli
{ {
public class Program 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<AssemblyInformationalVersionAttribute>();
return attr?.InformationalVersion;
}
public static int Main(string[] args) public static int Main(string[] args)
{ {
@ -91,7 +65,7 @@ Common Commands:
} }
else if (IsArg(args[lastArg], "h", "help")) else if (IsArg(args[lastArg], "h", "help"))
{ {
PrintHelp(); HelpCommand.PrintHelp();
return 0; return 0;
} }
else if (args[lastArg].StartsWith("-")) else if (args[lastArg].StartsWith("-"))
@ -108,7 +82,7 @@ Common Commands:
} }
if (!success) if (!success)
{ {
PrintHelp(); HelpCommand.PrintHelp();
return 1; return 1;
} }
@ -119,9 +93,9 @@ Common Commands:
Environment.SetEnvironmentVariable(CommandContext.Variables.Verbose, verbose.ToString()); 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<string, Func<string[], int>> var builtIns = new Dictionary<string, Func<string[], int>>
@ -131,6 +105,7 @@ Common Commands:
["compile-csc"] = CompileCscCommand.Run, ["compile-csc"] = CompileCscCommand.Run,
["compile-fsc"] = CompileFscCommand.Run, ["compile-fsc"] = CompileFscCommand.Run,
["compile-native"] = CompileNativeCommand.Run, ["compile-native"] = CompileNativeCommand.Run,
["help"] = HelpCommand.Run,
["new"] = NewCommand.Run, ["new"] = NewCommand.Run,
["pack"] = PackCommand.Run, ["pack"] = PackCommand.Run,
["projectmodel-server"] = ProjectModelServerCommand.Run, ["projectmodel-server"] = ProjectModelServerCommand.Run,
@ -155,40 +130,9 @@ Common Commands:
.ExitCode; .ExitCode;
} }
private static int RunHelpCommand(IEnumerable<string> 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() private static void PrintVersionInfo()
{ {
PrintVersionHeader(); HelpCommand.PrintVersionHeader();
var runtimeEnvironment = PlatformServices.Default.Runtime; var runtimeEnvironment = PlatformServices.Default.Runtime;
Reporter.Output.WriteLine("Runtime Environment:"); Reporter.Output.WriteLine("Runtime Environment:");

View file

@ -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<AssemblyInformationalVersionAttribute>();
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);
}
}
}

View file

@ -169,6 +169,20 @@ namespace Microsoft.DotNet.Tests.EndToEnd
TestExecutable(OutputDirectory, publishCommand.GetOutputExecutable(), s_expectedOutput); 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() private void TestInstanceSetup()
{ {
var root = Temp.CreateDirectory(); var root = Temp.CreateDirectory();

View file

@ -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);
}
}
}