diff --git a/src/dotnet/Parser.cs b/src/dotnet/Parser.cs index 4b53bfa45..0c208cfa5 100644 --- a/src/dotnet/Parser.cs +++ b/src/dotnet/Parser.cs @@ -27,6 +27,7 @@ namespace Microsoft.DotNet.Cli RemoveCommandParser.Remove(), ListCommandParser.List(), NuGetCommandParser.NuGet(), + CacheCommandParser.Cache(), Create.Command("msbuild", ""), Create.Command("vstest", ""), CompleteCommandParser.Complete(), diff --git a/src/dotnet/commands/dotnet-cache/CacheCommandParser.cs b/src/dotnet/commands/dotnet-cache/CacheCommandParser.cs new file mode 100644 index 000000000..580f0fb48 --- /dev/null +++ b/src/dotnet/commands/dotnet-cache/CacheCommandParser.cs @@ -0,0 +1,57 @@ +// 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.Cache; + +namespace Microsoft.DotNet.Cli +{ + internal static class CacheCommandParser + { + public static Command Cache() => + Create.Command( + LocalizableStrings.AppFullName, + LocalizableStrings.AppDescription, + Accept.ZeroOrMoreArguments, + CommonOptions.HelpOption(), + Create.Option( + "-e|--entries", + LocalizableStrings.ProjectEntryDescription, + Accept.ExactlyOneArgument + .With(name: LocalizableStrings.ProjectEntries) + .Forward()), + CommonOptions.FrameworkOption(), + Create.Option( + "--framework-version", + LocalizableStrings.FrameworkVersionOptionDescription, + Accept.ExactlyOneArgument + .With(name: LocalizableStrings.FrameworkVersionOption) + .ForwardAs(o => $"/p:FX_Version={o.Arguments.Single()}")), + CommonOptions.RuntimeOption(), + CommonOptions.ConfigurationOption(), + Create.Option( + "-o|--output", + LocalizableStrings.OutputOptionDescription, + Accept.ExactlyOneArgument + .With(name: LocalizableStrings.OutputOption) + .ForwardAs(o => $"/p:ComposeDir={o.Arguments.Single()}")), + Create.Option( + "-w |--working-dir", + LocalizableStrings.IntermediateWorkingDirOptionDescription, + Accept.ExactlyOneArgument + .With(name: LocalizableStrings.IntermediateWorkingDirOption) + .ForwardAs(o => $"/p:ComposeWorkingDir={o.Arguments.Single()}")), + Create.Option( + "--preserve-working-dir", + LocalizableStrings.PreserveIntermediateWorkingDirOptionDescription, + Accept.NoArguments + .ForwardAs(o => $"/p:PreserveComposeWorkingDir=true")), + Create.Option( + "--skip-optimization", + LocalizableStrings.SkipOptimizationOptionDescription, + Accept.NoArguments + .ForwardAs("/p:SkipOptimization=true")), + CommonOptions.VerbosityOption()); + } +} \ No newline at end of file diff --git a/src/dotnet/commands/dotnet-cache/LocalizableStrings.cs b/src/dotnet/commands/dotnet-cache/LocalizableStrings.cs index 677ae5618..9596af025 100644 --- a/src/dotnet/commands/dotnet-cache/LocalizableStrings.cs +++ b/src/dotnet/commands/dotnet-cache/LocalizableStrings.cs @@ -9,7 +9,7 @@ namespace Microsoft.DotNet.Tools.Cache public const string AppDescription = "Caches the specified assemblies for the .NET Platform. By default, these will be optimized for the target runtime and framework."; - public const string ProjectEntries = "ProjectEntries"; + public const string ProjectEntries = "PROJECT_ENTRIES"; public const string ProjectEntryDescription = "The XML file that contains the list of packages to be cached."; diff --git a/src/dotnet/commands/dotnet-cache/Program.cs b/src/dotnet/commands/dotnet-cache/Program.cs index 2e995ea42..d4325ab1a 100644 --- a/src/dotnet/commands/dotnet-cache/Program.cs +++ b/src/dotnet/commands/dotnet-cache/Program.cs @@ -23,113 +23,31 @@ namespace Microsoft.DotNet.Tools.Cache { DebugHelper.HandleDebugSwitch(ref args); - var app = new CommandLineApplication(throwOnUnexpectedArg: false); - app.Name = "dotnet cache"; - app.FullName = LocalizableStrings.AppFullName; - app.Description = LocalizableStrings.AppDescription; - app.AllowArgumentSeparator = true; - app.ArgumentSeparatorHelpText = HelpMessageStrings.MSBuildAdditionalArgsHelpText; - app.HelpOption("-h|--help"); + var msbuildArgs = new List(); - CommandOption projectArgument = app.Option( - $"-e|--entries <{LocalizableStrings.ProjectEntries}>", LocalizableStrings.ProjectEntryDescription, - CommandOptionType.SingleValue); + var parser = Parser.Instance; - CommandOption frameworkOption = app.Option( - $"-f|--framework <{LocalizableStrings.FrameworkOption}>", LocalizableStrings.FrameworkOptionDescription, - CommandOptionType.SingleValue); + var result = parser.ParseFrom("dotnet cache", args); - CommandOption runtimeOption = app.Option( - $"-r|--runtime <{LocalizableStrings.RuntimeOption}>", LocalizableStrings.RuntimeOptionDescription, - CommandOptionType.SingleValue); + Reporter.Output.WriteLine(result.Diagram()); - CommandOption outputOption = app.Option( - $"-o|--output <{LocalizableStrings.OutputOption}>", LocalizableStrings.OutputOptionDescription, - CommandOptionType.SingleValue); + result.ShowHelpIfRequested(); - CommandOption fxOption = app.Option( - $"--framework-version <{LocalizableStrings.FrameworkVersionOption}>", LocalizableStrings.FrameworkVersionOptionDescription, - CommandOptionType.SingleValue); + var appliedBuildOptions = result["dotnet"]["cache"]; - CommandOption skipOptimizationOption = app.Option( - $"--skip-optimization", LocalizableStrings.SkipOptimizationOptionDescription, - CommandOptionType.NoValue); - - CommandOption workingDir = app.Option( - $"-w |--working-dir <{LocalizableStrings.IntermediateWorkingDirOption}>", LocalizableStrings.IntermediateWorkingDirOptionDescription, - CommandOptionType.SingleValue); - - CommandOption preserveWorkingDir = app.Option( - $"--preserve-working-dir", LocalizableStrings.PreserveIntermediateWorkingDirOptionDescription, - CommandOptionType.NoValue); - - CommandOption verbosityOption = MSBuildForwardingApp.AddVerbosityOption(app); - - List msbuildArgs = null; - app.OnExecute(() => + if (!result.HasOption("-e")) { - msbuildArgs = new List(); - - if (string.IsNullOrEmpty(projectArgument.Value())) - { - throw new InvalidOperationException(LocalizableStrings.SpecifyEntries); - } - - msbuildArgs.Add("/t:ComposeCache"); - msbuildArgs.Add(projectArgument.Value()); - - if (!string.IsNullOrEmpty(frameworkOption.Value())) - { - msbuildArgs.Add($"/p:TargetFramework={frameworkOption.Value()}"); - } - - if (!string.IsNullOrEmpty(runtimeOption.Value())) - { - msbuildArgs.Add($"/p:RuntimeIdentifier={runtimeOption.Value()}"); - } - - if (!string.IsNullOrEmpty(outputOption.Value())) - { - var outputPath = Path.GetFullPath(outputOption.Value()); - msbuildArgs.Add($"/p:ComposeDir={outputPath}"); - } - - if (!string.IsNullOrEmpty(fxOption.Value())) - { - msbuildArgs.Add($"/p:FX_Version={fxOption.Value()}"); - } - - if (!string.IsNullOrEmpty(workingDir.Value())) - { - msbuildArgs.Add($"/p:ComposeWorkingDir={workingDir.Value()}"); - } - - if (skipOptimizationOption.HasValue()) - { - msbuildArgs.Add($"/p:SkipOptimization={skipOptimizationOption.HasValue()}"); - } - - if (preserveWorkingDir.HasValue()) - { - msbuildArgs.Add($"/p:PreserveComposeWorkingDir={preserveWorkingDir.HasValue()}"); - } - - if (!string.IsNullOrEmpty(verbosityOption.Value())) - { - msbuildArgs.Add($"/verbosity:{verbosityOption.Value()}"); - } - - msbuildArgs.AddRange(app.RemainingArguments); - - return 0; - }); - - int exitCode = app.Execute(args); - if (msbuildArgs == null) - { - throw new CommandCreationException(exitCode); + throw new InvalidOperationException(LocalizableStrings.SpecifyEntries); } + var msbuildArgs = msbuildArgs = new List(); + + msbuildArgs.Add("/t:ComposeCache"); + + msbuildArgs.AddRange(appliedBuildOptions.OptionValuesToBeForwarded()); + + msbuildArgs.AddRange(appliedBuildOptions.Arguments); + return new CacheCommand(msbuildArgs, msbuildPath); }