From 071f4dc697b5c090ed32ec924232271c14dcb26d Mon Sep 17 00:00:00 2001 From: Piotr Puszkiewicz Date: Fri, 10 Mar 2017 09:08:01 -0800 Subject: [PATCH] Convert Run --- .../commands/dotnet-build/BuildCommand.cs | 1 + src/dotnet/commands/dotnet-run/Program.cs | 55 +++++++++---------- .../commands/dotnet-run/RunCommandParser.cs | 37 ++++++++----- 3 files changed, 50 insertions(+), 43 deletions(-) diff --git a/src/dotnet/commands/dotnet-build/BuildCommand.cs b/src/dotnet/commands/dotnet-build/BuildCommand.cs index d2703cbcc..3ecd6eeba 100644 --- a/src/dotnet/commands/dotnet-build/BuildCommand.cs +++ b/src/dotnet/commands/dotnet-build/BuildCommand.cs @@ -54,6 +54,7 @@ namespace Microsoft.DotNet.Tools.Build DebugHelper.HandleDebugSwitch(ref args); BuildCommand cmd; + try { cmd = FromArgs(args); diff --git a/src/dotnet/commands/dotnet-run/Program.cs b/src/dotnet/commands/dotnet-run/Program.cs index f608695eb..a795b455b 100644 --- a/src/dotnet/commands/dotnet-run/Program.cs +++ b/src/dotnet/commands/dotnet-run/Program.cs @@ -4,47 +4,42 @@ using System; using Microsoft.DotNet.Cli.CommandLine; using Microsoft.DotNet.Cli.Utils; +using Microsoft.DotNet.Cli; +using Parser = Microsoft.DotNet.Cli.Parser; namespace Microsoft.DotNet.Tools.Run { public partial class RunCommand { + public static RunCommand FromArgs(string[] args, string msbuildPath = null) + { + var parser = Parser.Instance; + + var result = parser.ParseFrom("dotnet run", args); + + Reporter.Output.WriteLine(result.Diagram()); + + result.ShowHelpIfRequested(); + + return result["dotnet"]["run"].Value(); + } + public static int Run(string[] args) { DebugHelper.HandleDebugSwitch(ref args); - CommandLineApplication app = new CommandLineApplication(throwOnUnexpectedArg: false); - app.Name = "dotnet run"; - app.FullName = LocalizableStrings.AppFullName; - app.Description = LocalizableStrings.AppDescription; - app.HandleResponseFiles = true; - app.AllowArgumentSeparator = true; - app.ArgumentSeparatorHelpText = LocalizableStrings.RunCommandAdditionalArgsHelpText; - app.HelpOption("-h|--help"); - - CommandOption configuration = app.Option( - "-c|--configuration", LocalizableStrings.CommandOptionConfigurationDescription, - CommandOptionType.SingleValue); - CommandOption framework = app.Option( - $"-f|--framework <{LocalizableStrings.CommandOptionFramework}>", LocalizableStrings.CommandOptionFrameworkDescription, - CommandOptionType.SingleValue); - CommandOption project = app.Option( - "-p|--project", LocalizableStrings.CommandOptionProjectDescription, - CommandOptionType.SingleValue); - - app.OnExecute(() => + RunCommand cmd; + + try { - RunCommand runCmd = new RunCommand(); + cmd = FromArgs(args); + } + catch (CommandCreationException e) + { + return e.ExitCode; + } - runCmd.Configuration = configuration.Value(); - runCmd.Framework = framework.Value(); - runCmd.Project = project.Value(); - runCmd.Args = app.RemainingArguments; - - return runCmd.Start(); - }); - - return app.Execute(args); + return cmd.Start(); } } } diff --git a/src/dotnet/commands/dotnet-run/RunCommandParser.cs b/src/dotnet/commands/dotnet-run/RunCommandParser.cs index dba6ec68a..2569579ab 100644 --- a/src/dotnet/commands/dotnet-run/RunCommandParser.cs +++ b/src/dotnet/commands/dotnet-run/RunCommandParser.cs @@ -1,25 +1,36 @@ // 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.Collections.Generic; using Microsoft.DotNet.Cli.CommandLine; +using Microsoft.DotNet.Tools.Run; +using LocalizableStrings = Microsoft.DotNet.Tools.Run.LocalizableStrings; namespace Microsoft.DotNet.Cli { internal static class RunCommandParser { public static Command Run() => - Create.Command("run", - ".NET Run Command", - CommonOptions.HelpOption(), - Create.Option("-c|--configuration", - @"Configuration to use for building the project. Default for most projects is ""Debug"".", - Accept.ExactlyOneArgument - .WithSuggestionsFrom("DEBUG", "RELEASE")), - Create.Option("-f|--framework", - "Build and run the app using the specified framework. The framework has to be specified in the project file.", - Accept.AnyOneOf(Suggest.TargetFrameworksFromProjectFile)), - Create.Option("-p|--project", - "The path to the project file to run (defaults to the current directory if there is only one project).", - Accept.ZeroOrOneArgument)); + Create.Command( + "run", + ".NET Run Command", + Accept.ZeroOrMoreArguments + .MaterializeAs(o => + { + return new RunCommand() + { + Configuration = o.ValueOrDefault("--configuration"), + Framework = o.ValueOrDefault("--framework"), + Project = o.ValueOrDefault("--project"), + Args = (IReadOnlyList)o.Arguments + }; + }), + CommonOptions.HelpOption(), + CommonOptions.ConfigurationOption(), + CommonOptions.FrameworkOption(), + Create.Option( + "-p|--project", + LocalizableStrings.CommandOptionProjectDescription, + Accept.ExactlyOneArgument)); } } \ No newline at end of file