diff --git a/src/dotnet/AppliedOptionExtensions.cs b/src/dotnet/AppliedOptionExtensions.cs new file mode 100644 index 000000000..2aa2ff814 --- /dev/null +++ b/src/dotnet/AppliedOptionExtensions.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 System; +using System.Linq; +using Microsoft.DotNet.Cli.CommandLine; + +namespace Microsoft.DotNet.Cli +{ + public static class AppliedOptionExtensions + { + public static T ValueOrDefault(this AppliedOption parseResult, string alias) + { + return parseResult + .AppliedOptions + .Where(o => o.HasAlias(alias)) + .Select(o => o.Value()) + .SingleOrDefault(); + } + } +} \ No newline at end of file diff --git a/src/dotnet/commands/dotnet-migrate/MigrateCommandParser.cs b/src/dotnet/commands/dotnet-migrate/MigrateCommandParser.cs index 2f30281af..738a95110 100644 --- a/src/dotnet/commands/dotnet-migrate/MigrateCommandParser.cs +++ b/src/dotnet/commands/dotnet-migrate/MigrateCommandParser.cs @@ -1,29 +1,48 @@ // 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.Linq; using Microsoft.DotNet.Cli.CommandLine; +using Microsoft.DotNet.Tools.Migrate; +using LocalizableStrings = Microsoft.DotNet.Tools.Migrate.LocalizableStrings; namespace Microsoft.DotNet.Cli { internal static class MigrateCommandParser { public static Command Migrate() => - Create.Command("migrate", - ".NET Migrate Command", - CommonOptions.HelpOption(), - Create.Option("-t|--template-file", - "Base MSBuild template to use for migrated app. The default is the project included in dotnet new."), - Create.Option("-v|--sdk-package-version", - "The version of the SDK package that will be referenced in the migrated app. The default is the version of the SDK in dotnet new."), - Create.Option("-x|--xproj-file", - "The path to the xproj file to use. Required when there is more than one xproj in a project directory."), - Create.Option("-s|--skip-project-references", - "Skip migrating project references. By default, project references are migrated recursively."), - Create.Option("-r|--report-file", - "Output migration report to the given file in addition to the console."), - Create.Option("--format-report-file-json", - "Output migration report file as json rather than user messages."), - Create.Option("--skip-backup", - "Skip moving project.json, global.json, and *.xproj to a `backup` directory after successful migration.")); + Create.Command( + "migrate", + ".NET Migrate Command", + Accept.ZeroOrOneArgument + .MaterializeAs(o => + { + return new MigrateCommand( + o.ValueOrDefault("--template-file"), + o.Arguments.FirstOrDefault(), + o.ValueOrDefault("--sdk-package-version"), + o.ValueOrDefault("--xproj-file"), + o.ValueOrDefault("--report-file"), + o.ValueOrDefault("--skip-project-references"), + o.ValueOrDefault("--format-report-file-json"), + o.ValueOrDefault("--skip-backup")); + }) + .With(name: LocalizableStrings.CmdProjectArgument, + description: LocalizableStrings.CmdProjectArgumentDescription), + CommonOptions.HelpOption(), + Create.Option("-t|--template-file", + LocalizableStrings.CmdTemplateDescription), + Create.Option("-v|--sdk-package-version", + LocalizableStrings.CmdVersionDescription), + Create.Option("-x|--xproj-file", + LocalizableStrings.CmdXprojFileDescription), + Create.Option("-s|--skip-project-references", + LocalizableStrings.CmdSkipProjectReferencesDescription), + Create.Option("-r|--report-file", + LocalizableStrings.CmdReportFileDescription), + Create.Option("--format-report-file-json", + LocalizableStrings.CmdReportOutputDescription), + Create.Option("--skip-backup", + LocalizableStrings.CmdSkipBackupDescription)); } } \ No newline at end of file diff --git a/src/dotnet/commands/dotnet-migrate/Program.cs b/src/dotnet/commands/dotnet-migrate/Program.cs index 177fb920f..7c0fcb635 100644 --- a/src/dotnet/commands/dotnet-migrate/Program.cs +++ b/src/dotnet/commands/dotnet-migrate/Program.cs @@ -2,13 +2,32 @@ // Licensed under the MIT license. See LICENSE file in the project root for full license information. using System; +using System.Collections.Generic; +using Microsoft.DotNet.Cli; using Microsoft.DotNet.Cli.CommandLine; using Microsoft.DotNet.Cli.Utils; +using Parser = Microsoft.DotNet.Cli.Parser; namespace Microsoft.DotNet.Tools.Migrate { public partial class MigrateCommand { + public static MigrateCommand FromArgs(string[] args, string msbuildPath = null) + { + var msbuildArgs = new List(); + + var parser = Parser.Instance; + + var result = parser.ParseFrom("dotnet migrate", args); + + Reporter.Output.WriteLine(result.Diagram()); + + result.ShowHelpIfRequested(); + + return result["dotnet"]["migrate"].Value(); + } + + public static int Run(string[] args) { @@ -19,64 +38,19 @@ namespace Microsoft.DotNet.Tools.Migrate DebugHelper.HandleDebugSwitch(ref args); - CommandLineApplication app = new CommandLineApplication(); - app.Name = "dotnet migrate"; - app.FullName = LocalizableStrings.AppFullName; - app.Description = LocalizableStrings.AppDescription; - app.HandleResponseFiles = true; - app.HelpOption("-h|--help"); - - CommandArgument projectArgument = app.Argument( - $"<{LocalizableStrings.CmdProjectArgument}>", - LocalizableStrings.CmdProjectArgumentDescription); - - CommandOption template = app.Option( - "-t|--template-file", - LocalizableStrings.CmdTemplateDescription, - CommandOptionType.SingleValue); - CommandOption sdkVersion = app.Option( - "-v|--sdk-package-version", - LocalizableStrings.CmdVersionDescription, - CommandOptionType.SingleValue); - CommandOption xprojFile = app.Option( - "-x|--xproj-file", - LocalizableStrings.CmdXprojFileDescription, - CommandOptionType.SingleValue); - CommandOption skipProjectReferences = app.Option( - "-s|--skip-project-references", - LocalizableStrings.CmdSkipProjectReferencesDescription, - CommandOptionType.BoolValue); - - CommandOption reportFile = app.Option( - "-r|--report-file", - LocalizableStrings.CmdReportFileDescription, - CommandOptionType.SingleValue); - CommandOption structuredReportOutput = app.Option( - "--format-report-file-json", - LocalizableStrings.CmdReportOutputDescription, - CommandOptionType.BoolValue); - CommandOption skipBackup = app.Option("--skip-backup", - LocalizableStrings.CmdSkipBackupDescription, - CommandOptionType.BoolValue); - - app.OnExecute(() => + MigrateCommand cmd; + try { - MigrateCommand migrateCommand = new MigrateCommand( - template.Value(), - projectArgument.Value, - sdkVersion.Value(), - xprojFile.Value(), - reportFile.Value(), - skipProjectReferences.BoolValue.HasValue ? skipProjectReferences.BoolValue.Value : false, - structuredReportOutput.BoolValue.HasValue ? structuredReportOutput.BoolValue.Value : false, - skipBackup.BoolValue.HasValue ? skipBackup.BoolValue.Value : false); - - return migrateCommand.Execute(); - }); + cmd = FromArgs(args); + } + catch (CommandCreationException e) + { + return e.ExitCode; + } try { - return app.Execute(args); + return cmd.Execute(); } catch (GracefulException e) {