diff --git a/src/Cli/dotnet/CommandLineInfo.cs b/src/Cli/dotnet/CommandLineInfo.cs index 84f0b4ba1eff..497d677f57ae 100644 --- a/src/Cli/dotnet/CommandLineInfo.cs +++ b/src/Cli/dotnet/CommandLineInfo.cs @@ -24,7 +24,7 @@ public static void PrintInfo() Reporter.Output.WriteLine($"{LocalizableStrings.DotNetSdkInfoLabel}"); Reporter.Output.WriteLine($" Version: {Product.Version}"); Reporter.Output.WriteLine($" Commit: {commitSha}"); - Reporter.Output.WriteLine($" Workload version: {WorkloadCommandParser.GetWorkloadsVersion()}"); + Reporter.Output.WriteLine($" Workload version: {WorkloadCommandDefinition.GetWorkloadsVersion()}"); Reporter.Output.WriteLine($" MSBuild version: {MSBuildForwardingAppWithoutLogging.MSBuildVersion.ToString()}"); Reporter.Output.WriteLine(); Reporter.Output.WriteLine($"{LocalizableStrings.DotNetRuntimeInfoLabel}"); @@ -40,7 +40,7 @@ private static void PrintWorkloadsInfo() { Reporter.Output.WriteLine(); Reporter.Output.WriteLine($"{LocalizableStrings.DotnetWorkloadInfoLabel}"); - WorkloadCommandParser.ShowWorkloadsInfo(showVersion: false); + WorkloadCommandDefinition.ShowWorkloadsInfo(showVersion: false); } private static string GetDisplayRid(DotnetVersionFile versionFile) diff --git a/src/Cli/dotnet/Commands/Build/BuildCommand.cs b/src/Cli/dotnet/Commands/Build/BuildCommand.cs index 264abdad7d80..9b6dabdb020a 100644 --- a/src/Cli/dotnet/Commands/Build/BuildCommand.cs +++ b/src/Cli/dotnet/Commands/Build/BuildCommand.cs @@ -22,14 +22,14 @@ public static CommandBase FromParseResult(ParseResult parseResult, string? msbui parseResult.ShowHelpOrErrorIfAppropriate(); CommonOptions.ValidateSelfContainedOptions( - parseResult.HasOption(BuildCommandParser.SelfContainedOption), - parseResult.HasOption(BuildCommandParser.NoSelfContainedOption)); + parseResult.HasOption(BuildCommandDefinition.SelfContainedOption), + parseResult.HasOption(BuildCommandDefinition.NoSelfContainedOption)); - bool noRestore = parseResult.HasOption(BuildCommandParser.NoRestoreOption); + bool noRestore = parseResult.HasOption(BuildCommandDefinition.NoRestoreOption); return CommandFactory.CreateVirtualOrPhysicalCommand( BuildCommandParser.GetCommand(), - BuildCommandParser.SlnOrProjectOrFileArgument, + BuildCommandDefinition.SlnOrProjectOrFileArgument, (msbuildArgs, appFilePath) => new VirtualProjectBuildingCommand( entryPointFileFullPath: Path.GetFullPath(appFilePath), msbuildArgs: msbuildArgs @@ -43,7 +43,7 @@ public static CommandBase FromParseResult(ParseResult parseResult, string? msbui noRestore: noRestore, msbuildPath: msbuildPath ), - [CommonOptions.PropertiesOption, CommonOptions.RestorePropertiesOption, BuildCommandParser.TargetOption, BuildCommandParser.VerbosityOption, BuildCommandParser.NoLogoOption], + [CommonOptions.PropertiesOption, CommonOptions.RestorePropertiesOption, BuildCommandDefinition.TargetOption, BuildCommandDefinition.VerbosityOption, BuildCommandDefinition.NoLogoOption], parseResult, msbuildPath ); diff --git a/src/Cli/dotnet/Commands/Build/BuildCommandDefinition.cs b/src/Cli/dotnet/Commands/Build/BuildCommandDefinition.cs new file mode 100644 index 000000000000..e4c3b40b3628 --- /dev/null +++ b/src/Cli/dotnet/Commands/Build/BuildCommandDefinition.cs @@ -0,0 +1,95 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.CommandLine; +using Microsoft.DotNet.Cli.CommandLine; +using Microsoft.DotNet.Cli.Commands.Restore; +using Microsoft.DotNet.Cli.Extensions; + +namespace Microsoft.DotNet.Cli.Commands.Build; + +internal static class BuildCommandDefinition +{ + public static readonly string DocsLink = "https://aka.ms/dotnet-build"; + + public static readonly Argument SlnOrProjectOrFileArgument = new(CliStrings.SolutionOrProjectOrFileArgumentName) + { + Description = CliStrings.SolutionOrProjectOrFileArgumentDescription, + Arity = ArgumentArity.ZeroOrMore + }; + + public static readonly Option OutputOption = new Option("--output", "-o") + { + Description = CliCommandStrings.BuildOutputOptionDescription, + HelpName = CliCommandStrings.OutputOptionName + }.ForwardAsOutputPath("OutputPath"); + + public static readonly Option NoIncrementalOption = new Option("--no-incremental") + { + Description = CliCommandStrings.NoIncrementalOptionDescription, + Arity = ArgumentArity.Zero + }.ForwardAs("--target:Rebuild"); + + public static readonly Option NoDependenciesOption = new Option("--no-dependencies") + { + Description = CliCommandStrings.NoDependenciesOptionDescription, + Arity = ArgumentArity.Zero + }.ForwardAs("--property:BuildProjectReferences=false"); + + public static readonly Option NoLogoOption = CommonOptions.NoLogoOption(); + + public static readonly Option NoRestoreOption = CommonOptions.NoRestoreOption; + + public static readonly Option SelfContainedOption = CommonOptions.SelfContainedOption; + + public static readonly Option NoSelfContainedOption = CommonOptions.NoSelfContainedOption; + + public static readonly Option RuntimeOption = CommonOptions.RuntimeOption(CliCommandStrings.BuildRuntimeOptionDescription); + + public static readonly Option FrameworkOption = CommonOptions.FrameworkOption(CliCommandStrings.BuildFrameworkOptionDescription); + + public static readonly Option ConfigurationOption = CommonOptions.ConfigurationOption(CliCommandStrings.BuildConfigurationOptionDescription); + + /// + /// Build actually means 'run the default Target' generally in MSBuild + /// + public static readonly Option TargetOption = CommonOptions.MSBuildTargetOption(); + + public static readonly Option VerbosityOption = CommonOptions.VerbosityOption(); + + public static Command Create() + { + Command command = new("build", CliCommandStrings.BuildAppFullName) + { + DocsLink = DocsLink + }; + + command.Arguments.Add(SlnOrProjectOrFileArgument); + RestoreCommandDefinition.AddImplicitRestoreOptions(command, includeRuntimeOption: false, includeNoDependenciesOption: false); + command.Options.Add(FrameworkOption); + command.Options.Add(ConfigurationOption); + command.Options.Add(RuntimeOption); + command.Options.Add(CommonOptions.VersionSuffixOption); + command.Options.Add(NoRestoreOption); + command.Options.Add(CommonOptions.InteractiveMsBuildForwardOption); + command.Options.Add(VerbosityOption); + command.Options.Add(CommonOptions.DebugOption); + command.Options.Add(OutputOption); + command.Options.Add(CommonOptions.ArtifactsPathOption); + command.Options.Add(NoIncrementalOption); + command.Options.Add(NoDependenciesOption); + command.Options.Add(NoLogoOption); + command.Options.Add(SelfContainedOption); + command.Options.Add(NoSelfContainedOption); + command.Options.Add(CommonOptions.ArchitectureOption); + command.Options.Add(CommonOptions.OperatingSystemOption); + command.Options.Add(CommonOptions.DisableBuildServersOption); + command.Options.Add(TargetOption); + command.Options.Add(CommonOptions.GetPropertyOption); + command.Options.Add(CommonOptions.GetItemOption); + command.Options.Add(CommonOptions.GetTargetResultOption); + command.Options.Add(CommonOptions.GetResultOutputFileOption); + + return command; + } +} diff --git a/src/Cli/dotnet/Commands/Build/BuildCommandParser.cs b/src/Cli/dotnet/Commands/Build/BuildCommandParser.cs index be2e52c45263..7759cdf1684c 100644 --- a/src/Cli/dotnet/Commands/Build/BuildCommandParser.cs +++ b/src/Cli/dotnet/Commands/Build/BuildCommandParser.cs @@ -2,103 +2,21 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.CommandLine; -using Microsoft.DotNet.Cli.CommandLine; -using Microsoft.DotNet.Cli.Commands.Restore; -using Microsoft.DotNet.Cli.Extensions; namespace Microsoft.DotNet.Cli.Commands.Build; internal static class BuildCommandParser { - public static readonly string DocsLink = "https://aka.ms/dotnet-build"; - - public static readonly Argument SlnOrProjectOrFileArgument = new(CliStrings.SolutionOrProjectOrFileArgumentName) - { - Description = CliStrings.SolutionOrProjectOrFileArgumentDescription, - Arity = ArgumentArity.ZeroOrMore - }; - - public static readonly Option OutputOption = new Option("--output", "-o") - { - Description = CliCommandStrings.BuildOutputOptionDescription, - HelpName = CliCommandStrings.OutputOptionName - }.ForwardAsOutputPath("OutputPath"); - - public static readonly Option NoIncrementalOption = new Option("--no-incremental") - { - Description = CliCommandStrings.NoIncrementalOptionDescription, - Arity = ArgumentArity.Zero - }.ForwardAs("--target:Rebuild"); - - public static readonly Option NoDependenciesOption = new Option("--no-dependencies") - { - Description = CliCommandStrings.NoDependenciesOptionDescription, - Arity = ArgumentArity.Zero - }.ForwardAs("--property:BuildProjectReferences=false"); - - public static readonly Option NoLogoOption = CommonOptions.NoLogoOption(); - - public static readonly Option NoRestoreOption = CommonOptions.NoRestoreOption; - - public static readonly Option SelfContainedOption = CommonOptions.SelfContainedOption; - - public static readonly Option NoSelfContainedOption = CommonOptions.NoSelfContainedOption; - - public static readonly Option RuntimeOption = CommonOptions.RuntimeOption(CliCommandStrings.BuildRuntimeOptionDescription); - - public static readonly Option FrameworkOption = CommonOptions.FrameworkOption(CliCommandStrings.BuildFrameworkOptionDescription); - - public static readonly Option ConfigurationOption = CommonOptions.ConfigurationOption(CliCommandStrings.BuildConfigurationOptionDescription); - - /// - /// Build actually means 'run the default Target' generally in MSBuild - /// - public static readonly Option TargetOption = CommonOptions.MSBuildTargetOption(); - - public static readonly Option VerbosityOption = CommonOptions.VerbosityOption(); - - private static readonly Command Command = ConstructCommand(); + private static readonly Command Command = SetAction(BuildCommandDefinition.Create()); public static Command GetCommand() { return Command; } - private static Command ConstructCommand() + private static Command SetAction(Command command) { - Command command = new("build", CliCommandStrings.BuildAppFullName) - { - DocsLink = DocsLink - }; - - command.Arguments.Add(SlnOrProjectOrFileArgument); - RestoreCommandParser.AddImplicitRestoreOptions(command, includeRuntimeOption: false, includeNoDependenciesOption: false); - command.Options.Add(FrameworkOption); - command.Options.Add(ConfigurationOption); - command.Options.Add(RuntimeOption); - command.Options.Add(CommonOptions.VersionSuffixOption); - command.Options.Add(NoRestoreOption); - command.Options.Add(CommonOptions.InteractiveMsBuildForwardOption); - command.Options.Add(VerbosityOption); - command.Options.Add(CommonOptions.DebugOption); - command.Options.Add(OutputOption); - command.Options.Add(CommonOptions.ArtifactsPathOption); - command.Options.Add(NoIncrementalOption); - command.Options.Add(NoDependenciesOption); - command.Options.Add(NoLogoOption); - command.Options.Add(SelfContainedOption); - command.Options.Add(NoSelfContainedOption); - command.Options.Add(CommonOptions.ArchitectureOption); - command.Options.Add(CommonOptions.OperatingSystemOption); - command.Options.Add(CommonOptions.DisableBuildServersOption); - command.Options.Add(TargetOption); - command.Options.Add(CommonOptions.GetPropertyOption); - command.Options.Add(CommonOptions.GetItemOption); - command.Options.Add(CommonOptions.GetTargetResultOption); - command.Options.Add(CommonOptions.GetResultOutputFileOption); - command.SetAction(BuildCommand.Run); - return command; } } diff --git a/src/Cli/dotnet/Commands/BuildServer/BuildServerCommandDefinition.cs b/src/Cli/dotnet/Commands/BuildServer/BuildServerCommandDefinition.cs new file mode 100644 index 000000000000..9c7726f139bf --- /dev/null +++ b/src/Cli/dotnet/Commands/BuildServer/BuildServerCommandDefinition.cs @@ -0,0 +1,27 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.CommandLine; +using Microsoft.DotNet.Cli.Commands.BuildServer.Shutdown; +using Microsoft.DotNet.Cli.CommandLine; +using Microsoft.DotNet.Cli.Extensions; + +namespace Microsoft.DotNet.Cli.Commands.BuildServer; + +internal static class BuildServerCommandDefinition +{ + public const string Name = "build-server"; + public static readonly string DocsLink = "https://aka.ms/dotnet-build-server"; + + public static Command Create() + { + var command = new Command(Name, CliCommandStrings.BuildServerCommandDescription) + { + DocsLink = DocsLink + }; + + command.Subcommands.Add(BuildServerShutdownCommandDefinition.Create()); + + return command; + } +} diff --git a/src/Cli/dotnet/Commands/BuildServer/BuildServerCommandParser.cs b/src/Cli/dotnet/Commands/BuildServer/BuildServerCommandParser.cs index 468e60ad4fe1..201e5ff94274 100644 --- a/src/Cli/dotnet/Commands/BuildServer/BuildServerCommandParser.cs +++ b/src/Cli/dotnet/Commands/BuildServer/BuildServerCommandParser.cs @@ -1,37 +1,25 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#nullable disable - using System.CommandLine; using Microsoft.DotNet.Cli.Commands.BuildServer.Shutdown; -using Microsoft.DotNet.Cli.CommandLine; using Microsoft.DotNet.Cli.Extensions; namespace Microsoft.DotNet.Cli.Commands.BuildServer; internal static class BuildServerCommandParser { - public static readonly string DocsLink = "https://aka.ms/dotnet-build-server"; - - private static readonly Command Command = ConstructCommand(); + private static readonly Command Command = SetAction(BuildServerCommandDefinition.Create()); public static Command GetCommand() { return Command; } - private static Command ConstructCommand() + private static Command SetAction(Command command) { - var command = new Command("build-server", CliCommandStrings.BuildServerCommandDescription) - { - DocsLink = DocsLink - }; - - command.Subcommands.Add(BuildServerShutdownCommandParser.GetCommand()); - command.SetAction((parseResult) => parseResult.HandleMissingCommand()); - + command.Subcommands.Single(c => c.Name == BuildServerCommandDefinition.Name).SetAction((parseResult) => new BuildServerShutdownCommand(parseResult).Execute()); return command; } } diff --git a/src/Cli/dotnet/Commands/BuildServer/Shutdown/BuildServerShutdownCommand.cs b/src/Cli/dotnet/Commands/BuildServer/Shutdown/BuildServerShutdownCommand.cs index 244660651751..62f83d368b74 100644 --- a/src/Cli/dotnet/Commands/BuildServer/Shutdown/BuildServerShutdownCommand.cs +++ b/src/Cli/dotnet/Commands/BuildServer/Shutdown/BuildServerShutdownCommand.cs @@ -25,9 +25,9 @@ public BuildServerShutdownCommand( IReporter reporter = null) : base(result) { - bool msbuild = result.GetValue(BuildServerShutdownCommandParser.MSBuildOption); - bool vbcscompiler = result.GetValue(BuildServerShutdownCommandParser.VbcsOption); - bool razor = result.GetValue(BuildServerShutdownCommandParser.RazorOption); + bool msbuild = result.GetValue(BuildServerShutdownCommandDefinition.MSBuildOption); + bool vbcscompiler = result.GetValue(BuildServerShutdownCommandDefinition.VbcsOption); + bool razor = result.GetValue(BuildServerShutdownCommandDefinition.RazorOption); bool all = !msbuild && !vbcscompiler && !razor; _enumerationFlags = ServerEnumerationFlags.None; diff --git a/src/Cli/dotnet/Commands/BuildServer/Shutdown/BuildServerShutdownCommandParser.cs b/src/Cli/dotnet/Commands/BuildServer/Shutdown/BuildServerShutdownCommandDefinition.cs similarity index 75% rename from src/Cli/dotnet/Commands/BuildServer/Shutdown/BuildServerShutdownCommandParser.cs rename to src/Cli/dotnet/Commands/BuildServer/Shutdown/BuildServerShutdownCommandDefinition.cs index a34ea767f28f..1ceed33ff7d9 100644 --- a/src/Cli/dotnet/Commands/BuildServer/Shutdown/BuildServerShutdownCommandParser.cs +++ b/src/Cli/dotnet/Commands/BuildServer/Shutdown/BuildServerShutdownCommandDefinition.cs @@ -7,7 +7,7 @@ namespace Microsoft.DotNet.Cli.Commands.BuildServer.Shutdown; -internal static class BuildServerShutdownCommandParser +internal static class BuildServerShutdownCommandDefinition { public static readonly Option MSBuildOption = new("--msbuild") { @@ -27,14 +27,7 @@ internal static class BuildServerShutdownCommandParser Arity = ArgumentArity.Zero }; - private static readonly Command Command = ConstructCommand(); - - public static Command GetCommand() - { - return Command; - } - - private static Command ConstructCommand() + public static Command Create() { Command command = new("shutdown", CliCommandStrings.BuildServerShutdownCommandDescription); @@ -42,8 +35,6 @@ private static Command ConstructCommand() command.Options.Add(VbcsOption); command.Options.Add(RazorOption); - command.SetAction((parseResult) => new BuildServerShutdownCommand(parseResult).Execute()); - return command; } } diff --git a/src/Cli/dotnet/Commands/Clean/CleanCommand.cs b/src/Cli/dotnet/Commands/Clean/CleanCommand.cs index c7e48c376ee9..15a183a4da6d 100644 --- a/src/Cli/dotnet/Commands/Clean/CleanCommand.cs +++ b/src/Cli/dotnet/Commands/Clean/CleanCommand.cs @@ -22,7 +22,7 @@ public static CommandBase FromParseResult(ParseResult result, string? msbuildPat result.ShowHelpOrErrorIfAppropriate(); return CommandFactory.CreateVirtualOrPhysicalCommand( CleanCommandParser.GetCommand(), - CleanCommandParser.SlnOrProjectOrFileArgument, + CleanCommandDefinition.SlnOrProjectOrFileArgument, static (msbuildArgs, appFilePath) => new VirtualProjectBuildingCommand( entryPointFileFullPath: appFilePath, msbuildArgs: msbuildArgs) @@ -33,7 +33,7 @@ public static CommandBase FromParseResult(ParseResult result, string? msbuildPat NoWriteBuildMarkers = true, }, static (msbuildArgs, msbuildPath) => new CleanCommand(msbuildArgs, msbuildPath), - [ CommonOptions.PropertiesOption, CommonOptions.RestorePropertiesOption, CleanCommandParser.TargetOption, CleanCommandParser.VerbosityOption, CleanCommandParser.NoLogoOption], + [ CommonOptions.PropertiesOption, CommonOptions.RestorePropertiesOption, CleanCommandDefinition.TargetOption, CleanCommandDefinition.VerbosityOption, CleanCommandDefinition.NoLogoOption], result, msbuildPath ); diff --git a/src/Cli/dotnet/Commands/Clean/CleanCommandDefinition.cs b/src/Cli/dotnet/Commands/Clean/CleanCommandDefinition.cs new file mode 100644 index 000000000000..1dc8e9028aed --- /dev/null +++ b/src/Cli/dotnet/Commands/Clean/CleanCommandDefinition.cs @@ -0,0 +1,63 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.CommandLine; +using Microsoft.DotNet.Cli.CommandLine; +using Microsoft.DotNet.Cli.Commands.Clean.FileBasedAppArtifacts; +using Microsoft.DotNet.Cli.Extensions; + +namespace Microsoft.DotNet.Cli.Commands.Clean; + +internal static class CleanCommandDefinition +{ + public static readonly string DocsLink = "https://aka.ms/dotnet-clean"; + + public static readonly Argument SlnOrProjectOrFileArgument = new(CliStrings.SolutionOrProjectOrFileArgumentName) + { + Description = CliStrings.SolutionOrProjectOrFileArgumentDescription, + Arity = ArgumentArity.ZeroOrMore + }; + + public static readonly Option OutputOption = new Option("--output", "-o") + { + Description = CliCommandStrings.CleanCmdOutputDirDescription, + HelpName = CliCommandStrings.CleanCmdOutputDir + }.ForwardAsOutputPath("OutputPath"); + + public static readonly Option NoLogoOption = CommonOptions.NoLogoOption(); + + public static readonly Option FrameworkOption = CommonOptions.FrameworkOption(CliCommandStrings.CleanFrameworkOptionDescription); + + public static readonly Option ConfigurationOption = CommonOptions.ConfigurationOption(CliCommandStrings.CleanConfigurationOptionDescription); + + public static readonly Option TargetOption = CommonOptions.RequiredMSBuildTargetOption("Clean"); + + public static readonly Option VerbosityOption = CommonOptions.VerbosityOption(Utils.VerbosityOptions.normal); + + public static Command Create() + { + Command command = new("clean", CliCommandStrings.CleanAppFullName) + { + DocsLink = DocsLink + }; + + command.Arguments.Add(SlnOrProjectOrFileArgument); + command.Options.Add(FrameworkOption); + command.Options.Add(CommonOptions.RuntimeOption(CliCommandStrings.CleanRuntimeOptionDescription)); + command.Options.Add(ConfigurationOption); + command.Options.Add(CommonOptions.InteractiveMsBuildForwardOption); + command.Options.Add(VerbosityOption); + command.Options.Add(OutputOption); + command.Options.Add(CommonOptions.ArtifactsPathOption); + command.Options.Add(NoLogoOption); + command.Options.Add(CommonOptions.DisableBuildServersOption); + command.Options.Add(TargetOption); + command.Options.Add(CommonOptions.GetPropertyOption); + command.Options.Add(CommonOptions.GetItemOption); + command.Options.Add(CommonOptions.GetTargetResultOption); + command.Options.Add(CommonOptions.GetResultOutputFileOption); + command.Subcommands.Add(CleanFileBasedAppArtifactsCommandDefinition.Create()); + + return command; + } +} diff --git a/src/Cli/dotnet/Commands/Clean/CleanCommandParser.cs b/src/Cli/dotnet/Commands/Clean/CleanCommandParser.cs index 117537d7fd1f..8a9237d558b0 100644 --- a/src/Cli/dotnet/Commands/Clean/CleanCommandParser.cs +++ b/src/Cli/dotnet/Commands/Clean/CleanCommandParser.cs @@ -2,72 +2,26 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.CommandLine; -using Microsoft.DotNet.Cli.CommandLine; using Microsoft.DotNet.Cli.Commands.Clean.FileBasedAppArtifacts; -using Microsoft.DotNet.Cli.Extensions; namespace Microsoft.DotNet.Cli.Commands.Clean; internal static class CleanCommandParser { - public static readonly string DocsLink = "https://aka.ms/dotnet-clean"; - - public static readonly Argument SlnOrProjectOrFileArgument = new(CliStrings.SolutionOrProjectOrFileArgumentName) - { - Description = CliStrings.SolutionOrProjectOrFileArgumentDescription, - Arity = ArgumentArity.ZeroOrMore - }; - - public static readonly Option OutputOption = new Option("--output", "-o") - { - Description = CliCommandStrings.CleanCmdOutputDirDescription, - HelpName = CliCommandStrings.CleanCmdOutputDir - }.ForwardAsOutputPath("OutputPath"); - - public static readonly Option NoLogoOption = CommonOptions.NoLogoOption(); - - public static readonly Option FrameworkOption = CommonOptions.FrameworkOption(CliCommandStrings.CleanFrameworkOptionDescription); - - public static readonly Option ConfigurationOption = CommonOptions.ConfigurationOption(CliCommandStrings.CleanConfigurationOptionDescription); - - public static readonly Option TargetOption = CommonOptions.RequiredMSBuildTargetOption("Clean"); - - public static readonly Option VerbosityOption = CommonOptions.VerbosityOption(Utils.VerbosityOptions.normal); - - - private static readonly Command Command = ConstructCommand(); + private static readonly Command Command = SetAction(CleanCommandDefinition.Create()); public static Command GetCommand() { return Command; } - private static Command ConstructCommand() + private static Command SetAction(Command command) { - Command command = new("clean", CliCommandStrings.CleanAppFullName) - { - DocsLink = DocsLink - }; - - command.Arguments.Add(SlnOrProjectOrFileArgument); - command.Options.Add(FrameworkOption); - command.Options.Add(CommonOptions.RuntimeOption(CliCommandStrings.CleanRuntimeOptionDescription)); - command.Options.Add(ConfigurationOption); - command.Options.Add(CommonOptions.InteractiveMsBuildForwardOption); - command.Options.Add(VerbosityOption); - command.Options.Add(OutputOption); - command.Options.Add(CommonOptions.ArtifactsPathOption); - command.Options.Add(NoLogoOption); - command.Options.Add(CommonOptions.DisableBuildServersOption); - command.Options.Add(TargetOption); - command.Options.Add(CommonOptions.GetPropertyOption); - command.Options.Add(CommonOptions.GetItemOption); - command.Options.Add(CommonOptions.GetTargetResultOption); - command.Options.Add(CommonOptions.GetResultOutputFileOption); - command.Subcommands.Add(CleanFileBasedAppArtifactsCommandParser.Command); - command.SetAction(CleanCommand.Run); + command.Subcommands.Single(c => c.Name == CleanFileBasedAppArtifactsCommandDefinition.Name) + .SetAction(parseResult => new CleanFileBasedAppArtifactsCommand(parseResult).Execute()); + return command; } } diff --git a/src/Cli/dotnet/Commands/Clean/FileBasedAppArtifacts/CleanFileBasedAppArtifactsCommand.cs b/src/Cli/dotnet/Commands/Clean/FileBasedAppArtifacts/CleanFileBasedAppArtifactsCommand.cs index 006e8830ad5c..6654ab3697b9 100644 --- a/src/Cli/dotnet/Commands/Clean/FileBasedAppArtifacts/CleanFileBasedAppArtifactsCommand.cs +++ b/src/Cli/dotnet/Commands/Clean/FileBasedAppArtifacts/CleanFileBasedAppArtifactsCommand.cs @@ -14,7 +14,7 @@ internal sealed class CleanFileBasedAppArtifactsCommand(ParseResult parseResult) { public override int Execute() { - bool dryRun = _parseResult.GetValue(CleanFileBasedAppArtifactsCommandParser.DryRunOption); + bool dryRun = _parseResult.GetValue(CleanFileBasedAppArtifactsCommandDefinition.DryRunOption); using var metadataFileStream = OpenMetadataFile(); @@ -70,7 +70,7 @@ private IEnumerable GetFoldersToRemove() Reporter.Output.WriteLine(CliCommandStrings.CleanFileBasedAppArtifactsScanning, directory.FullName); - var days = _parseResult.GetValue(CleanFileBasedAppArtifactsCommandParser.DaysOption); + var days = _parseResult.GetValue(CleanFileBasedAppArtifactsCommandDefinition.DaysOption); var cutoff = DateTime.UtcNow.AddDays(-days); foreach (var subdir in directory.GetDirectories()) @@ -89,7 +89,7 @@ private static FileInfo GetMetadataFile() private FileStream? OpenMetadataFile() { - if (!_parseResult.GetValue(CleanFileBasedAppArtifactsCommandParser.AutomaticOption)) + if (!_parseResult.GetValue(CleanFileBasedAppArtifactsCommandDefinition.AutomaticOption)) { return null; } @@ -127,8 +127,8 @@ public static void StartAutomaticCleanupIfNeeded() Arguments = ArgumentEscaper.EscapeAndConcatenateArgArrayForProcessStart( [ CleanCommandParser.GetCommand().Name, - CleanFileBasedAppArtifactsCommandParser.Command.Name, - CleanFileBasedAppArtifactsCommandParser.AutomaticOption.Name, + CleanFileBasedAppArtifactsCommandDefinition.Name, + CleanFileBasedAppArtifactsCommandDefinition.AutomaticOption.Name, ]), UseShellExecute = false, RedirectStandardInput = true, diff --git a/src/Cli/dotnet/Commands/Clean/FileBasedAppArtifacts/CleanFileBasedAppArtifactsCommandParser.cs b/src/Cli/dotnet/Commands/Clean/FileBasedAppArtifacts/CleanFileBasedAppArtifactsCommandDefinition.cs similarity index 73% rename from src/Cli/dotnet/Commands/Clean/FileBasedAppArtifacts/CleanFileBasedAppArtifactsCommandParser.cs rename to src/Cli/dotnet/Commands/Clean/FileBasedAppArtifacts/CleanFileBasedAppArtifactsCommandDefinition.cs index 818c4f23a5ec..db9bf92bd846 100644 --- a/src/Cli/dotnet/Commands/Clean/FileBasedAppArtifacts/CleanFileBasedAppArtifactsCommandParser.cs +++ b/src/Cli/dotnet/Commands/Clean/FileBasedAppArtifacts/CleanFileBasedAppArtifactsCommandDefinition.cs @@ -5,8 +5,10 @@ namespace Microsoft.DotNet.Cli.Commands.Clean.FileBasedAppArtifacts; -internal sealed class CleanFileBasedAppArtifactsCommandParser +internal sealed class CleanFileBasedAppArtifactsCommandDefinition { + public const string Name = "file-based-apps"; + public static readonly Option DryRunOption = new("--dry-run") { Description = CliCommandStrings.CleanFileBasedAppArtifactsDryRun, @@ -28,11 +30,8 @@ internal sealed class CleanFileBasedAppArtifactsCommandParser Hidden = true, }; - public static Command Command => field ??= ConstructCommand(); - - private static Command ConstructCommand() - { - Command command = new("file-based-apps", CliCommandStrings.CleanFileBasedAppArtifactsCommandDescription) + public static Command Create() + => new(Name, CliCommandStrings.CleanFileBasedAppArtifactsCommandDescription) { Hidden = true, Options = @@ -42,8 +41,4 @@ private static Command ConstructCommand() AutomaticOption, }, }; - - command.SetAction((parseResult) => new CleanFileBasedAppArtifactsCommand(parseResult).Execute()); - return command; - } } diff --git a/src/Cli/dotnet/Commands/Dnx/DnxCommandDefinition.cs b/src/Cli/dotnet/Commands/Dnx/DnxCommandDefinition.cs new file mode 100644 index 000000000000..03546f4ac5a8 --- /dev/null +++ b/src/Cli/dotnet/Commands/Dnx/DnxCommandDefinition.cs @@ -0,0 +1,28 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.CommandLine; +using Microsoft.DotNet.Cli.Commands.Tool.Execute; + +namespace Microsoft.DotNet.Cli.Commands.Dnx; + +internal static class DnxCommandDefinition +{ + public static Command Create() + { + Command command = new("dnx", CliCommandStrings.ToolExecuteCommandDescription); + command.Hidden = true; + + foreach (var argument in ToolExecuteCommandParser.Command.Arguments) + { + command.Arguments.Add(argument); + } + + foreach (var option in ToolExecuteCommandParser.Command.Options) + { + command.Options.Add(option); + } + + return command; + } +} diff --git a/src/Cli/dotnet/Commands/Dnx/DnxCommandParser.cs b/src/Cli/dotnet/Commands/Dnx/DnxCommandParser.cs index 89938373a637..2e3488ff5e13 100644 --- a/src/Cli/dotnet/Commands/Dnx/DnxCommandParser.cs +++ b/src/Cli/dotnet/Commands/Dnx/DnxCommandParser.cs @@ -1,37 +1,23 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. using System.CommandLine; -using Microsoft.DotNet.Cli.Commands.Tool; using Microsoft.DotNet.Cli.Commands.Tool.Execute; namespace Microsoft.DotNet.Cli.Commands.Dnx; internal static class DnxCommandParser { - public static readonly Command Command = ConstructCommand(); + public static readonly Command Command = ConfigureCommand(DnxCommandDefinition.Create()); + public static Command GetCommand() { return Command; } - private static Command ConstructCommand() + private static Command ConfigureCommand(Command command) { - Command command = new("dnx", CliCommandStrings.ToolExecuteCommandDescription); - command.Hidden = true; - - foreach (var argument in ToolExecuteCommandParser.Command.Arguments) - { - command.Arguments.Add(argument); - } - - foreach (var option in ToolExecuteCommandParser.Command.Options) - { - command.Options.Add(option); - } - command.SetAction((parseResult) => new ToolExecuteCommand(parseResult).Execute()); - return command; } } diff --git a/src/Cli/dotnet/Commands/Format/FormatCommand.cs b/src/Cli/dotnet/Commands/Format/FormatCommand.cs index 9ee9296172fa..ae4bedf56185 100644 --- a/src/Cli/dotnet/Commands/Format/FormatCommand.cs +++ b/src/Cli/dotnet/Commands/Format/FormatCommand.cs @@ -19,7 +19,7 @@ public static FormatCommand FromArgs(string[] args) public static FormatCommand FromParseResult(ParseResult result) { - return new FormatCommand(result.GetValue(FormatCommandParser.Arguments)); + return new FormatCommand(result.GetValue(FormatCommandDefinition.Arguments)); } public static int Run(ParseResult parseResult) diff --git a/src/Cli/dotnet/Commands/Format/FormatCommandDefinition.cs b/src/Cli/dotnet/Commands/Format/FormatCommandDefinition.cs new file mode 100644 index 000000000000..5419f8917cc1 --- /dev/null +++ b/src/Cli/dotnet/Commands/Format/FormatCommandDefinition.cs @@ -0,0 +1,24 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.CommandLine; +using Microsoft.DotNet.Cli.CommandLine; + +namespace Microsoft.DotNet.Cli.Commands.Format; + +internal static class FormatCommandDefinition +{ + public const string Name = "format"; + + public static readonly Argument Arguments = new("arguments"); + + public static readonly string DocsLink = "https://aka.ms/dotnet-format"; + + public static Command Create() + { + var formatCommand = new Command(Name) + { + Arguments = { Arguments }, + DocsLink = DocsLink, + }; +} diff --git a/src/Cli/dotnet/Commands/Format/FormatCommandParser.cs b/src/Cli/dotnet/Commands/Format/FormatCommandParser.cs index dcbeb642e4a8..7ba0478c04ec 100644 --- a/src/Cli/dotnet/Commands/Format/FormatCommandParser.cs +++ b/src/Cli/dotnet/Commands/Format/FormatCommandParser.cs @@ -1,34 +1,22 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#nullable disable - using System.CommandLine; -using Microsoft.DotNet.Cli.CommandLine; namespace Microsoft.DotNet.Cli.Commands.Format; -internal static partial class FormatCommandParser +internal static class FormatCommandParser { - public static readonly Argument Arguments = new("arguments"); - - public static readonly string DocsLink = "https://aka.ms/dotnet-format"; - - private static readonly Command Command = ConstructCommand(); + private static readonly Command Command = SetAction(FormatCommandDefinition.Create()); public static Command GetCommand() { return Command; } - private static Command ConstructCommand() + private static Command SetAction(Command command) { - var formatCommand = new Command("format") - { - Arguments = { Arguments }, - DocsLink = DocsLink, - }; - formatCommand.SetAction((parseResult) => FormatCommand.Run(parseResult.GetValue(Arguments))); - return formatCommand; + command.SetAction(parseResult => FormatCommand.Run(parseResult.GetValue(FormatCommandDefinition.Arguments))); + return command; } } diff --git a/src/Cli/dotnet/Commands/Fsi/FsiCommandDefinition.cs b/src/Cli/dotnet/Commands/Fsi/FsiCommandDefinition.cs new file mode 100644 index 000000000000..7dd63f6d4057 --- /dev/null +++ b/src/Cli/dotnet/Commands/Fsi/FsiCommandDefinition.cs @@ -0,0 +1,26 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.CommandLine; +using Microsoft.DotNet.Cli.CommandLine; + +namespace Microsoft.DotNet.Cli.Commands.Fsi; + +internal static class FsiCommandDefinition +{ + public const string Name = "fsi"; + + public static readonly string DocsLink = "https://aka.ms/dotnet-fsi"; + + public static readonly Argument Arguments = new("arguments"); + + public static Command Create() + { + Command command = new(Name) { + Arguments = { Arguments }, + DocsLink = DocsLink, + }; + + return command; + } +} diff --git a/src/Cli/dotnet/Commands/Fsi/FsiCommandParser.cs b/src/Cli/dotnet/Commands/Fsi/FsiCommandParser.cs index c81c34e79566..06d3a2a18a33 100644 --- a/src/Cli/dotnet/Commands/Fsi/FsiCommandParser.cs +++ b/src/Cli/dotnet/Commands/Fsi/FsiCommandParser.cs @@ -1,34 +1,22 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#nullable disable - using System.CommandLine; -using Microsoft.DotNet.Cli.CommandLine; namespace Microsoft.DotNet.Cli.Commands.Fsi; internal static class FsiCommandParser { - public static readonly string DocsLink = "https://aka.ms/dotnet-fsi"; - - public static readonly Argument Arguments = new("arguments"); - - private static readonly Command Command = ConstructCommand(); + private static readonly Command Command = SetAction(FsiCommandDefinition.Create()); public static Command GetCommand() { return Command; } - private static Command ConstructCommand() + private static Command SetAction(Command command) { - Command command = new("fsi") { - Arguments = { Arguments }, - DocsLink = DocsLink, - }; - command.SetAction((parseResult) => FsiCommand.Run(parseResult.GetValue(Arguments))); - + command.SetAction((parseResult) => FsiCommand.Run(parseResult.GetValue(FsiCommandDefinition.Arguments))); return command; } } diff --git a/src/Cli/dotnet/Commands/Help/HelpCommand.cs b/src/Cli/dotnet/Commands/Help/HelpCommand.cs index a2646a3764d4..1eb9a76e32fa 100644 --- a/src/Cli/dotnet/Commands/Help/HelpCommand.cs +++ b/src/Cli/dotnet/Commands/Help/HelpCommand.cs @@ -20,7 +20,7 @@ public static int Run(ParseResult result) result.ShowHelpOrErrorIfAppropriate(); - if (result.GetValue(HelpCommandParser.Argument) is string[] args && args is not []) + if (result.GetValue(HelpCommandDefinition.Argument) is string[] args && args is not []) { return new HelpCommand(args).Execute(); } diff --git a/src/Cli/dotnet/Commands/Help/HelpCommandDefinition.cs b/src/Cli/dotnet/Commands/Help/HelpCommandDefinition.cs new file mode 100644 index 000000000000..50802d4463f8 --- /dev/null +++ b/src/Cli/dotnet/Commands/Help/HelpCommandDefinition.cs @@ -0,0 +1,31 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.CommandLine; +using Microsoft.DotNet.Cli.CommandLine; + +namespace Microsoft.DotNet.Cli.Commands.Help; + +internal static class HelpCommandDefinition +{ + public static readonly string DocsLink = "https://aka.ms/dotnet-help"; + + public static readonly Argument Argument = new(CliCommandStrings.CommandArgumentName) + { + Description = CliCommandStrings.CommandArgumentDescription, + Arity = ArgumentArity.ZeroOrMore + }; + + public static Command Create() + { + Command command = new("help", CliCommandStrings.HelpAppFullName) + { + DocsLink = DocsLink + }; + + command.Arguments.Add(Argument); + + return command; + } +} + diff --git a/src/Cli/dotnet/Commands/Help/HelpCommandParser.cs b/src/Cli/dotnet/Commands/Help/HelpCommandParser.cs index 8334e262381f..f6226803fb17 100644 --- a/src/Cli/dotnet/Commands/Help/HelpCommandParser.cs +++ b/src/Cli/dotnet/Commands/Help/HelpCommandParser.cs @@ -1,42 +1,22 @@ -// Licensed to the .NET Foundation under one or more agreements. +// Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#nullable disable - using System.CommandLine; -using Microsoft.DotNet.Cli.CommandLine; namespace Microsoft.DotNet.Cli.Commands.Help; internal static class HelpCommandParser { - public static readonly string DocsLink = "https://aka.ms/dotnet-help"; - - public static readonly Argument Argument = new(CliCommandStrings.CommandArgumentName) - { - Description = CliCommandStrings.CommandArgumentDescription, - Arity = ArgumentArity.ZeroOrMore - }; - - private static readonly Command Command = ConstructCommand(); + private static readonly Command Command = SetAction(HelpCommandDefinition.Create()); public static Command GetCommand() { return Command; } - private static Command ConstructCommand() + private static Command SetAction(Command command) { - Command command = new("help", CliCommandStrings.HelpAppFullName) - { - DocsLink = DocsLink - }; - - command.Arguments.Add(Argument); - command.SetAction(HelpCommand.Run); - return command; } } - diff --git a/src/Cli/dotnet/Commands/Hidden/Add/AddCommandDefinition.cs b/src/Cli/dotnet/Commands/Hidden/Add/AddCommandDefinition.cs new file mode 100644 index 000000000000..a0c890ca18ea --- /dev/null +++ b/src/Cli/dotnet/Commands/Hidden/Add/AddCommandDefinition.cs @@ -0,0 +1,31 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.CommandLine; +using Microsoft.DotNet.Cli.Commands.Hidden.Add.Package; +using Microsoft.DotNet.Cli.Commands.Hidden.Add.Reference; +using Microsoft.DotNet.Cli.Commands.Package; + +namespace Microsoft.DotNet.Cli.Commands.Hidden.Add; + +internal static class AddCommandDefinition +{ + public const string Name = "add"; + + public static readonly string DocsLink = "https://aka.ms/dotnet-add"; + + public static Command Create() + { + var command = new Command(Name, CliCommandStrings.NetAddCommand) + { + Hidden = true, + DocsLink = DocsLink + }; + + command.Arguments.Add(PackageCommandDefinition.ProjectOrFileArgument); + command.Subcommands.Add(AddPackageCommandDefinition.Create()); + command.Subcommands.Add(AddReferenceCommandDefinition.Create()); + + return command; + } +} diff --git a/src/Cli/dotnet/Commands/Hidden/Add/AddCommandParser.cs b/src/Cli/dotnet/Commands/Hidden/Add/AddCommandParser.cs index c2d21405b1f2..bbd7230b0829 100644 --- a/src/Cli/dotnet/Commands/Hidden/Add/AddCommandParser.cs +++ b/src/Cli/dotnet/Commands/Hidden/Add/AddCommandParser.cs @@ -4,37 +4,27 @@ using System.CommandLine; using Microsoft.DotNet.Cli.Commands.Hidden.Add.Package; using Microsoft.DotNet.Cli.Commands.Hidden.Add.Reference; -using Microsoft.DotNet.Cli.Commands.Package; -using Microsoft.DotNet.Cli.CommandLine; +using Microsoft.DotNet.Cli.Commands.Package.Add; +using Microsoft.DotNet.Cli.Commands.Reference.Add; using Microsoft.DotNet.Cli.Extensions; namespace Microsoft.DotNet.Cli.Commands.Hidden.Add; internal static class AddCommandParser { - public static readonly string DocsLink = "https://aka.ms/dotnet-add"; - - private static readonly Command Command = ConstructCommand(); + private static readonly Command Command = SetAction(AddCommandDefinition.Create()); public static Command GetCommand() { return Command; } - private static Command ConstructCommand() + private static Command SetAction(Command command) { - var command = new Command("add", CliCommandStrings.NetAddCommand) - { - Hidden = true, - DocsLink = DocsLink - }; - - command.Arguments.Add(PackageCommandParser.ProjectOrFileArgument); - command.Subcommands.Add(AddPackageCommandParser.GetCommand()); - command.Subcommands.Add(AddReferenceCommandParser.GetCommand()); - command.SetAction((parseResult) => parseResult.HandleMissingCommand()); + command.Subcommands.Single(c => c.Name == AddPackageCommandDefinition.Name).SetAction((parseResult) => new PackageAddCommand(parseResult).Execute()); + command.Subcommands.Single(c => c.Name == AddReferenceCommandDefinition.Name).SetAction((parseResult) => new ReferenceAddCommand(parseResult).Execute()); return command; } } diff --git a/src/Cli/dotnet/Commands/Hidden/Add/Package/AddPackageCommandDefinition.cs b/src/Cli/dotnet/Commands/Hidden/Add/Package/AddPackageCommandDefinition.cs new file mode 100644 index 000000000000..213cf1c18fa8 --- /dev/null +++ b/src/Cli/dotnet/Commands/Hidden/Add/Package/AddPackageCommandDefinition.cs @@ -0,0 +1,31 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.CommandLine; +using Microsoft.DotNet.Cli.Commands.Package; +using Microsoft.DotNet.Cli.Commands.Package.Add; + +namespace Microsoft.DotNet.Cli.Commands.Hidden.Add.Package; + +internal static class AddPackageCommandDefinition +{ + public const string Name = "package"; + + public static Command Create() + { + Command command = new(Name, CliCommandStrings.PackageAddAppFullName); + + command.Arguments.Add(PackageAddCommandDefinition.CmdPackageArgument); + command.Options.Add(PackageAddCommandDefinition.VersionOption); + command.Options.Add(PackageAddCommandDefinition.FrameworkOption); + command.Options.Add(PackageAddCommandDefinition.NoRestoreOption); + command.Options.Add(PackageAddCommandDefinition.SourceOption); + command.Options.Add(PackageAddCommandDefinition.PackageDirOption); + command.Options.Add(PackageAddCommandDefinition.InteractiveOption); + command.Options.Add(PackageAddCommandDefinition.PrereleaseOption); + command.Options.Add(PackageCommandDefinition.ProjectOption); + command.Options.Add(PackageCommandDefinition.FileOption); + + return command; + } +} diff --git a/src/Cli/dotnet/Commands/Hidden/Add/Package/AddPackageCommandParser.cs b/src/Cli/dotnet/Commands/Hidden/Add/Package/AddPackageCommandParser.cs deleted file mode 100644 index f6cb1909b23d..000000000000 --- a/src/Cli/dotnet/Commands/Hidden/Add/Package/AddPackageCommandParser.cs +++ /dev/null @@ -1,38 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.CommandLine; -using Microsoft.DotNet.Cli.Commands.Package; -using Microsoft.DotNet.Cli.Commands.Package.Add; - -namespace Microsoft.DotNet.Cli.Commands.Hidden.Add.Package; - -internal static class AddPackageCommandParser -{ - private static readonly Command Command = ConstructCommand(); - - public static Command GetCommand() - { - return Command; - } - - private static Command ConstructCommand() - { - Command command = new("package", CliCommandStrings.PackageAddAppFullName); - - command.Arguments.Add(PackageAddCommandParser.CmdPackageArgument); - command.Options.Add(PackageAddCommandParser.VersionOption); - command.Options.Add(PackageAddCommandParser.FrameworkOption); - command.Options.Add(PackageAddCommandParser.NoRestoreOption); - command.Options.Add(PackageAddCommandParser.SourceOption); - command.Options.Add(PackageAddCommandParser.PackageDirOption); - command.Options.Add(PackageAddCommandParser.InteractiveOption); - command.Options.Add(PackageAddCommandParser.PrereleaseOption); - command.Options.Add(PackageCommandParser.ProjectOption); - command.Options.Add(PackageCommandParser.FileOption); - - command.SetAction((parseResult) => new PackageAddCommand(parseResult).Execute()); - - return command; - } -} diff --git a/src/Cli/dotnet/Commands/Hidden/Add/Reference/AddReferenceCommandDefinition.cs b/src/Cli/dotnet/Commands/Hidden/Add/Reference/AddReferenceCommandDefinition.cs new file mode 100644 index 000000000000..2bfe74a9b573 --- /dev/null +++ b/src/Cli/dotnet/Commands/Hidden/Add/Reference/AddReferenceCommandDefinition.cs @@ -0,0 +1,25 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.CommandLine; +using Microsoft.DotNet.Cli.Commands.Reference; +using Microsoft.DotNet.Cli.Commands.Reference.Add; + +namespace Microsoft.DotNet.Cli.Commands.Hidden.Add.Reference; + +internal static class AddReferenceCommandDefinition +{ + public const string Name = "reference"; + + public static Command Create() + { + Command command = new(Name, CliCommandStrings.ReferenceAddAppFullName); + + command.Arguments.Add(ReferenceAddCommandDefinition.ProjectPathArgument); + command.Options.Add(ReferenceAddCommandDefinition.FrameworkOption); + command.Options.Add(ReferenceAddCommandDefinition.InteractiveOption); + command.Options.Add(ReferenceCommandDefinition.ProjectOption); + + return command; + } +} diff --git a/src/Cli/dotnet/Commands/Hidden/Add/Reference/AddReferenceCommandParser.cs b/src/Cli/dotnet/Commands/Hidden/Add/Reference/AddReferenceCommandParser.cs deleted file mode 100644 index 2cb98fd75b35..000000000000 --- a/src/Cli/dotnet/Commands/Hidden/Add/Reference/AddReferenceCommandParser.cs +++ /dev/null @@ -1,34 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -#nullable disable - -using System.CommandLine; -using Microsoft.DotNet.Cli.Commands.Reference; -using Microsoft.DotNet.Cli.Commands.Reference.Add; - -namespace Microsoft.DotNet.Cli.Commands.Hidden.Add.Reference; - -internal static class AddReferenceCommandParser -{ - private static readonly Command Command = ConstructCommand(); - - public static Command GetCommand() - { - return Command; - } - - private static Command ConstructCommand() - { - Command command = new("reference", CliCommandStrings.ReferenceAddAppFullName); - - command.Arguments.Add(ReferenceAddCommandParser.ProjectPathArgument); - command.Options.Add(ReferenceAddCommandParser.FrameworkOption); - command.Options.Add(ReferenceAddCommandParser.InteractiveOption); - command.Options.Add(ReferenceCommandParser.ProjectOption); - - command.SetAction((parseResult) => new ReferenceAddCommand(parseResult).Execute()); - - return command; - } -} diff --git a/src/Cli/dotnet/Commands/Hidden/Complete/CompleteCommand.cs b/src/Cli/dotnet/Commands/Hidden/Complete/CompleteCommand.cs index 5cdf66cfca6e..cd193ed52669 100644 --- a/src/Cli/dotnet/Commands/Hidden/Complete/CompleteCommand.cs +++ b/src/Cli/dotnet/Commands/Hidden/Complete/CompleteCommand.cs @@ -51,9 +51,9 @@ public static int RunWithReporter(ParseResult result, IReporter reporter) private static CompletionItem[] Completions(ParseResult complete) { - var input = complete.GetValue(CompleteCommandParser.PathArgument) ?? string.Empty; + var input = complete.GetValue(CompleteCommandDefinition.PathArgument) ?? string.Empty; - var position = complete.GetValue(CompleteCommandParser.PositionOption); + var position = complete.GetValue(CompleteCommandDefinition.PositionOption); if (position > input.Length) { diff --git a/src/Cli/dotnet/Commands/Hidden/Complete/CompleteCommandDefinition.cs b/src/Cli/dotnet/Commands/Hidden/Complete/CompleteCommandDefinition.cs new file mode 100644 index 000000000000..cdfbe82b5f13 --- /dev/null +++ b/src/Cli/dotnet/Commands/Hidden/Complete/CompleteCommandDefinition.cs @@ -0,0 +1,29 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.CommandLine; + +namespace Microsoft.DotNet.Cli.Commands.Hidden.Complete; + +internal static class CompleteCommandDefinition +{ + public static readonly Argument PathArgument = new("path"); + + public static readonly Option PositionOption = new("--position") + { + HelpName = "command" + }; + + public static Command Create() + { + Command command = new("complete") + { + Hidden = true + }; + + command.Arguments.Add(PathArgument); + command.Options.Add(PositionOption); + + return command; + } +} diff --git a/src/Cli/dotnet/Commands/Hidden/Complete/CompleteCommandParser.cs b/src/Cli/dotnet/Commands/Hidden/Complete/CompleteCommandParser.cs index e613f4ad739a..459f128ebc79 100644 --- a/src/Cli/dotnet/Commands/Hidden/Complete/CompleteCommandParser.cs +++ b/src/Cli/dotnet/Commands/Hidden/Complete/CompleteCommandParser.cs @@ -1,40 +1,22 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#nullable disable - using System.CommandLine; namespace Microsoft.DotNet.Cli.Commands.Hidden.Complete; internal static class CompleteCommandParser { - public static readonly Argument PathArgument = new("path"); - - public static readonly Option PositionOption = new("--position") - { - HelpName = "command" - }; - - private static readonly Command Command = ConstructCommand(); + private static readonly Command Command = SetAction(CompleteCommandDefinition.Create()); public static Command GetCommand() { return Command; } - private static Command ConstructCommand() + private static Command SetAction(Command command) { - Command command = new("complete") - { - Hidden = true - }; - - command.Arguments.Add(PathArgument); - command.Options.Add(PositionOption); - command.SetAction(CompleteCommand.Run); - return command; } } diff --git a/src/Cli/dotnet/Commands/Hidden/InternalReportInstallSuccess/InternalReportInstallSuccessCommand.cs b/src/Cli/dotnet/Commands/Hidden/InternalReportInstallSuccess/InternalReportInstallSuccessCommand.cs index 744289023948..af7e3df0c98e 100644 --- a/src/Cli/dotnet/Commands/Hidden/InternalReportInstallSuccess/InternalReportInstallSuccessCommand.cs +++ b/src/Cli/dotnet/Commands/Hidden/InternalReportInstallSuccess/InternalReportInstallSuccessCommand.cs @@ -31,7 +31,7 @@ public static void ProcessInputAndSendTelemetry(string[] args, ITelemetry teleme public static void ProcessInputAndSendTelemetry(ParseResult result, ITelemetry telemetry) { - var exeName = Path.GetFileName(result.GetValue(InternalReportInstallSuccessCommandParser.Argument)); + var exeName = Path.GetFileName(result.GetValue(InternalReportInstallSuccessCommandDefinition.Argument)); var filter = new TelemetryFilter(Sha256Hasher.HashWithNormalizedCasing); foreach (var e in filter.Filter(new InstallerSuccessReport(exeName))) diff --git a/src/Cli/dotnet/Commands/Hidden/InternalReportInstallSuccess/InternalReportInstallSuccessCommandDefinition.cs b/src/Cli/dotnet/Commands/Hidden/InternalReportInstallSuccess/InternalReportInstallSuccessCommandDefinition.cs new file mode 100644 index 000000000000..23736c5f4ac8 --- /dev/null +++ b/src/Cli/dotnet/Commands/Hidden/InternalReportInstallSuccess/InternalReportInstallSuccessCommandDefinition.cs @@ -0,0 +1,23 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.CommandLine; + +namespace Microsoft.DotNet.Cli.Commands.Hidden.InternalReportInstallSuccess; + +internal static class InternalReportInstallSuccessCommandDefinition +{ + public static readonly Argument Argument = new("internal-reportinstallsuccess-arg"); + + public static Command Create() + { + Command command = new("internal-reportinstallsuccess") + { + Hidden = true + }; + + command.Arguments.Add(Argument); + + return command; + } +} diff --git a/src/Cli/dotnet/Commands/Hidden/InternalReportInstallSuccess/InternalReportInstallSuccessCommandParser.cs b/src/Cli/dotnet/Commands/Hidden/InternalReportInstallSuccess/InternalReportInstallSuccessCommandParser.cs index 5a5ea89397e8..e174c010d04b 100644 --- a/src/Cli/dotnet/Commands/Hidden/InternalReportInstallSuccess/InternalReportInstallSuccessCommandParser.cs +++ b/src/Cli/dotnet/Commands/Hidden/InternalReportInstallSuccess/InternalReportInstallSuccessCommandParser.cs @@ -1,34 +1,22 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#nullable disable - using System.CommandLine; namespace Microsoft.DotNet.Cli.Commands.Hidden.InternalReportInstallSuccess; internal static class InternalReportInstallSuccessCommandParser { - public static readonly Argument Argument = new("internal-reportinstallsuccess-arg"); - - private static readonly Command Command = ConstructCommand(); + private static readonly Command Command = SetAction(InternalReportInstallSuccessCommandDefinition.Create()); public static Command GetCommand() { return Command; } - private static Command ConstructCommand() + private static Command SetAction(Command command) { - Command command = new("internal-reportinstallsuccess") - { - Hidden = true - }; - - command.Arguments.Add(Argument); - command.SetAction(InternalReportInstallSuccessCommand.Run); - return command; } } diff --git a/src/Cli/dotnet/Commands/Hidden/List/ListCommandDefinition.cs b/src/Cli/dotnet/Commands/Hidden/List/ListCommandDefinition.cs new file mode 100644 index 000000000000..3ccdf8684a76 --- /dev/null +++ b/src/Cli/dotnet/Commands/Hidden/List/ListCommandDefinition.cs @@ -0,0 +1,37 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.CommandLine; +using Microsoft.DotNet.Cli.Commands.Hidden.List.Package; +using Microsoft.DotNet.Cli.Commands.Hidden.List.Reference; + +namespace Microsoft.DotNet.Cli.Commands.Hidden.List; + +internal static class ListCommandDefinition +{ + public static readonly string DocsLink = "https://aka.ms/dotnet-list"; + + public static readonly Argument SlnOrProjectArgument = CreateSlnOrProjectArgument(CliStrings.SolutionOrProjectArgumentName, CliStrings.SolutionOrProjectArgumentDescription); + + internal static Argument CreateSlnOrProjectArgument(string name, string description) + => new Argument(name) + { + Description = description, + Arity = ArgumentArity.ZeroOrOne + }.DefaultToCurrentDirectory(); + + public static Command Create() + { + var command = new Command("list", CliCommandStrings.NetListCommand) + { + Hidden = true, + DocsLink = DocsLink + }; + + command.Arguments.Add(SlnOrProjectArgument); + command.Subcommands.Add(ListPackageCommandDefinition.Create()); + command.Subcommands.Add(ListReferenceCommandDefinition.Create()); + + return command; + } +} diff --git a/src/Cli/dotnet/Commands/Hidden/List/ListCommandParser.cs b/src/Cli/dotnet/Commands/Hidden/List/ListCommandParser.cs index f471c20b62d1..8d10c7e12f1c 100644 --- a/src/Cli/dotnet/Commands/Hidden/List/ListCommandParser.cs +++ b/src/Cli/dotnet/Commands/Hidden/List/ListCommandParser.cs @@ -1,49 +1,30 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#nullable disable - using System.CommandLine; using Microsoft.DotNet.Cli.Commands.Hidden.List.Package; using Microsoft.DotNet.Cli.Commands.Hidden.List.Reference; +using Microsoft.DotNet.Cli.Commands.Package.List; +using Microsoft.DotNet.Cli.Commands.Reference.List; using Microsoft.DotNet.Cli.Extensions; -using Microsoft.DotNet.Cli.CommandLine; namespace Microsoft.DotNet.Cli.Commands.Hidden.List; internal static class ListCommandParser { - public static readonly string DocsLink = "https://aka.ms/dotnet-list"; - - public static readonly Argument SlnOrProjectArgument = CreateSlnOrProjectArgument(CliStrings.SolutionOrProjectArgumentName, CliStrings.SolutionOrProjectArgumentDescription); - - internal static Argument CreateSlnOrProjectArgument(string name, string description) - => new Argument(name) - { - Description = description, - Arity = ArgumentArity.ZeroOrOne - }.DefaultToCurrentDirectory(); - - private static readonly Command Command = ConstructCommand(); + private static readonly Command Command = SetAction(ListCommandDefinition.Create()); public static Command GetCommand() { return Command; } - private static Command ConstructCommand() + private static Command SetAction(Command command) { - var command = new Command("list", CliCommandStrings.NetListCommand) - { - Hidden = true, - DocsLink = DocsLink - }; - - command.Arguments.Add(SlnOrProjectArgument); - command.Subcommands.Add(ListPackageCommandParser.GetCommand()); - command.Subcommands.Add(ListReferenceCommandParser.GetCommand()); + command.SetAction(parseResult => parseResult.HandleMissingCommand()); - command.SetAction((parseResult) => parseResult.HandleMissingCommand()); + command.Subcommands.Single(c => c.Name == ListPackageCommandDefinition.Name).SetAction((parseResult) => new PackageListCommand(parseResult).Execute()); + command.Subcommands.Single(c => c.Name == ListReferenceCommandDefinition.Name).SetAction((parseResult) => new ReferenceListCommand(parseResult).Execute()); return command; } diff --git a/src/Cli/dotnet/Commands/Hidden/List/Package/ListPackageCommandDefinition.cs b/src/Cli/dotnet/Commands/Hidden/List/Package/ListPackageCommandDefinition.cs new file mode 100644 index 000000000000..df28ebcda833 --- /dev/null +++ b/src/Cli/dotnet/Commands/Hidden/List/Package/ListPackageCommandDefinition.cs @@ -0,0 +1,35 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.CommandLine; +using Microsoft.DotNet.Cli.Commands.Package.List; + +namespace Microsoft.DotNet.Cli.Commands.Hidden.List.Package; + +internal static class ListPackageCommandDefinition +{ + public const string Name = "package"; + + public static Command Create() + { + Command command = new(Name, CliCommandStrings.PackageListAppFullName); + + command.Options.Add(PackageListCommandDefinition.VerbosityOption); + command.Options.Add(PackageListCommandDefinition.OutdatedOption); + command.Options.Add(PackageListCommandDefinition.DeprecatedOption); + command.Options.Add(PackageListCommandDefinition.VulnerableOption); + command.Options.Add(PackageListCommandDefinition.FrameworkOption); + command.Options.Add(PackageListCommandDefinition.TransitiveOption); + command.Options.Add(PackageListCommandDefinition.PrereleaseOption); + command.Options.Add(PackageListCommandDefinition.HighestPatchOption); + command.Options.Add(PackageListCommandDefinition.HighestMinorOption); + command.Options.Add(PackageListCommandDefinition.ConfigOption); + command.Options.Add(PackageListCommandDefinition.SourceOption); + command.Options.Add(PackageListCommandDefinition.InteractiveOption); + command.Options.Add(PackageListCommandDefinition.FormatOption); + command.Options.Add(PackageListCommandDefinition.OutputVersionOption); + command.Options.Add(PackageListCommandDefinition.NoRestore); + + return command; + } +} diff --git a/src/Cli/dotnet/Commands/Hidden/List/Package/ListPackageCommandParser.cs b/src/Cli/dotnet/Commands/Hidden/List/Package/ListPackageCommandParser.cs deleted file mode 100644 index a1d8c613f289..000000000000 --- a/src/Cli/dotnet/Commands/Hidden/List/Package/ListPackageCommandParser.cs +++ /dev/null @@ -1,44 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -#nullable disable - -using System.CommandLine; -using Microsoft.DotNet.Cli.Commands.Package.List; - -namespace Microsoft.DotNet.Cli.Commands.Hidden.List.Package; - -internal static class ListPackageCommandParser -{ - private static readonly Command Command = ConstructCommand(); - - public static Command GetCommand() - { - return Command; - } - - private static Command ConstructCommand() - { - Command command = new("package", CliCommandStrings.PackageListAppFullName); - - command.Options.Add(PackageListCommandParser.VerbosityOption); - command.Options.Add(PackageListCommandParser.OutdatedOption); - command.Options.Add(PackageListCommandParser.DeprecatedOption); - command.Options.Add(PackageListCommandParser.VulnerableOption); - command.Options.Add(PackageListCommandParser.FrameworkOption); - command.Options.Add(PackageListCommandParser.TransitiveOption); - command.Options.Add(PackageListCommandParser.PrereleaseOption); - command.Options.Add(PackageListCommandParser.HighestPatchOption); - command.Options.Add(PackageListCommandParser.HighestMinorOption); - command.Options.Add(PackageListCommandParser.ConfigOption); - command.Options.Add(PackageListCommandParser.SourceOption); - command.Options.Add(PackageListCommandParser.InteractiveOption); - command.Options.Add(PackageListCommandParser.FormatOption); - command.Options.Add(PackageListCommandParser.OutputVersionOption); - command.Options.Add(PackageListCommandParser.NoRestore); - - command.SetAction((parseResult) => new PackageListCommand(parseResult).Execute()); - - return command; - } -} diff --git a/src/Cli/dotnet/Commands/Hidden/List/Reference/ListReferenceCommandParser.cs b/src/Cli/dotnet/Commands/Hidden/List/Reference/ListReferenceCommandDefinition.cs similarity index 52% rename from src/Cli/dotnet/Commands/Hidden/List/Reference/ListReferenceCommandParser.cs rename to src/Cli/dotnet/Commands/Hidden/List/Reference/ListReferenceCommandDefinition.cs index 6401f55aa0b9..75dd65165055 100644 --- a/src/Cli/dotnet/Commands/Hidden/List/Reference/ListReferenceCommandParser.cs +++ b/src/Cli/dotnet/Commands/Hidden/List/Reference/ListReferenceCommandDefinition.cs @@ -1,32 +1,23 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#nullable disable - using System.CommandLine; using Microsoft.DotNet.Cli.Commands.Reference.List; namespace Microsoft.DotNet.Cli.Commands.Hidden.List.Reference; -internal static class ListReferenceCommandParser +internal static class ListReferenceCommandDefinition { - public static readonly Argument Argument = new("argument") { Arity = ArgumentArity.ZeroOrOne, Hidden = true }; + public const string Name = "reference"; - private static readonly Command Command = ConstructCommand(); - - public static Command GetCommand() - { - return Command; - } + public static readonly Argument Argument = new("argument") { Arity = ArgumentArity.ZeroOrOne, Hidden = true }; - private static Command ConstructCommand() + public static Command Create() { - var command = new Command("reference", CliCommandStrings.ReferenceListAppFullName); + var command = new Command(Name, CliCommandStrings.ReferenceListAppFullName); command.Arguments.Add(Argument); - command.SetAction((parseResult) => new ReferenceListCommand(parseResult).Execute()); - return command; } } diff --git a/src/Cli/dotnet/Commands/Hidden/Parse/ParseCommandDefinition.cs b/src/Cli/dotnet/Commands/Hidden/Parse/ParseCommandDefinition.cs new file mode 100644 index 000000000000..419e1ddd4bc7 --- /dev/null +++ b/src/Cli/dotnet/Commands/Hidden/Parse/ParseCommandDefinition.cs @@ -0,0 +1,15 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.CommandLine; + +namespace Microsoft.DotNet.Cli.Commands.Hidden.Parse; + +internal static class ParseCommandDefinition +{ + public static Command Create() + => new("parse") + { + Hidden = true + }; +} diff --git a/src/Cli/dotnet/Commands/Hidden/Parse/ParseCommandParser.cs b/src/Cli/dotnet/Commands/Hidden/Parse/ParseCommandParser.cs index 14ef8cc1a26b..4049bde2df92 100644 --- a/src/Cli/dotnet/Commands/Hidden/Parse/ParseCommandParser.cs +++ b/src/Cli/dotnet/Commands/Hidden/Parse/ParseCommandParser.cs @@ -1,30 +1,22 @@ // Licensed to the .NET Foundation under one or more agreements. // The .NET Foundation licenses this file to you under the MIT license. -#nullable disable - using System.CommandLine; namespace Microsoft.DotNet.Cli.Commands.Hidden.Parse; internal static class ParseCommandParser { - private static readonly Command Command = ConstructCommand(); + private static readonly Command Command = SetAction(ParseCommandDefinition.Create()); public static Command GetCommand() { return Command; } - private static Command ConstructCommand() + private static Command SetAction(Command command) { - var command = new Command("parse") - { - Hidden = true - }; - command.SetAction(ParseCommand.Run); - return command; } } diff --git a/src/Cli/dotnet/Commands/Hidden/Remove/Package/RemovePackageCommandDefinition.cs b/src/Cli/dotnet/Commands/Hidden/Remove/Package/RemovePackageCommandDefinition.cs new file mode 100644 index 000000000000..2739d53e1b3c --- /dev/null +++ b/src/Cli/dotnet/Commands/Hidden/Remove/Package/RemovePackageCommandDefinition.cs @@ -0,0 +1,22 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.CommandLine; +using Microsoft.DotNet.Cli.Commands.Package.Remove; + +namespace Microsoft.DotNet.Cli.Commands.Hidden.Remove.Package; + +internal static class RemovePackageCommandDefinition +{ + public const string Name = "package"; + + public static Command Create() + { + var command = new Command(Name, CliCommandStrings.PackageRemoveAppFullName); + + command.Arguments.Add(PackageRemoveCommandDefinition.CmdPackageArgument); + command.Options.Add(PackageRemoveCommandDefinition.InteractiveOption); + + return command; + } +} diff --git a/src/Cli/dotnet/Commands/Hidden/Remove/Package/RemovePackageCommandParser.cs b/src/Cli/dotnet/Commands/Hidden/Remove/Package/RemovePackageCommandParser.cs deleted file mode 100644 index 51eb32535cc2..000000000000 --- a/src/Cli/dotnet/Commands/Hidden/Remove/Package/RemovePackageCommandParser.cs +++ /dev/null @@ -1,29 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -using System.CommandLine; -using Microsoft.DotNet.Cli.Commands.Package.Remove; - -namespace Microsoft.DotNet.Cli.Commands.Hidden.Remove.Package; - -internal static class RemovePackageCommandParser -{ - private static readonly Command Command = ConstructCommand(); - - public static Command GetCommand() - { - return Command; - } - - private static Command ConstructCommand() - { - var command = new Command("package", CliCommandStrings.PackageRemoveAppFullName); - - command.Arguments.Add(PackageRemoveCommandParser.CmdPackageArgument); - command.Options.Add(PackageRemoveCommandParser.InteractiveOption); - - command.SetAction((parseResult) => new PackageRemoveCommand(parseResult).Execute()); - - return command; - } -} diff --git a/src/Cli/dotnet/Commands/Hidden/Remove/Reference/RemoveReferenceCommandDefinition.cs b/src/Cli/dotnet/Commands/Hidden/Remove/Reference/RemoveReferenceCommandDefinition.cs new file mode 100644 index 000000000000..c792f9f6eac3 --- /dev/null +++ b/src/Cli/dotnet/Commands/Hidden/Remove/Reference/RemoveReferenceCommandDefinition.cs @@ -0,0 +1,22 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.CommandLine; +using Microsoft.DotNet.Cli.Commands.Reference.Remove; + +namespace Microsoft.DotNet.Cli.Commands.Hidden.Remove.Reference; + +internal static class RemoveReferenceCommandDefinition +{ + public const string Name = "reference"; + + public static Command Create() + { + var command = new Command(Name, CliCommandStrings.ReferenceRemoveAppFullName); + + command.Arguments.Add(ReferenceRemoveCommandDefinition.ProjectPathArgument); + command.Options.Add(ReferenceRemoveCommandDefinition.FrameworkOption); + + return command; + } +} diff --git a/src/Cli/dotnet/Commands/Hidden/Remove/Reference/RemoveReferenceCommandParser.cs b/src/Cli/dotnet/Commands/Hidden/Remove/Reference/RemoveReferenceCommandParser.cs deleted file mode 100644 index d0ab77efebbf..000000000000 --- a/src/Cli/dotnet/Commands/Hidden/Remove/Reference/RemoveReferenceCommandParser.cs +++ /dev/null @@ -1,31 +0,0 @@ -// Licensed to the .NET Foundation under one or more agreements. -// The .NET Foundation licenses this file to you under the MIT license. - -#nullable disable - -using System.CommandLine; -using Microsoft.DotNet.Cli.Commands.Reference.Remove; - -namespace Microsoft.DotNet.Cli.Commands.Hidden.Remove.Reference; - -internal static class RemoveReferenceCommandParser -{ - private static readonly Command Command = ConstructCommand(); - - public static Command GetCommand() - { - return Command; - } - - private static Command ConstructCommand() - { - var command = new Command("reference", CliCommandStrings.ReferenceRemoveAppFullName); - - command.Arguments.Add(ReferenceRemoveCommandParser.ProjectPathArgument); - command.Options.Add(ReferenceRemoveCommandParser.FrameworkOption); - - command.SetAction((parseResult) => new ReferenceRemoveCommand(parseResult).Execute()); - - return command; - } -} diff --git a/src/Cli/dotnet/Commands/Hidden/Remove/RemoveCommandDefinition.cs b/src/Cli/dotnet/Commands/Hidden/Remove/RemoveCommandDefinition.cs new file mode 100644 index 000000000000..c74619c928d1 --- /dev/null +++ b/src/Cli/dotnet/Commands/Hidden/Remove/RemoveCommandDefinition.cs @@ -0,0 +1,29 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.CommandLine; +using Microsoft.DotNet.Cli.Commands.Hidden.Remove.Package; +using Microsoft.DotNet.Cli.Commands.Hidden.Remove.Reference; +using Microsoft.DotNet.Cli.Commands.Package; + +namespace Microsoft.DotNet.Cli.Commands.Hidden.Remove; + +internal static class RemoveCommandDefinition +{ + public static readonly string DocsLink = "https://aka.ms/dotnet-remove"; + + public static Command Create() + { + var command = new Command("remove", CliCommandStrings.NetRemoveCommand) + { + Hidden = true, + DocsLink = DocsLink + }; + + command.Arguments.Add(PackageCommandDefinition.ProjectOrFileArgument); + command.Subcommands.Add(RemovePackageCommandDefinition.Create()); + command.Subcommands.Add(RemoveReferenceCommandDefinition.Create()); + + return command; + } +} diff --git a/src/Cli/dotnet/Commands/Hidden/Remove/RemoveCommandParser.cs b/src/Cli/dotnet/Commands/Hidden/Remove/RemoveCommandParser.cs index a91ed69540f8..b56556e0d11b 100644 --- a/src/Cli/dotnet/Commands/Hidden/Remove/RemoveCommandParser.cs +++ b/src/Cli/dotnet/Commands/Hidden/Remove/RemoveCommandParser.cs @@ -3,38 +3,28 @@ using System.CommandLine; using Microsoft.DotNet.Cli.Commands.Hidden.Remove.Package; -using Microsoft.DotNet.Cli.Commands.Hidden.Remove.Reference; -using Microsoft.DotNet.Cli.Commands.Package; -using Microsoft.DotNet.Cli.CommandLine; +using Microsoft.DotNet.Cli.Commands.Package.Remove; +using Microsoft.DotNet.Cli.Commands.Reference.Remove; using Microsoft.DotNet.Cli.Extensions; namespace Microsoft.DotNet.Cli.Commands.Hidden.Remove; internal static class RemoveCommandParser { - public static readonly string DocsLink = "https://aka.ms/dotnet-remove"; - - private static readonly Command Command = ConstructCommand(); + private static readonly Command Command = ConfigureCommand(RemoveCommandDefinition.Create()); public static Command GetCommand() { return Command; } - private static Command ConstructCommand() + private static Command ConfigureCommand(Command command) { - var command = new Command("remove", CliCommandStrings.NetRemoveCommand) - { - Hidden = true, - DocsLink = DocsLink - }; - - command.Arguments.Add(PackageCommandParser.ProjectOrFileArgument); - command.Subcommands.Add(RemovePackageCommandParser.GetCommand()); - command.Subcommands.Add(RemoveReferenceCommandParser.GetCommand()); - command.SetAction((parseResult) => parseResult.HandleMissingCommand()); + command.Subcommands.Single(c => c.Name == RemovePackageCommandDefinition.Name).SetAction((parseResult) => new PackageRemoveCommand(parseResult).Execute()); + command.Subcommands.Single(c => c.Name == RemovePackageCommandDefinition.Name).SetAction((parseResult) => new ReferenceRemoveCommand(parseResult).Execute()); + return command; } } diff --git a/src/Cli/dotnet/Commands/MSBuild/MSBuildCommand.cs b/src/Cli/dotnet/Commands/MSBuild/MSBuildCommand.cs index 7e28945067a3..ce0be333fbdc 100644 --- a/src/Cli/dotnet/Commands/MSBuild/MSBuildCommand.cs +++ b/src/Cli/dotnet/Commands/MSBuild/MSBuildCommand.cs @@ -15,7 +15,7 @@ public class MSBuildCommand( [.. msbuildArgs], CommonOptions.PropertiesOption, CommonOptions.RestorePropertiesOption, - MSBuildCommandParser.TargetOption, + MSBuildCommandDefinition.TargetOption, CommonOptions.VerbosityOption(), // We set the no-logo option to false here to ensure that by default the logo is shown for this command. // This is different from other commands that default to hiding the logo - but this command is meant to mimic @@ -32,7 +32,7 @@ public static MSBuildCommand FromArgs(string[] args, string? msbuildPath = null) public static MSBuildCommand FromParseResult(ParseResult parseResult, string? msbuildPath = null) { var msbuildArgs = new List(); - msbuildArgs.AddRange(parseResult.GetValue(MSBuildCommandParser.Arguments) ?? []); + msbuildArgs.AddRange(parseResult.GetValue(MSBuildCommandDefinition.Arguments) ?? []); msbuildArgs.AddRange(parseResult.OptionValuesToBeForwarded(MSBuildCommandParser.GetCommand())); MSBuildCommand command = new( diff --git a/src/Cli/dotnet/Commands/MSBuild/MSBuildCommandDefinition.cs b/src/Cli/dotnet/Commands/MSBuild/MSBuildCommandDefinition.cs new file mode 100644 index 000000000000..43a48acd4459 --- /dev/null +++ b/src/Cli/dotnet/Commands/MSBuild/MSBuildCommandDefinition.cs @@ -0,0 +1,29 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.CommandLine; +using Microsoft.DotNet.Cli.CommandLine; + +namespace Microsoft.DotNet.Cli.Commands.MSBuild; + +internal static class MSBuildCommandDefinition +{ + public static readonly string DocsLink = "https://aka.ms/dotnet-msbuild"; + + public static readonly Argument Arguments = new("arguments"); + public static readonly Option TargetOption = CommonOptions.MSBuildTargetOption(); + + public static Command Create() + { + var command = new Command("msbuild", CliCommandStrings.BuildAppFullName) + { + Arguments = { Arguments }, + DocsLink = DocsLink, + }; + + command.Options.Add(CommonOptions.DisableBuildServersOption); + command.Options.Add(TargetOption); + + return command; + } +} diff --git a/src/Cli/dotnet/Commands/MSBuild/MSBuildCommandParser.cs b/src/Cli/dotnet/Commands/MSBuild/MSBuildCommandParser.cs index 5893570d7be2..f5484ef3fe5f 100644 --- a/src/Cli/dotnet/Commands/MSBuild/MSBuildCommandParser.cs +++ b/src/Cli/dotnet/Commands/MSBuild/MSBuildCommandParser.cs @@ -2,36 +2,21 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.CommandLine; -using Microsoft.DotNet.Cli.CommandLine; namespace Microsoft.DotNet.Cli.Commands.MSBuild; internal static class MSBuildCommandParser { - public static readonly string DocsLink = "https://aka.ms/dotnet-msbuild"; - - public static readonly Argument Arguments = new("arguments"); - public static readonly Option TargetOption = CommonOptions.MSBuildTargetOption(); - - private static readonly Command Command = ConstructCommand(); + private static readonly Command Command = ConfigureCommand(MSBuildCommandDefinition.Create()); public static Command GetCommand() { return Command; } - private static Command ConstructCommand() + private static Command ConfigureCommand(Command command) { - var command = new Command("msbuild", CliCommandStrings.BuildAppFullName) - { - Arguments = { Arguments }, - DocsLink = DocsLink, - }; - - command.Options.Add(CommonOptions.DisableBuildServersOption); - command.Options.Add(TargetOption); command.SetAction(MSBuildCommand.Run); - return command; } } diff --git a/src/Cli/dotnet/Commands/New/DotnetCommandCallbacks.cs b/src/Cli/dotnet/Commands/New/DotnetCommandCallbacks.cs index 4aae2f0f037e..9de29fe2287e 100644 --- a/src/Cli/dotnet/Commands/New/DotnetCommandCallbacks.cs +++ b/src/Cli/dotnet/Commands/New/DotnetCommandCallbacks.cs @@ -19,7 +19,7 @@ internal static bool AddPackageReference(string projectPath, string packageName, IEnumerable commandArgs = ["add", projectPath, "package", packageName]; if (!string.IsNullOrWhiteSpace(version)) { - commandArgs = commandArgs.Append(PackageAddCommandParser.VersionOption.Name).Append(version); + commandArgs = commandArgs.Append(PackageAddCommandDefinition.VersionOption.Name).Append(version); } var addPackageReferenceCommand = new PackageAddCommand(AddCommandParser.GetCommand().Parse([.. commandArgs])); return addPackageReferenceCommand.Execute() == 0; @@ -48,12 +48,12 @@ internal static bool AddProjectsToSolution(string solutionPath, IReadOnlyList commandArgs = new[] { "solution", solutionPath, "add" }.Concat(projectsToAdd); if (!string.IsNullOrWhiteSpace(solutionFolder)) { - commandArgs = commandArgs.Append(SolutionAddCommandParser.SolutionFolderOption.Name).Append(solutionFolder); + commandArgs = commandArgs.Append(SolutionAddCommandDefinition.SolutionFolderOption.Name).Append(solutionFolder); } if (inRoot is true) { - commandArgs = commandArgs.Append(SolutionAddCommandParser.InRootOption.Name); + commandArgs = commandArgs.Append(SolutionAddCommandDefinition.InRootOption.Name); } var addProjectToSolutionCommand = new SolutionAddCommand(SolutionCommandParser.GetCommand().Parse([.. commandArgs])); return addProjectToSolutionCommand.Execute() == 0; diff --git a/src/Cli/dotnet/Commands/NuGet/NuGetCommandDefinition.cs b/src/Cli/dotnet/Commands/NuGet/NuGetCommandDefinition.cs new file mode 100644 index 000000000000..aedfc8c40898 --- /dev/null +++ b/src/Cli/dotnet/Commands/NuGet/NuGetCommandDefinition.cs @@ -0,0 +1,249 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.CommandLine; +using Microsoft.DotNet.Cli.CommandLine; +using NuGetWhyCommand = NuGet.CommandLine.XPlat.Commands.Why.WhyCommand; + +namespace Microsoft.DotNet.Cli.Commands.NuGet; + +// This parser is used for completion and telemetry. +// See https://github.com/NuGet/NuGet.Client for the actual implementation. +internal static class NuGetCommandDefinition +{ + public static readonly string DocsLink = "https://aka.ms/dotnet-nuget"; + + public static Command Create() + { + var command = new Command("nuget") + { + // some subcommands are not defined here and just forwarded to NuGet app + TreatUnmatchedTokensAsErrors = false, + DocsLink = DocsLink + }; + + command.Options.Add(new Option("--version") + { + Arity = ArgumentArity.Zero + }); + command.Options.Add(new Option("--verbosity", "-v")); + + command.Subcommands.Add(CreateDeleteCommand()); + command.Subcommands.Add(CreateLocalsCommand()); + command.Subcommands.Add(CreatePushCommand()); + command.Subcommands.Add(CreateVerifyCommand()); + command.Subcommands.Add(CreateTrustCommand()); + command.Subcommands.Add(CreateSignCommand()); + NuGetWhyCommand.GetWhyCommand(command); + + return command; + } + + private static Command CreateDeleteCommand() + { + Command deleteCommand = new("delete"); + deleteCommand.Arguments.Add(new Argument>("package-paths") { Arity = ArgumentArity.OneOrMore }); + deleteCommand.Options.Add(new Option("--force-english-output") + { + Arity = ArgumentArity.Zero + }); + deleteCommand.Options.Add(new Option("--source", "-s")); + deleteCommand.Options.Add(new Option("--non-interactive") + { + Arity = ArgumentArity.Zero + }); + deleteCommand.Options.Add(new Option("--api-key", "-k")); + deleteCommand.Options.Add(new Option("--no-service-endpoint") + { + Arity = ArgumentArity.Zero + }); + deleteCommand.Options.Add(CommonOptions.InteractiveOption()); + + return deleteCommand; + } + + private static Command CreateLocalsCommand() + { + Command localsCommand = new("locals"); + + Argument foldersArgument = new("folders"); + foldersArgument.AcceptOnlyFromAmong(["all", "http-cache", "global-packages", "plugins-cache", "temp"]); + + localsCommand.Arguments.Add(foldersArgument); + + localsCommand.Options.Add(new Option("--force-english-output") + { + Arity = ArgumentArity.Zero + }); + localsCommand.Options.Add(new Option("--clear", "-c") + { + Arity = ArgumentArity.Zero + }); + localsCommand.Options.Add(new Option("--list", "-l") + { + Arity = ArgumentArity.Zero + }); + + return localsCommand; + } + + private static Command CreatePushCommand() + { + Command pushCommand = new("push"); + + pushCommand.Arguments.Add(new Argument>("package-paths") { Arity = ArgumentArity.OneOrMore }); + + pushCommand.Options.Add(new Option("--force-english-output") + { + Arity = ArgumentArity.Zero + }); + pushCommand.Options.Add(new Option("--source", "-s")); + pushCommand.Options.Add(new Option("--symbol-source", "-ss")); + pushCommand.Options.Add(new Option("--timeout", "-t")); + pushCommand.Options.Add(new Option("--api-key", "-k")); + pushCommand.Options.Add(new Option("--symbol-api-key", "-sk")); + pushCommand.Options.Add(new Option("--disable-buffering", "-d") + { + Arity = ArgumentArity.Zero + }); + pushCommand.Options.Add(new Option("--no-symbols", "-n") + { + Arity = ArgumentArity.Zero + }); + pushCommand.Options.Add(new Option("--no-service-endpoint") + { + Arity = ArgumentArity.Zero + }); + pushCommand.Options.Add(CommonOptions.InteractiveOption()); + pushCommand.Options.Add(new Option("--skip-duplicate") + { + Arity = ArgumentArity.Zero + }); + pushCommand.Options.Add(new Option("--configfile")); + + return pushCommand; + } + + private static Command CreateVerifyCommand() + { + const string fingerprint = "--certificate-fingerprint"; + Command verifyCommand = new("verify"); + + verifyCommand.Arguments.Add(new Argument>("package-paths") { Arity = ArgumentArity.OneOrMore }); + + verifyCommand.Options.Add(new Option("--all") + { + Arity = ArgumentArity.Zero + }); + verifyCommand.Options.Add(new Option>(fingerprint) + .ForwardAsManyArgumentsEachPrefixedByOption(fingerprint) + .AllowSingleArgPerToken()); + verifyCommand.Options.Add(CommonOptions.VerbosityOption(Utils.VerbosityOptions.normal)); + + return verifyCommand; + } + + private static Command CreateTrustCommand() + { + Command trustCommand = new("trust"); + + Option allowUntrustedRoot = new("--allow-untrusted-root") + { + Arity = ArgumentArity.Zero + }; + Option owners = new("--owners"); + + trustCommand.Subcommands.Add(new Command("list")); + trustCommand.Subcommands.Add(CreateAuthorCommand()); + trustCommand.Subcommands.Add(CreateRepositoryCommand()); + trustCommand.Subcommands.Add(CreateSourceCommand()); + trustCommand.Subcommands.Add(CreateCertificateCommand()); + trustCommand.Subcommands.Add(CreateRemoveCommand()); + trustCommand.Subcommands.Add(CreateSyncCommand()); + + Option configFile = new("--configfile"); + + // now set global options for all nuget commands: configfile, verbosity + // as well as the standard NugetCommand.Run handler + + trustCommand.Options.Add(configFile); + trustCommand.Options.Add(CommonOptions.VerbosityOption(Utils.VerbosityOptions.normal)); + + foreach (var command in trustCommand.Subcommands) + { + command.Options.Add(configFile); + command.Options.Add(CommonOptions.VerbosityOption(Utils.VerbosityOptions.normal)); + } + + Command CreateAuthorCommand() => new("author") { + new Argument("NAME"), + new Argument("PACKAGE"), + allowUntrustedRoot, + }; + + Command CreateRepositoryCommand() => new("repository") { + new Argument("NAME"), + new Argument("PACKAGE"), + allowUntrustedRoot, + owners + }; + + Command CreateSourceCommand() => new("source") { + new Argument("NAME"), + owners, + new Option("--source-url"), + }; + + Command CreateCertificateCommand() + { + Option algorithm = new("--algorithm") + { + DefaultValueFactory = (_argResult) => "SHA256" + }; + algorithm.AcceptOnlyFromAmong("SHA256", "SHA384", "SHA512"); + + return new Command("certificate") { + new Argument("NAME"), + new Argument("FINGERPRINT"), + allowUntrustedRoot, + algorithm + }; + } + ; + + Command CreateRemoveCommand() => new("remove") { + new Argument("NAME"), + }; + + Command CreateSyncCommand() => new("sync") { + new Argument("NAME"), + }; + + return trustCommand; + } + + private static Command CreateSignCommand() + { + Command signCommand = new("sign"); + + signCommand.Arguments.Add(new Argument>("package-paths") { Arity = ArgumentArity.OneOrMore }); + + signCommand.Options.Add(new Option("--output", "-o")); + signCommand.Options.Add(new Option("--certificate-path")); + signCommand.Options.Add(new Option("--certificate-store-name")); + signCommand.Options.Add(new Option("--certificate-store-location")); + signCommand.Options.Add(new Option("--certificate-subject-name")); + signCommand.Options.Add(new Option("--certificate-fingerprint")); + signCommand.Options.Add(new Option("--certificate-password")); + signCommand.Options.Add(new Option("--hash-algorithm")); + signCommand.Options.Add(new Option("--timestamper")); + signCommand.Options.Add(new Option("--timestamp-hash-algorithm")); + signCommand.Options.Add(new Option("--overwrite") + { + Arity = ArgumentArity.Zero + }); + signCommand.Options.Add(CommonOptions.VerbosityOption(Utils.VerbosityOptions.normal)); + + return signCommand; + } +} diff --git a/src/Cli/dotnet/Commands/NuGet/NuGetCommandParser.cs b/src/Cli/dotnet/Commands/NuGet/NuGetCommandParser.cs index 814ad45f8544..89c0af1d020d 100644 --- a/src/Cli/dotnet/Commands/NuGet/NuGetCommandParser.cs +++ b/src/Cli/dotnet/Commands/NuGet/NuGetCommandParser.cs @@ -2,269 +2,28 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.CommandLine; -using Microsoft.DotNet.Cli.CommandLine; -using NuGetWhyCommand = NuGet.CommandLine.XPlat.Commands.Why.WhyCommand; namespace Microsoft.DotNet.Cli.Commands.NuGet; -// This parser is used for completion and telemetry. -// See https://github.com/NuGet/NuGet.Client for the actual implementation. internal static class NuGetCommandParser { - public static readonly string DocsLink = "https://aka.ms/dotnet-nuget"; - - private static readonly Command Command = ConstructCommand(); + private static readonly Command Command = SetAction(NuGetCommandDefinition.Create()); public static Command GetCommand() { return Command; } - private static Command ConstructCommand() + private static Command SetAction(Command command) { - var command = new Command("nuget") - { - // some subcommands are not defined here and just forwarded to NuGet app - TreatUnmatchedTokensAsErrors = false, - DocsLink = DocsLink - }; - - command.Options.Add(new Option("--version") - { - Arity = ArgumentArity.Zero - }); - command.Options.Add(new Option("--verbosity", "-v")); - - command.Subcommands.Add(GetDeleteCommand()); - command.Subcommands.Add(GetLocalsCommand()); - command.Subcommands.Add(GetPushCommand()); - command.Subcommands.Add(GetVerifyCommand()); - command.Subcommands.Add(GetTrustCommand()); - command.Subcommands.Add(GetSignCommand()); - NuGetWhyCommand.GetWhyCommand(command); - command.SetAction(NuGetCommand.Run); - return command; - } - - private static Command GetDeleteCommand() - { - Command deleteCommand = new("delete"); - deleteCommand.Arguments.Add(new Argument>("package-paths") { Arity = ArgumentArity.OneOrMore }); - deleteCommand.Options.Add(new Option("--force-english-output") - { - Arity = ArgumentArity.Zero - }); - deleteCommand.Options.Add(new Option("--source", "-s")); - deleteCommand.Options.Add(new Option("--non-interactive") - { - Arity = ArgumentArity.Zero - }); - deleteCommand.Options.Add(new Option("--api-key", "-k")); - deleteCommand.Options.Add(new Option("--no-service-endpoint") - { - Arity = ArgumentArity.Zero - }); - deleteCommand.Options.Add(CommonOptions.InteractiveOption()); - - deleteCommand.SetAction(NuGetCommand.Run); - - return deleteCommand; - } - - private static Command GetLocalsCommand() - { - Command localsCommand = new("locals"); - - Argument foldersArgument = new("folders"); - foldersArgument.AcceptOnlyFromAmong(["all", "http-cache", "global-packages", "plugins-cache", "temp"]); - - localsCommand.Arguments.Add(foldersArgument); - - localsCommand.Options.Add(new Option("--force-english-output") - { - Arity = ArgumentArity.Zero - }); - localsCommand.Options.Add(new Option("--clear", "-c") + foreach (var subcommand in command.Subcommands) { - Arity = ArgumentArity.Zero - }); - localsCommand.Options.Add(new Option("--list", "-l") - { - Arity = ArgumentArity.Zero - }); - - localsCommand.SetAction(NuGetCommand.Run); - - return localsCommand; - } - - private static Command GetPushCommand() - { - Command pushCommand = new("push"); - - pushCommand.Arguments.Add(new Argument>("package-paths") { Arity = ArgumentArity.OneOrMore }); - - pushCommand.Options.Add(new Option("--force-english-output") - { - Arity = ArgumentArity.Zero - }); - pushCommand.Options.Add(new Option("--source", "-s")); - pushCommand.Options.Add(new Option("--symbol-source", "-ss")); - pushCommand.Options.Add(new Option("--timeout", "-t")); - pushCommand.Options.Add(new Option("--api-key", "-k")); - pushCommand.Options.Add(new Option("--symbol-api-key", "-sk")); - pushCommand.Options.Add(new Option("--disable-buffering", "-d") - { - Arity = ArgumentArity.Zero - }); - pushCommand.Options.Add(new Option("--no-symbols", "-n") - { - Arity = ArgumentArity.Zero - }); - pushCommand.Options.Add(new Option("--no-service-endpoint") - { - Arity = ArgumentArity.Zero - }); - pushCommand.Options.Add(CommonOptions.InteractiveOption()); - pushCommand.Options.Add(new Option("--skip-duplicate") - { - Arity = ArgumentArity.Zero - }); - pushCommand.Options.Add(new Option("--configfile")); - - pushCommand.SetAction(NuGetCommand.Run); - - return pushCommand; - } - - private static Command GetVerifyCommand() - { - const string fingerprint = "--certificate-fingerprint"; - Command verifyCommand = new("verify"); - - verifyCommand.Arguments.Add(new Argument>("package-paths") { Arity = ArgumentArity.OneOrMore }); - - verifyCommand.Options.Add(new Option("--all") - { - Arity = ArgumentArity.Zero - }); - verifyCommand.Options.Add(new Option>(fingerprint) - .ForwardAsManyArgumentsEachPrefixedByOption(fingerprint) - .AllowSingleArgPerToken()); - verifyCommand.Options.Add(CommonOptions.VerbosityOption(Utils.VerbosityOptions.normal)); - - verifyCommand.SetAction(NuGetCommand.Run); - - return verifyCommand; - } - - private static Command GetTrustCommand() - { - Command trustCommand = new("trust"); - - Option allowUntrustedRoot = new("--allow-untrusted-root") - { - Arity = ArgumentArity.Zero - }; - Option owners = new("--owners"); - - trustCommand.Subcommands.Add(new Command("list")); - trustCommand.Subcommands.Add(AuthorCommand()); - trustCommand.Subcommands.Add(RepositoryCommand()); - trustCommand.Subcommands.Add(SourceCommand()); - trustCommand.Subcommands.Add(CertificateCommand()); - trustCommand.Subcommands.Add(RemoveCommand()); - trustCommand.Subcommands.Add(SyncCommand()); - - Option configFile = new("--configfile"); - - // now set global options for all nuget commands: configfile, verbosity - // as well as the standard NugetCommand.Run handler - - trustCommand.Options.Add(configFile); - trustCommand.Options.Add(CommonOptions.VerbosityOption(Utils.VerbosityOptions.normal)); - trustCommand.SetAction(NuGetCommand.Run); - - foreach (var command in trustCommand.Subcommands) - { - command.Options.Add(configFile); - command.Options.Add(CommonOptions.VerbosityOption(Utils.VerbosityOptions.normal)); - command.SetAction(NuGetCommand.Run); + subcommand.SetAction(NuGetCommand.Run); } - Command AuthorCommand() => new("author") { - new Argument("NAME"), - new Argument("PACKAGE"), - allowUntrustedRoot, - }; - - Command RepositoryCommand() => new("repository") { - new Argument("NAME"), - new Argument("PACKAGE"), - allowUntrustedRoot, - owners - }; - - Command SourceCommand() => new("source") { - new Argument("NAME"), - owners, - new Option("--source-url"), - }; - - Command CertificateCommand() - { - Option algorithm = new("--algorithm") - { - DefaultValueFactory = (_argResult) => "SHA256" - }; - algorithm.AcceptOnlyFromAmong("SHA256", "SHA384", "SHA512"); - - return new Command("certificate") { - new Argument("NAME"), - new Argument("FINGERPRINT"), - allowUntrustedRoot, - algorithm - }; - } - ; - - Command RemoveCommand() => new("remove") { - new Argument("NAME"), - }; - - Command SyncCommand() => new("sync") { - new Argument("NAME"), - }; - - return trustCommand; - } - - private static Command GetSignCommand() - { - Command signCommand = new("sign"); - - signCommand.Arguments.Add(new Argument>("package-paths") { Arity = ArgumentArity.OneOrMore }); - - signCommand.Options.Add(new Option("--output", "-o")); - signCommand.Options.Add(new Option("--certificate-path")); - signCommand.Options.Add(new Option("--certificate-store-name")); - signCommand.Options.Add(new Option("--certificate-store-location")); - signCommand.Options.Add(new Option("--certificate-subject-name")); - signCommand.Options.Add(new Option("--certificate-fingerprint")); - signCommand.Options.Add(new Option("--certificate-password")); - signCommand.Options.Add(new Option("--hash-algorithm")); - signCommand.Options.Add(new Option("--timestamper")); - signCommand.Options.Add(new Option("--timestamp-hash-algorithm")); - signCommand.Options.Add(new Option("--overwrite") - { - Arity = ArgumentArity.Zero - }); - signCommand.Options.Add(CommonOptions.VerbosityOption(Utils.VerbosityOptions.normal)); - - signCommand.SetAction(NuGetCommand.Run); - - return signCommand; + return command; } } + diff --git a/src/Cli/dotnet/Commands/Pack/PackCommand.cs b/src/Cli/dotnet/Commands/Pack/PackCommand.cs index 9f31bfa145fe..fe36ed860367 100644 --- a/src/Cli/dotnet/Commands/Pack/PackCommand.cs +++ b/src/Cli/dotnet/Commands/Pack/PackCommand.cs @@ -31,17 +31,17 @@ public static CommandBase FromArgs(string[] args, string? msbuildPath = null) public static CommandBase FromParseResult(ParseResult parseResult, string? msbuildPath = null) { - var args = parseResult.GetValue(PackCommandParser.SlnOrProjectOrFileArgument) ?? []; + var args = parseResult.GetValue(PackCommandDefinition.SlnOrProjectOrFileArgument) ?? []; LoggerUtility.SeparateBinLogArguments(args, out var binLogArgs, out var nonBinLogArgs); - bool noBuild = parseResult.HasOption(PackCommandParser.NoBuildOption); + bool noBuild = parseResult.HasOption(PackCommandDefinition.NoBuildOption); - bool noRestore = noBuild || parseResult.HasOption(PackCommandParser.NoRestoreOption); + bool noRestore = noBuild || parseResult.HasOption(PackCommandDefinition.NoRestoreOption); return CommandFactory.CreateVirtualOrPhysicalCommand( PackCommandParser.GetCommand(), - PackCommandParser.SlnOrProjectOrFileArgument, + PackCommandDefinition.SlnOrProjectOrFileArgument, (msbuildArgs, appFilePath) => new VirtualProjectBuildingCommand( entryPointFileFullPath: Path.GetFullPath(appFilePath), msbuildArgs: msbuildArgs) @@ -58,9 +58,9 @@ public static CommandBase FromParseResult(ParseResult parseResult, string? msbui [ CommonOptions.PropertiesOption, CommonOptions.RestorePropertiesOption, - PackCommandParser.TargetOption, - PackCommandParser.VerbosityOption, - PackCommandParser.NoLogoOption + PackCommandDefinition.TargetOption, + PackCommandDefinition.VerbosityOption, + PackCommandDefinition.NoLogoOption ], parseResult, msbuildPath, @@ -69,7 +69,7 @@ public static CommandBase FromParseResult(ParseResult parseResult, string? msbui ReleasePropertyProjectLocator projectLocator = new(parseResult, MSBuildPropertyNames.PACK_RELEASE, new ReleasePropertyProjectLocator.DependentCommandOptions( nonBinLogArgs, - parseResult.HasOption(PackCommandParser.ConfigurationOption) ? parseResult.GetValue(PackCommandParser.ConfigurationOption) : null + parseResult.HasOption(PackCommandDefinition.ConfigurationOption) ? parseResult.GetValue(PackCommandDefinition.ConfigurationOption) : null ) ); return msbuildArgs.CloneWithAdditionalProperties(projectLocator.GetCustomDefaultConfigurationValueIfSpecified()); @@ -90,22 +90,22 @@ private static LogLevel MappingVerbosityToNugetLogLevel(VerbosityOptions? verbos public static int RunPackCommand(ParseResult parseResult) { - var args = parseResult.GetValue(PackCommandParser.SlnOrProjectOrFileArgument)?.ToList() ?? new List(); + var args = parseResult.GetValue(PackCommandDefinition.SlnOrProjectOrFileArgument)?.ToList() ?? new List(); if (args.Count != 1) { - Console.Error.WriteLine(CliStrings.PackCmd_OneNuspecAllowed); + Console.Error.WriteLine(CliStrings.PackCmd_OneNuspecAllowed); return 1; } var nuspecPath = args[0]; var packArgs = new PackArgs() - { + { Logger = new NuGetConsoleLogger(), Exclude = new List(), - OutputDirectory = parseResult.GetValue(PackCommandParser.OutputOption), - LogLevel = MappingVerbosityToNugetLogLevel(parseResult.GetValue(BuildCommandParser.VerbosityOption)), + OutputDirectory = parseResult.GetValue(PackCommandDefinition.OutputOption), + LogLevel = MappingVerbosityToNugetLogLevel(parseResult.GetValue(BuildCommandDefinition.VerbosityOption)), Arguments = [nuspecPath] }; @@ -117,11 +117,11 @@ public static int RunPackCommand(ParseResult parseResult) if (globalProperties != null) packArgs.Properties.AddRange(globalProperties); - var version = parseResult.GetValue(PackCommandParser.VersionOption); + var version = parseResult.GetValue(PackCommandDefinition.VersionOption); if (version != null) packArgs.Version = version.ToNormalizedString(); - var configuration = parseResult.GetValue(PackCommandParser.ConfigurationOption) ?? "Debug"; + var configuration = parseResult.GetValue(PackCommandDefinition.ConfigurationOption) ?? "Debug"; packArgs.Properties["configuration"] = configuration; var packCommandRunner = new PackCommandRunner(packArgs, null); @@ -135,7 +135,7 @@ public static int Run(ParseResult parseResult) parseResult.HandleDebugSwitch(); parseResult.ShowHelpOrErrorIfAppropriate(); - var args = parseResult.GetValue(PackCommandParser.SlnOrProjectOrFileArgument)?.ToList() ?? new List(); + var args = parseResult.GetValue(PackCommandDefinition.SlnOrProjectOrFileArgument)?.ToList() ?? new List(); if (args.Count > 0 && Path.GetExtension(args[0]).Equals(".nuspec", StringComparison.OrdinalIgnoreCase)) { diff --git a/src/Cli/dotnet/Commands/Pack/PackCommandDefinition.cs b/src/Cli/dotnet/Commands/Pack/PackCommandDefinition.cs new file mode 100644 index 000000000000..100b5e42cc39 --- /dev/null +++ b/src/Cli/dotnet/Commands/Pack/PackCommandDefinition.cs @@ -0,0 +1,114 @@ +// Licensed to the .NET Foundation under one or more agreements. +// The .NET Foundation licenses this file to you under the MIT license. + +using System.CommandLine; +using Microsoft.DotNet.Cli.CommandLine; +using Microsoft.DotNet.Cli.Commands.Build; +using Microsoft.DotNet.Cli.Commands.Restore; +using NuGet.Versioning; + +namespace Microsoft.DotNet.Cli.Commands.Pack; + +internal static class PackCommandDefinition +{ + public static readonly string DocsLink = "https://aka.ms/dotnet-pack"; + + public static readonly Argument SlnOrProjectOrFileArgument = new(CliStrings.SolutionOrProjectOrFileArgumentName) + { + Description = CliStrings.SolutionOrProjectOrFileArgumentDescription, + Arity = ArgumentArity.ZeroOrMore + }; + + public static readonly Option OutputOption = new Option("--output", "-o") + { + Description = CliCommandStrings.PackCmdOutputDirDescription, + HelpName = CliCommandStrings.PackCmdOutputDir + }.ForwardAsSingle(o => $"-property:PackageOutputPath={CommandDirectoryContext.GetFullPath(o)}"); + + public static readonly Option NoBuildOption = new Option("--no-build") + { + Description = CliCommandStrings.CmdNoBuildOptionDescription, + Arity = ArgumentArity.Zero + }.ForwardAs("-property:NoBuild=true"); + + public static readonly Option IncludeSymbolsOption = new Option("--include-symbols") + { + Description = CliCommandStrings.CmdIncludeSymbolsDescription, + Arity = ArgumentArity.Zero + }.ForwardAs("-property:IncludeSymbols=true"); + + public static readonly Option IncludeSourceOption = new Option("--include-source") + { + Description = CliCommandStrings.CmdIncludeSourceDescription, + Arity = ArgumentArity.Zero + }.ForwardAs("-property:IncludeSource=true"); + + public static readonly Option ServiceableOption = new Option("--serviceable", "-s") + { + Description = CliCommandStrings.CmdServiceableDescription, + Arity = ArgumentArity.Zero + }.ForwardAs("-property:Serviceable=true"); + + public static readonly Option NoLogoOption = CommonOptions.NoLogoOption(); + + public static readonly Option NoRestoreOption = CommonOptions.NoRestoreOption; + + public static readonly Option ConfigurationOption = CommonOptions.ConfigurationOption(CliCommandStrings.PackConfigurationOptionDescription); + + public static readonly Option TargetOption = CommonOptions.RequiredMSBuildTargetOption("Pack", [("_IsPacking", "true")]); + public static readonly Option VerbosityOption = BuildCommandDefinition.VerbosityOption; + + public static Option VersionOption = + new Option("--version") + { + Description = CliCommandStrings.PackCmdVersionDescription, + HelpName = CliCommandStrings.PackCmdVersion, + Arity = ArgumentArity.ExactlyOne, + CustomParser = r => + { + if (r.Tokens.Count == 0) + return null; + var value = r.Tokens[0].Value; + if (NuGetVersion.TryParse(value, out var version)) + return version; + r.AddError(string.Format(CliStrings.InvalidVersion, value)); + return null; + + } + }.ForwardAsSingle(o => $"--property:PackageVersion={o}"); + + public static Command Create() + { + var command = new Command("pack", CliCommandStrings.PackAppFullName) + { + DocsLink = DocsLink + }; + + command.Arguments.Add(SlnOrProjectOrFileArgument); + command.Options.Add(OutputOption); + command.Options.Add(CommonOptions.ArtifactsPathOption); + command.Options.Add(NoBuildOption); + command.Options.Add(IncludeSymbolsOption); + command.Options.Add(IncludeSourceOption); + command.Options.Add(ServiceableOption); + command.Options.Add(NoLogoOption); + command.Options.Add(CommonOptions.InteractiveMsBuildForwardOption); + command.Options.Add(NoRestoreOption); + command.Options.Add(VerbosityOption); + command.Options.Add(CommonOptions.VersionSuffixOption); + command.Options.Add(VersionOption); + command.Options.Add(ConfigurationOption); + command.Options.Add(CommonOptions.DisableBuildServersOption); + command.Options.Add(TargetOption); + command.Options.Add(CommonOptions.GetPropertyOption); + command.Options.Add(CommonOptions.GetItemOption); + command.Options.Add(CommonOptions.GetTargetResultOption); + command.Options.Add(CommonOptions.GetResultOutputFileOption); + + // Don't include runtime option because we want to include it specifically and allow the short version ("-r") to be used + RestoreCommandDefinition.AddImplicitRestoreOptions(command, includeRuntimeOption: false, includeNoDependenciesOption: true); + command.Options.Add(CommonOptions.RuntimeOption(CliCommandStrings.BuildRuntimeOptionDescription)); + + return command; + } +} diff --git a/src/Cli/dotnet/Commands/Pack/PackCommandParser.cs b/src/Cli/dotnet/Commands/Pack/PackCommandParser.cs index 23fc1ef58f69..577d3d2c9bb4 100644 --- a/src/Cli/dotnet/Commands/Pack/PackCommandParser.cs +++ b/src/Cli/dotnet/Commands/Pack/PackCommandParser.cs @@ -2,122 +2,21 @@ // The .NET Foundation licenses this file to you under the MIT license. using System.CommandLine; -using Microsoft.DotNet.Cli.CommandLine; -using Microsoft.DotNet.Cli.Commands.Build; -using Microsoft.DotNet.Cli.Commands.Restore; -using NuGet.Versioning; namespace Microsoft.DotNet.Cli.Commands.Pack; internal static class PackCommandParser { - public static readonly string DocsLink = "https://aka.ms/dotnet-pack"; - - public static readonly Argument SlnOrProjectOrFileArgument = new(CliStrings.SolutionOrProjectOrFileArgumentName) - { - Description = CliStrings.SolutionOrProjectOrFileArgumentDescription, - Arity = ArgumentArity.ZeroOrMore - }; - - public static readonly Option OutputOption = new Option("--output", "-o") - { - Description = CliCommandStrings.PackCmdOutputDirDescription, - HelpName = CliCommandStrings.PackCmdOutputDir - }.ForwardAsSingle(o => $"-property:PackageOutputPath={CommandDirectoryContext.GetFullPath(o)}"); - - public static readonly Option NoBuildOption = new Option("--no-build") - { - Description = CliCommandStrings.CmdNoBuildOptionDescription, - Arity = ArgumentArity.Zero - }.ForwardAs("-property:NoBuild=true"); - - public static readonly Option IncludeSymbolsOption = new Option("--include-symbols") - { - Description = CliCommandStrings.CmdIncludeSymbolsDescription, - Arity = ArgumentArity.Zero - }.ForwardAs("-property:IncludeSymbols=true"); - - public static readonly Option IncludeSourceOption = new Option("--include-source") - { - Description = CliCommandStrings.CmdIncludeSourceDescription, - Arity = ArgumentArity.Zero - }.ForwardAs("-property:IncludeSource=true"); - - public static readonly Option ServiceableOption = new Option("--serviceable", "-s") - { - Description = CliCommandStrings.CmdServiceableDescription, - Arity = ArgumentArity.Zero - }.ForwardAs("-property:Serviceable=true"); - - public static readonly Option NoLogoOption = CommonOptions.NoLogoOption(); - - public static readonly Option NoRestoreOption = CommonOptions.NoRestoreOption; - - public static readonly Option ConfigurationOption = CommonOptions.ConfigurationOption(CliCommandStrings.PackConfigurationOptionDescription); - - public static readonly Option TargetOption = CommonOptions.RequiredMSBuildTargetOption("Pack", [("_IsPacking", "true")]); - public static readonly Option VerbosityOption = BuildCommandParser.VerbosityOption; - - public static Option VersionOption = - new Option("--version") - { - Description = CliCommandStrings.PackCmdVersionDescription, - HelpName = CliCommandStrings.PackCmdVersion, - Arity = ArgumentArity.ExactlyOne, - CustomParser = r => - { - if (r.Tokens.Count == 0) - return null; - var value = r.Tokens[0].Value; - if (NuGetVersion.TryParse(value, out var version)) - return version; - r.AddError(string.Format(CliStrings.InvalidVersion, value)); - return null; - - } - }.ForwardAsSingle(o => $"--property:PackageVersion={o}"); - - private static readonly Command Command = ConstructCommand(); + private static readonly Command Command = SetAction(PackCommandDefinition.Create()); public static Command GetCommand() { return Command; } - private static Command ConstructCommand() + private static Command SetAction(Command command) { - var command = new Command("pack", CliCommandStrings.PackAppFullName) - { - DocsLink = DocsLink - }; - - command.Arguments.Add(SlnOrProjectOrFileArgument); - command.Options.Add(OutputOption); - command.Options.Add(CommonOptions.ArtifactsPathOption); - command.Options.Add(NoBuildOption); - command.Options.Add(IncludeSymbolsOption); - command.Options.Add(IncludeSourceOption); - command.Options.Add(ServiceableOption); - command.Options.Add(NoLogoOption); - command.Options.Add(CommonOptions.InteractiveMsBuildForwardOption); - command.Options.Add(NoRestoreOption); - command.Options.Add(VerbosityOption); - command.Options.Add(CommonOptions.VersionSuffixOption); - command.Options.Add(VersionOption); - command.Options.Add(ConfigurationOption); - command.Options.Add(CommonOptions.DisableBuildServersOption); - command.Options.Add(TargetOption); - command.Options.Add(CommonOptions.GetPropertyOption); - command.Options.Add(CommonOptions.GetItemOption); - command.Options.Add(CommonOptions.GetTargetResultOption); - command.Options.Add(CommonOptions.GetResultOutputFileOption); - - // Don't include runtime option because we want to include it specifically and allow the short version ("-r") to be used - RestoreCommandParser.AddImplicitRestoreOptions(command, includeRuntimeOption: false, includeNoDependenciesOption: true); - command.Options.Add(CommonOptions.RuntimeOption(CliCommandStrings.BuildRuntimeOptionDescription)); - command.SetAction(PackCommand.Run); - return command; } } diff --git a/src/Cli/dotnet/Commands/Package/Add/PackageAddCommand.cs b/src/Cli/dotnet/Commands/Package/Add/PackageAddCommand.cs index a463165828cb..4353668c6d6c 100644 --- a/src/Cli/dotnet/Commands/Package/Add/PackageAddCommand.cs +++ b/src/Cli/dotnet/Commands/Package/Add/PackageAddCommand.cs @@ -19,11 +19,11 @@ namespace Microsoft.DotNet.Cli.Commands.Package.Add; internal class PackageAddCommand(ParseResult parseResult) : CommandBase(parseResult) { - private readonly PackageIdentityWithRange _packageId = parseResult.GetValue(PackageAddCommandParser.CmdPackageArgument)!; + private readonly PackageIdentityWithRange _packageId = parseResult.GetValue(PackageAddCommandDefinition.CmdPackageArgument)!; public override int Execute() { - var (fileOrDirectory, allowedAppKinds) = PackageCommandParser.ProcessPathOptions(_parseResult); + var (fileOrDirectory, allowedAppKinds) = PackageCommandDefinition.ProcessPathOptions(_parseResult); if (allowedAppKinds.HasFlag(AppKinds.FileBased) && VirtualProjectBuildingCommand.IsValidEntryPointPath(fileOrDirectory)) { @@ -44,7 +44,7 @@ public override int Execute() var tempDgFilePath = string.Empty; - if (!_parseResult.GetValue(PackageAddCommandParser.NoRestoreOption)) + if (!_parseResult.GetValue(PackageAddCommandDefinition.NoRestoreOption)) { try @@ -134,7 +134,7 @@ private string[] TransformArgs(PackageIdentityWithRange packageId, string tempDg .OptionValuesToBeForwarded() .SelectMany(a => a.Split(' ', 2))); - if (_parseResult.GetValue(PackageAddCommandParser.NoRestoreOption)) + if (_parseResult.GetValue(PackageAddCommandDefinition.NoRestoreOption)) { args.Add("--no-restore"); } @@ -153,9 +153,9 @@ private int ExecuteForFileBasedApp(string path) // Check disallowed options. ReadOnlySpan