From 38a50579a2a217ddc2623dd950662f9c15e039e2 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 16 Dec 2025 21:42:22 +0000 Subject: [PATCH 1/8] Initial plan From 4cdb531d94f0a302837b7bb979e5fb70a6e68ee8 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 16 Dec 2025 22:03:50 +0000 Subject: [PATCH 2/8] Add DOTNET_CLI_CONTEXT_VERBOSE support when verbosity is diagnostic Co-authored-by: marcpopMSFT <12663534+marcpopMSFT@users.noreply.github.com> --- src/Cli/dotnet/Commands/Build/BuildCommand.cs | 2 ++ src/Cli/dotnet/Commands/Clean/CleanCommand.cs | 3 ++ src/Cli/dotnet/Commands/NuGet/NuGetCommand.cs | 9 ++++++ src/Cli/dotnet/Commands/Pack/PackCommand.cs | 2 ++ .../Package/List/PackageListCommand.cs | 4 +++ .../dotnet/Commands/Publish/PublishCommand.cs | 2 ++ .../dotnet/Commands/Restore/RestoreCommand.cs | 2 ++ src/Cli/dotnet/Commands/Run/RunCommand.cs | 2 ++ src/Cli/dotnet/Commands/Store/StoreCommand.cs | 7 ++++ .../MicrosoftTestingPlatformTestCommand.cs | 2 ++ .../Commands/Test/VSTest/TestCommand.cs | 2 ++ .../Tool/Execute/ToolExecuteCommand.cs | 3 ++ .../ToolInstallGlobalOrToolPathCommand.cs | 2 ++ .../Tool/Restore/ToolRestoreCommand.cs | 2 ++ .../Commands/Workload/WorkloadCommandBase.cs | 2 ++ .../Extensions/CommonOptionsExtensions.cs | 32 +++++++++++++++++++ 16 files changed, 78 insertions(+) diff --git a/src/Cli/dotnet/Commands/Build/BuildCommand.cs b/src/Cli/dotnet/Commands/Build/BuildCommand.cs index 8c67ed030f36..7f573459366e 100644 --- a/src/Cli/dotnet/Commands/Build/BuildCommand.cs +++ b/src/Cli/dotnet/Commands/Build/BuildCommand.cs @@ -52,6 +52,8 @@ public static int Run(ParseResult parseResult) { parseResult.HandleDebugSwitch(); + parseResult.GetValue(BuildCommandDefinition.VerbosityOption).ApplyVerbosityOptions(); + return FromParseResult(parseResult).Execute(); } } diff --git a/src/Cli/dotnet/Commands/Clean/CleanCommand.cs b/src/Cli/dotnet/Commands/Clean/CleanCommand.cs index e2074b7a7d58..8a7c1c3b02f4 100644 --- a/src/Cli/dotnet/Commands/Clean/CleanCommand.cs +++ b/src/Cli/dotnet/Commands/Clean/CleanCommand.cs @@ -42,6 +42,9 @@ public static CommandBase FromParseResult(ParseResult result, string? msbuildPat public static int Run(ParseResult parseResult) { parseResult.HandleDebugSwitch(); + + parseResult.GetValue(CleanCommandDefinition.VerbosityOption).ApplyVerbosityOptions(); + return FromParseResult(parseResult).Execute(); } } diff --git a/src/Cli/dotnet/Commands/NuGet/NuGetCommand.cs b/src/Cli/dotnet/Commands/NuGet/NuGetCommand.cs index 9a68c31b22e7..0d32235161b2 100644 --- a/src/Cli/dotnet/Commands/NuGet/NuGetCommand.cs +++ b/src/Cli/dotnet/Commands/NuGet/NuGetCommand.cs @@ -18,6 +18,15 @@ public static int Run(string[] args) public static int Run(ParseResult parseResult) { + // Apply verbosity options if diagnostic - check for verbosity in the arguments + var verbosityOption = parseResult.CommandResult.Command.Options + .OfType>() + .FirstOrDefault(o => o.Name == "--verbosity"); + if (verbosityOption != null) + { + parseResult.GetValue(verbosityOption).ApplyVerbosityOptions(); + } + return Run(parseResult.GetArguments(), new NuGetCommandRunner()); } diff --git a/src/Cli/dotnet/Commands/Pack/PackCommand.cs b/src/Cli/dotnet/Commands/Pack/PackCommand.cs index fe36ed860367..933ee76e0c1a 100644 --- a/src/Cli/dotnet/Commands/Pack/PackCommand.cs +++ b/src/Cli/dotnet/Commands/Pack/PackCommand.cs @@ -135,6 +135,8 @@ public static int Run(ParseResult parseResult) parseResult.HandleDebugSwitch(); parseResult.ShowHelpOrErrorIfAppropriate(); + parseResult.GetValue(PackCommandDefinition.VerbosityOption).ApplyVerbosityOptions(); + 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/Package/List/PackageListCommand.cs b/src/Cli/dotnet/Commands/Package/List/PackageListCommand.cs index 3bb3a17d61de..42c207450c18 100644 --- a/src/Cli/dotnet/Commands/Package/List/PackageListCommand.cs +++ b/src/Cli/dotnet/Commands/Package/List/PackageListCommand.cs @@ -23,6 +23,8 @@ internal class PackageListCommand( parseResult.GetValue(PackageCommandDefinition.ProjectOption) : parseResult.GetValue(ListCommandDefinition.SlnOrProjectArgument) ?? ""); + private readonly VerbosityOptions _verbosity = parseResult.GetValue((Option)PackageListCommandDefinition.VerbosityOption); + private static string GetAbsolutePath(string currentDirectory, string relativePath) { return Path.GetFullPath(Path.Combine(currentDirectory, relativePath)); @@ -30,6 +32,8 @@ private static string GetAbsolutePath(string currentDirectory, string relativePa public override int Execute() { + _verbosity.ApplyVerbosityOptions(); + string projectFile = GetProjectOrSolution(); bool noRestore = _parseResult.HasOption(PackageListCommandDefinition.NoRestore); int restoreExitCode = 0; diff --git a/src/Cli/dotnet/Commands/Publish/PublishCommand.cs b/src/Cli/dotnet/Commands/Publish/PublishCommand.cs index 19568315c08c..0fe375125050 100644 --- a/src/Cli/dotnet/Commands/Publish/PublishCommand.cs +++ b/src/Cli/dotnet/Commands/Publish/PublishCommand.cs @@ -79,6 +79,8 @@ public static int Run(ParseResult parseResult) { parseResult.HandleDebugSwitch(); + parseResult.GetValue(PublishCommandDefinition.VerbosityOption).ApplyVerbosityOptions(); + return FromParseResult(parseResult).Execute(); } } diff --git a/src/Cli/dotnet/Commands/Restore/RestoreCommand.cs b/src/Cli/dotnet/Commands/Restore/RestoreCommand.cs index f0e3fdff688b..e4f8a3648f55 100644 --- a/src/Cli/dotnet/Commands/Restore/RestoreCommand.cs +++ b/src/Cli/dotnet/Commands/Restore/RestoreCommand.cs @@ -64,6 +64,8 @@ public static int Run(ParseResult parseResult) { parseResult.HandleDebugSwitch(); + parseResult.GetValue(RestoreCommandDefinition.VerbosityOption).ApplyVerbosityOptions(); + return FromParseResult(parseResult).Execute(); } } diff --git a/src/Cli/dotnet/Commands/Run/RunCommand.cs b/src/Cli/dotnet/Commands/Run/RunCommand.cs index 4a0b48ddb7df..fec0ddbb07fc 100644 --- a/src/Cli/dotnet/Commands/Run/RunCommand.cs +++ b/src/Cli/dotnet/Commands/Run/RunCommand.cs @@ -789,6 +789,8 @@ public static int Run(ParseResult parseResult) { parseResult.HandleDebugSwitch(); + parseResult.GetValue(RunCommandDefinition.VerbosityOption).ApplyVerbosityOptions(); + return FromParseResult(parseResult).Execute(); } diff --git a/src/Cli/dotnet/Commands/Store/StoreCommand.cs b/src/Cli/dotnet/Commands/Store/StoreCommand.cs index 6c5b2953f7be..8736668a31a1 100644 --- a/src/Cli/dotnet/Commands/Store/StoreCommand.cs +++ b/src/Cli/dotnet/Commands/Store/StoreCommand.cs @@ -46,6 +46,13 @@ public static int Run(ParseResult parseResult) { parseResult.HandleDebugSwitch(); + // Apply verbosity options if diagnostic + var verbosityOption = StoreCommandParser.GetCommand().Options.OfType>().FirstOrDefault(o => o.Name == "--verbosity"); + if (verbosityOption != null) + { + parseResult.GetValue(verbosityOption).ApplyVerbosityOptions(); + } + return FromParseResult(parseResult).Execute(); } } diff --git a/src/Cli/dotnet/Commands/Test/MTP/MicrosoftTestingPlatformTestCommand.cs b/src/Cli/dotnet/Commands/Test/MTP/MicrosoftTestingPlatformTestCommand.cs index d84264e6c49e..3fe982a665e8 100644 --- a/src/Cli/dotnet/Commands/Test/MTP/MicrosoftTestingPlatformTestCommand.cs +++ b/src/Cli/dotnet/Commands/Test/MTP/MicrosoftTestingPlatformTestCommand.cs @@ -23,6 +23,8 @@ public MicrosoftTestingPlatformTestCommand(string name, string? description = nu public int Run(ParseResult parseResult, bool isHelp = false) { + parseResult.GetValue(TestCommandDefinition.VerbosityOption).ApplyVerbosityOptions(); + int? exitCode = null; try { diff --git a/src/Cli/dotnet/Commands/Test/VSTest/TestCommand.cs b/src/Cli/dotnet/Commands/Test/VSTest/TestCommand.cs index 7ef075d87530..9e8ae105f43d 100644 --- a/src/Cli/dotnet/Commands/Test/VSTest/TestCommand.cs +++ b/src/Cli/dotnet/Commands/Test/VSTest/TestCommand.cs @@ -23,6 +23,8 @@ public static int Run(ParseResult parseResult) { parseResult.HandleDebugSwitch(); + parseResult.GetValue(TestCommandDefinition.VerbosityOption).ApplyVerbosityOptions(); + FeatureFlag.Instance.PrintFlagFeatureState(); // We use also current process id for the correlation id for possible future usage in case we need to know the parent process diff --git a/src/Cli/dotnet/Commands/Tool/Execute/ToolExecuteCommand.cs b/src/Cli/dotnet/Commands/Tool/Execute/ToolExecuteCommand.cs index bdfdacb42d58..4447e3b5fe38 100644 --- a/src/Cli/dotnet/Commands/Tool/Execute/ToolExecuteCommand.cs +++ b/src/Cli/dotnet/Commands/Tool/Execute/ToolExecuteCommand.cs @@ -7,6 +7,7 @@ using Microsoft.DotNet.Cli.Commands.Tool.Install; using Microsoft.DotNet.Cli.Commands.Tool.Restore; using Microsoft.DotNet.Cli.Commands.Tool.Run; +using Microsoft.DotNet.Cli.Extensions; using Microsoft.DotNet.Cli.NuGetPackageDownloader; using Microsoft.DotNet.Cli.ToolManifest; using Microsoft.DotNet.Cli.ToolPackage; @@ -41,6 +42,8 @@ internal class ToolExecuteCommand(ParseResult result, ToolManifestFinder? toolMa public override int Execute() { + _verbosity.ApplyVerbosityOptions(); + VersionRange versionRange = _parseResult.GetVersionRange(); PackageId packageId = new PackageId(_packageToolIdentityArgument.Id); diff --git a/src/Cli/dotnet/Commands/Tool/Install/ToolInstallGlobalOrToolPathCommand.cs b/src/Cli/dotnet/Commands/Tool/Install/ToolInstallGlobalOrToolPathCommand.cs index 9ee9120eb642..5d9f20202db9 100644 --- a/src/Cli/dotnet/Commands/Tool/Install/ToolInstallGlobalOrToolPathCommand.cs +++ b/src/Cli/dotnet/Commands/Tool/Install/ToolInstallGlobalOrToolPathCommand.cs @@ -84,6 +84,8 @@ public ToolInstallGlobalOrToolPathCommand( _toolPath = parseResult.GetValue(ToolInstallCommandParser.ToolPathOption); _architectureOption = parseResult.GetValue(ToolInstallCommandParser.ArchitectureOption); + _verbosity.ApplyVerbosityOptions(); + _forwardRestoreArguments = parseResult.OptionValuesToBeForwarded(ToolInstallCommandParser.GetCommand()); _environmentPathInstruction = environmentPathInstruction diff --git a/src/Cli/dotnet/Commands/Tool/Restore/ToolRestoreCommand.cs b/src/Cli/dotnet/Commands/Tool/Restore/ToolRestoreCommand.cs index e2622d8d9fb1..26684e0f6934 100644 --- a/src/Cli/dotnet/Commands/Tool/Restore/ToolRestoreCommand.cs +++ b/src/Cli/dotnet/Commands/Tool/Restore/ToolRestoreCommand.cs @@ -70,6 +70,8 @@ public ToolRestoreCommand( _verbosity = VerbosityOptions.minimal; } + _verbosity.ApplyVerbosityOptions(); + _restoreActionConfig = new RestoreActionConfig(DisableParallel: result.GetValue(ToolCommandRestorePassThroughOptions.DisableParallelOption), NoCache: result.GetValue(ToolCommandRestorePassThroughOptions.NoCacheOption) || result.GetValue(ToolCommandRestorePassThroughOptions.NoHttpCacheOption), IgnoreFailedSources: result.GetValue(ToolCommandRestorePassThroughOptions.IgnoreFailedSourcesOption), diff --git a/src/Cli/dotnet/Commands/Workload/WorkloadCommandBase.cs b/src/Cli/dotnet/Commands/Workload/WorkloadCommandBase.cs index 44b441349be3..2d0f0fea253f 100644 --- a/src/Cli/dotnet/Commands/Workload/WorkloadCommandBase.cs +++ b/src/Cli/dotnet/Commands/Workload/WorkloadCommandBase.cs @@ -98,6 +98,8 @@ public WorkloadCommandBase( ? parseResult.GetValue(CommonOptions.VerbosityOption(VerbosityOptions.normal)) : parseResult.GetValue(verbosityOptions) ; + Verbosity.ApplyVerbosityOptions(); + ILogger nugetLogger = Verbosity.IsDetailedOrDiagnostic() ? new NuGetConsoleLogger() : new NullLogger(); Reporter = reporter ?? Utils.Reporter.Output; diff --git a/src/Cli/dotnet/Extensions/CommonOptionsExtensions.cs b/src/Cli/dotnet/Extensions/CommonOptionsExtensions.cs index 9254bbd73b77..3d4ea09d734a 100644 --- a/src/Cli/dotnet/Extensions/CommonOptionsExtensions.cs +++ b/src/Cli/dotnet/Extensions/CommonOptionsExtensions.cs @@ -64,6 +64,38 @@ public static bool IsNormal(this VerbosityOptions verbosity) verbosity.Equals(VerbosityOptions.n); } + public static bool IsDiagnostic(this VerbosityOptions verbosity) + { + return verbosity.Equals(VerbosityOptions.diag) || + verbosity.Equals(VerbosityOptions.diagnostic); + } + + /// + /// Applies the verbosity options to the command logging context. + /// When verbosity is set to diagnostic, also enables verbose details via DOTNET_CLI_CONTEXT_VERBOSE. + /// + public static void ApplyVerbosityOptions(this VerbosityOptions verbosity) + { + if (verbosity.IsDiagnostic()) + { + Environment.SetEnvironmentVariable(CommandLoggingContext.Variables.Verbose, bool.TrueString); + CommandLoggingContext.SetVerbose(true); + Reporter.Reset(); + } + } + + /// + /// Applies the verbosity options to the command logging context. + /// When verbosity is set to diagnostic, also enables verbose details via DOTNET_CLI_CONTEXT_VERBOSE. + /// + public static void ApplyVerbosityOptions(this VerbosityOptions? verbosity) + { + if (verbosity.HasValue) + { + verbosity.Value.ApplyVerbosityOptions(); + } + } + /// /// Converts to Microsoft.Extensions.Logging.. /// From 13dd387c69156fdce8b004fe4d43ec111de0c02a Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 16 Dec 2025 22:14:01 +0000 Subject: [PATCH 3/8] Address code review comments - improve consistency in verbosity option access Co-authored-by: marcpopMSFT <12663534+marcpopMSFT@users.noreply.github.com> --- src/Cli/dotnet/Commands/NuGet/NuGetCommand.cs | 14 ++++++++------ .../Commands/Package/List/PackageListCommand.cs | 2 +- .../Package/List/PackageListCommandDefinition.cs | 2 +- src/Cli/dotnet/Commands/Store/StoreCommand.cs | 7 +------ .../Commands/Store/StoreCommandDefinition.cs | 4 +++- 5 files changed, 14 insertions(+), 15 deletions(-) diff --git a/src/Cli/dotnet/Commands/NuGet/NuGetCommand.cs b/src/Cli/dotnet/Commands/NuGet/NuGetCommand.cs index 0d32235161b2..468b9aceaad3 100644 --- a/src/Cli/dotnet/Commands/NuGet/NuGetCommand.cs +++ b/src/Cli/dotnet/Commands/NuGet/NuGetCommand.cs @@ -18,13 +18,15 @@ public static int Run(string[] args) public static int Run(ParseResult parseResult) { - // Apply verbosity options if diagnostic - check for verbosity in the arguments - var verbosityOption = parseResult.CommandResult.Command.Options - .OfType>() - .FirstOrDefault(o => o.Name == "--verbosity"); - if (verbosityOption != null) + // Try to apply verbosity options if diagnostic + // NuGet commands use CommonOptions.VerbosityOption(), so we need to search for it + foreach (var option in parseResult.CommandResult.Command.Options) { - parseResult.GetValue(verbosityOption).ApplyVerbosityOptions(); + if (option.Name == "--verbosity" && option is Option verbosityOpt) + { + parseResult.GetValue(verbosityOpt).ApplyVerbosityOptions(); + break; + } } return Run(parseResult.GetArguments(), new NuGetCommandRunner()); diff --git a/src/Cli/dotnet/Commands/Package/List/PackageListCommand.cs b/src/Cli/dotnet/Commands/Package/List/PackageListCommand.cs index 42c207450c18..3465d0bb1339 100644 --- a/src/Cli/dotnet/Commands/Package/List/PackageListCommand.cs +++ b/src/Cli/dotnet/Commands/Package/List/PackageListCommand.cs @@ -23,7 +23,7 @@ internal class PackageListCommand( parseResult.GetValue(PackageCommandDefinition.ProjectOption) : parseResult.GetValue(ListCommandDefinition.SlnOrProjectArgument) ?? ""); - private readonly VerbosityOptions _verbosity = parseResult.GetValue((Option)PackageListCommandDefinition.VerbosityOption); + private readonly VerbosityOptions _verbosity = parseResult.GetValue(PackageListCommandDefinition.VerbosityOption); private static string GetAbsolutePath(string currentDirectory, string relativePath) { diff --git a/src/Cli/dotnet/Commands/Package/List/PackageListCommandDefinition.cs b/src/Cli/dotnet/Commands/Package/List/PackageListCommandDefinition.cs index 8aeb0bdb904a..dcab26757055 100644 --- a/src/Cli/dotnet/Commands/Package/List/PackageListCommandDefinition.cs +++ b/src/Cli/dotnet/Commands/Package/List/PackageListCommandDefinition.cs @@ -82,7 +82,7 @@ internal static class PackageListCommandDefinition Arity = ArgumentArity.Zero }; - public static readonly Option VerbosityOption = new Option("--verbosity", "-v") + public static readonly Option VerbosityOption = new Option("--verbosity", "-v") { Description = CliStrings.VerbosityOptionDescription, HelpName = CliStrings.LevelArgumentName diff --git a/src/Cli/dotnet/Commands/Store/StoreCommand.cs b/src/Cli/dotnet/Commands/Store/StoreCommand.cs index 8736668a31a1..bc6200653d5f 100644 --- a/src/Cli/dotnet/Commands/Store/StoreCommand.cs +++ b/src/Cli/dotnet/Commands/Store/StoreCommand.cs @@ -46,12 +46,7 @@ public static int Run(ParseResult parseResult) { parseResult.HandleDebugSwitch(); - // Apply verbosity options if diagnostic - var verbosityOption = StoreCommandParser.GetCommand().Options.OfType>().FirstOrDefault(o => o.Name == "--verbosity"); - if (verbosityOption != null) - { - parseResult.GetValue(verbosityOption).ApplyVerbosityOptions(); - } + parseResult.GetValue(StoreCommandDefinition.VerbosityOption).ApplyVerbosityOptions(); return FromParseResult(parseResult).Execute(); } diff --git a/src/Cli/dotnet/Commands/Store/StoreCommandDefinition.cs b/src/Cli/dotnet/Commands/Store/StoreCommandDefinition.cs index 3a2241194f40..bb82b71ed86a 100644 --- a/src/Cli/dotnet/Commands/Store/StoreCommandDefinition.cs +++ b/src/Cli/dotnet/Commands/Store/StoreCommandDefinition.cs @@ -16,6 +16,8 @@ internal static class StoreCommandDefinition Arity = ArgumentArity.ZeroOrMore, }; + public static readonly Option VerbosityOption = CommonOptions.VerbosityOption(); + public static readonly Option> ManifestOption = new Option>("--manifest", "-m") { Description = CliCommandStrings.ProjectManifestDescription, @@ -83,7 +85,7 @@ public static Command Create() command.Options.Add(SkipSymbolsOption); command.Options.Add(CommonOptions.FrameworkOption(CliCommandStrings.StoreFrameworkOptionDescription)); command.Options.Add(CommonOptions.RuntimeOption(CliCommandStrings.StoreRuntimeOptionDescription)); - command.Options.Add(CommonOptions.VerbosityOption()); + command.Options.Add(VerbosityOption); command.Options.Add(CommonOptions.CurrentRuntimeOption(CliCommandStrings.CurrentRuntimeOptionDescription)); command.Options.Add(CommonOptions.DisableBuildServersOption); command.Options.Add(CommonOptions.NoLogoOption(true)); From 36115904b7fe3cb7ecdf4912d0c38acd66739a99 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 16 Dec 2025 22:15:38 +0000 Subject: [PATCH 4/8] Add clarifying comment for Reporter.Reset() call Co-authored-by: marcpopMSFT <12663534+marcpopMSFT@users.noreply.github.com> --- src/Cli/dotnet/Extensions/CommonOptionsExtensions.cs | 1 + 1 file changed, 1 insertion(+) diff --git a/src/Cli/dotnet/Extensions/CommonOptionsExtensions.cs b/src/Cli/dotnet/Extensions/CommonOptionsExtensions.cs index 3d4ea09d734a..fe7403923867 100644 --- a/src/Cli/dotnet/Extensions/CommonOptionsExtensions.cs +++ b/src/Cli/dotnet/Extensions/CommonOptionsExtensions.cs @@ -80,6 +80,7 @@ public static void ApplyVerbosityOptions(this VerbosityOptions verbosity) { Environment.SetEnvironmentVariable(CommandLoggingContext.Variables.Verbose, bool.TrueString); CommandLoggingContext.SetVerbose(true); + // Reset the reporter to apply the new verbose setting Reporter.Reset(); } } From 47b910652d0eff40bb7d77125dc679d902adce27 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 16 Dec 2025 22:50:49 +0000 Subject: [PATCH 5/8] Refactor to use Option Action instead of per-command calls Per @baronfel's feedback, moved verbosity handling to an Action on the VerbosityOption itself. This eliminates the need for each command to manually call ApplyVerbosityOptions(). The Action is attached to all VerbosityOption variants and will be invoked by System.CommandLine when the option is parsed. Changes: - Added ApplyVerbosityAction class in CommonOptions - Added Action property to all VerbosityOption definitions - Reverted all command-specific verbosity handling code - Kept IsDiagnostic() extension method for reuse Co-authored-by: baronfel <573979+baronfel@users.noreply.github.com> --- src/Cli/dotnet/Commands/Build/BuildCommand.cs | 2 - src/Cli/dotnet/Commands/Clean/CleanCommand.cs | 3 - src/Cli/dotnet/Commands/NuGet/NuGetCommand.cs | 11 ---- src/Cli/dotnet/Commands/Pack/PackCommand.cs | 2 - .../Package/List/PackageListCommand.cs | 4 -- .../List/PackageListCommandDefinition.cs | 2 +- .../dotnet/Commands/Publish/PublishCommand.cs | 2 - .../dotnet/Commands/Restore/RestoreCommand.cs | 2 - src/Cli/dotnet/Commands/Run/RunCommand.cs | 2 - src/Cli/dotnet/Commands/Store/StoreCommand.cs | 2 - .../Commands/Store/StoreCommandDefinition.cs | 4 +- .../MicrosoftTestingPlatformTestCommand.cs | 2 - .../Commands/Test/VSTest/TestCommand.cs | 2 - .../Tool/Execute/ToolExecuteCommand.cs | 3 - .../ToolInstallGlobalOrToolPathCommand.cs | 2 - .../Tool/Restore/ToolRestoreCommand.cs | 2 - .../Commands/Workload/WorkloadCommandBase.cs | 2 - src/Cli/dotnet/CommonOptions.cs | 66 ++++++++++++++++++- .../Extensions/CommonOptionsExtensions.cs | 27 -------- 19 files changed, 65 insertions(+), 77 deletions(-) diff --git a/src/Cli/dotnet/Commands/Build/BuildCommand.cs b/src/Cli/dotnet/Commands/Build/BuildCommand.cs index 7f573459366e..8c67ed030f36 100644 --- a/src/Cli/dotnet/Commands/Build/BuildCommand.cs +++ b/src/Cli/dotnet/Commands/Build/BuildCommand.cs @@ -52,8 +52,6 @@ public static int Run(ParseResult parseResult) { parseResult.HandleDebugSwitch(); - parseResult.GetValue(BuildCommandDefinition.VerbosityOption).ApplyVerbosityOptions(); - return FromParseResult(parseResult).Execute(); } } diff --git a/src/Cli/dotnet/Commands/Clean/CleanCommand.cs b/src/Cli/dotnet/Commands/Clean/CleanCommand.cs index 8a7c1c3b02f4..e2074b7a7d58 100644 --- a/src/Cli/dotnet/Commands/Clean/CleanCommand.cs +++ b/src/Cli/dotnet/Commands/Clean/CleanCommand.cs @@ -42,9 +42,6 @@ public static CommandBase FromParseResult(ParseResult result, string? msbuildPat public static int Run(ParseResult parseResult) { parseResult.HandleDebugSwitch(); - - parseResult.GetValue(CleanCommandDefinition.VerbosityOption).ApplyVerbosityOptions(); - return FromParseResult(parseResult).Execute(); } } diff --git a/src/Cli/dotnet/Commands/NuGet/NuGetCommand.cs b/src/Cli/dotnet/Commands/NuGet/NuGetCommand.cs index 468b9aceaad3..9a68c31b22e7 100644 --- a/src/Cli/dotnet/Commands/NuGet/NuGetCommand.cs +++ b/src/Cli/dotnet/Commands/NuGet/NuGetCommand.cs @@ -18,17 +18,6 @@ public static int Run(string[] args) public static int Run(ParseResult parseResult) { - // Try to apply verbosity options if diagnostic - // NuGet commands use CommonOptions.VerbosityOption(), so we need to search for it - foreach (var option in parseResult.CommandResult.Command.Options) - { - if (option.Name == "--verbosity" && option is Option verbosityOpt) - { - parseResult.GetValue(verbosityOpt).ApplyVerbosityOptions(); - break; - } - } - return Run(parseResult.GetArguments(), new NuGetCommandRunner()); } diff --git a/src/Cli/dotnet/Commands/Pack/PackCommand.cs b/src/Cli/dotnet/Commands/Pack/PackCommand.cs index 933ee76e0c1a..fe36ed860367 100644 --- a/src/Cli/dotnet/Commands/Pack/PackCommand.cs +++ b/src/Cli/dotnet/Commands/Pack/PackCommand.cs @@ -135,8 +135,6 @@ public static int Run(ParseResult parseResult) parseResult.HandleDebugSwitch(); parseResult.ShowHelpOrErrorIfAppropriate(); - parseResult.GetValue(PackCommandDefinition.VerbosityOption).ApplyVerbosityOptions(); - 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/Package/List/PackageListCommand.cs b/src/Cli/dotnet/Commands/Package/List/PackageListCommand.cs index 3465d0bb1339..3bb3a17d61de 100644 --- a/src/Cli/dotnet/Commands/Package/List/PackageListCommand.cs +++ b/src/Cli/dotnet/Commands/Package/List/PackageListCommand.cs @@ -23,8 +23,6 @@ internal class PackageListCommand( parseResult.GetValue(PackageCommandDefinition.ProjectOption) : parseResult.GetValue(ListCommandDefinition.SlnOrProjectArgument) ?? ""); - private readonly VerbosityOptions _verbosity = parseResult.GetValue(PackageListCommandDefinition.VerbosityOption); - private static string GetAbsolutePath(string currentDirectory, string relativePath) { return Path.GetFullPath(Path.Combine(currentDirectory, relativePath)); @@ -32,8 +30,6 @@ private static string GetAbsolutePath(string currentDirectory, string relativePa public override int Execute() { - _verbosity.ApplyVerbosityOptions(); - string projectFile = GetProjectOrSolution(); bool noRestore = _parseResult.HasOption(PackageListCommandDefinition.NoRestore); int restoreExitCode = 0; diff --git a/src/Cli/dotnet/Commands/Package/List/PackageListCommandDefinition.cs b/src/Cli/dotnet/Commands/Package/List/PackageListCommandDefinition.cs index dcab26757055..8aeb0bdb904a 100644 --- a/src/Cli/dotnet/Commands/Package/List/PackageListCommandDefinition.cs +++ b/src/Cli/dotnet/Commands/Package/List/PackageListCommandDefinition.cs @@ -82,7 +82,7 @@ internal static class PackageListCommandDefinition Arity = ArgumentArity.Zero }; - public static readonly Option VerbosityOption = new Option("--verbosity", "-v") + public static readonly Option VerbosityOption = new Option("--verbosity", "-v") { Description = CliStrings.VerbosityOptionDescription, HelpName = CliStrings.LevelArgumentName diff --git a/src/Cli/dotnet/Commands/Publish/PublishCommand.cs b/src/Cli/dotnet/Commands/Publish/PublishCommand.cs index 0fe375125050..19568315c08c 100644 --- a/src/Cli/dotnet/Commands/Publish/PublishCommand.cs +++ b/src/Cli/dotnet/Commands/Publish/PublishCommand.cs @@ -79,8 +79,6 @@ public static int Run(ParseResult parseResult) { parseResult.HandleDebugSwitch(); - parseResult.GetValue(PublishCommandDefinition.VerbosityOption).ApplyVerbosityOptions(); - return FromParseResult(parseResult).Execute(); } } diff --git a/src/Cli/dotnet/Commands/Restore/RestoreCommand.cs b/src/Cli/dotnet/Commands/Restore/RestoreCommand.cs index e4f8a3648f55..f0e3fdff688b 100644 --- a/src/Cli/dotnet/Commands/Restore/RestoreCommand.cs +++ b/src/Cli/dotnet/Commands/Restore/RestoreCommand.cs @@ -64,8 +64,6 @@ public static int Run(ParseResult parseResult) { parseResult.HandleDebugSwitch(); - parseResult.GetValue(RestoreCommandDefinition.VerbosityOption).ApplyVerbosityOptions(); - return FromParseResult(parseResult).Execute(); } } diff --git a/src/Cli/dotnet/Commands/Run/RunCommand.cs b/src/Cli/dotnet/Commands/Run/RunCommand.cs index fec0ddbb07fc..4a0b48ddb7df 100644 --- a/src/Cli/dotnet/Commands/Run/RunCommand.cs +++ b/src/Cli/dotnet/Commands/Run/RunCommand.cs @@ -789,8 +789,6 @@ public static int Run(ParseResult parseResult) { parseResult.HandleDebugSwitch(); - parseResult.GetValue(RunCommandDefinition.VerbosityOption).ApplyVerbosityOptions(); - return FromParseResult(parseResult).Execute(); } diff --git a/src/Cli/dotnet/Commands/Store/StoreCommand.cs b/src/Cli/dotnet/Commands/Store/StoreCommand.cs index bc6200653d5f..6c5b2953f7be 100644 --- a/src/Cli/dotnet/Commands/Store/StoreCommand.cs +++ b/src/Cli/dotnet/Commands/Store/StoreCommand.cs @@ -46,8 +46,6 @@ public static int Run(ParseResult parseResult) { parseResult.HandleDebugSwitch(); - parseResult.GetValue(StoreCommandDefinition.VerbosityOption).ApplyVerbosityOptions(); - return FromParseResult(parseResult).Execute(); } } diff --git a/src/Cli/dotnet/Commands/Store/StoreCommandDefinition.cs b/src/Cli/dotnet/Commands/Store/StoreCommandDefinition.cs index bb82b71ed86a..3a2241194f40 100644 --- a/src/Cli/dotnet/Commands/Store/StoreCommandDefinition.cs +++ b/src/Cli/dotnet/Commands/Store/StoreCommandDefinition.cs @@ -16,8 +16,6 @@ internal static class StoreCommandDefinition Arity = ArgumentArity.ZeroOrMore, }; - public static readonly Option VerbosityOption = CommonOptions.VerbosityOption(); - public static readonly Option> ManifestOption = new Option>("--manifest", "-m") { Description = CliCommandStrings.ProjectManifestDescription, @@ -85,7 +83,7 @@ public static Command Create() command.Options.Add(SkipSymbolsOption); command.Options.Add(CommonOptions.FrameworkOption(CliCommandStrings.StoreFrameworkOptionDescription)); command.Options.Add(CommonOptions.RuntimeOption(CliCommandStrings.StoreRuntimeOptionDescription)); - command.Options.Add(VerbosityOption); + command.Options.Add(CommonOptions.VerbosityOption()); command.Options.Add(CommonOptions.CurrentRuntimeOption(CliCommandStrings.CurrentRuntimeOptionDescription)); command.Options.Add(CommonOptions.DisableBuildServersOption); command.Options.Add(CommonOptions.NoLogoOption(true)); diff --git a/src/Cli/dotnet/Commands/Test/MTP/MicrosoftTestingPlatformTestCommand.cs b/src/Cli/dotnet/Commands/Test/MTP/MicrosoftTestingPlatformTestCommand.cs index 3fe982a665e8..d84264e6c49e 100644 --- a/src/Cli/dotnet/Commands/Test/MTP/MicrosoftTestingPlatformTestCommand.cs +++ b/src/Cli/dotnet/Commands/Test/MTP/MicrosoftTestingPlatformTestCommand.cs @@ -23,8 +23,6 @@ public MicrosoftTestingPlatformTestCommand(string name, string? description = nu public int Run(ParseResult parseResult, bool isHelp = false) { - parseResult.GetValue(TestCommandDefinition.VerbosityOption).ApplyVerbosityOptions(); - int? exitCode = null; try { diff --git a/src/Cli/dotnet/Commands/Test/VSTest/TestCommand.cs b/src/Cli/dotnet/Commands/Test/VSTest/TestCommand.cs index 9e8ae105f43d..7ef075d87530 100644 --- a/src/Cli/dotnet/Commands/Test/VSTest/TestCommand.cs +++ b/src/Cli/dotnet/Commands/Test/VSTest/TestCommand.cs @@ -23,8 +23,6 @@ public static int Run(ParseResult parseResult) { parseResult.HandleDebugSwitch(); - parseResult.GetValue(TestCommandDefinition.VerbosityOption).ApplyVerbosityOptions(); - FeatureFlag.Instance.PrintFlagFeatureState(); // We use also current process id for the correlation id for possible future usage in case we need to know the parent process diff --git a/src/Cli/dotnet/Commands/Tool/Execute/ToolExecuteCommand.cs b/src/Cli/dotnet/Commands/Tool/Execute/ToolExecuteCommand.cs index 4447e3b5fe38..bdfdacb42d58 100644 --- a/src/Cli/dotnet/Commands/Tool/Execute/ToolExecuteCommand.cs +++ b/src/Cli/dotnet/Commands/Tool/Execute/ToolExecuteCommand.cs @@ -7,7 +7,6 @@ using Microsoft.DotNet.Cli.Commands.Tool.Install; using Microsoft.DotNet.Cli.Commands.Tool.Restore; using Microsoft.DotNet.Cli.Commands.Tool.Run; -using Microsoft.DotNet.Cli.Extensions; using Microsoft.DotNet.Cli.NuGetPackageDownloader; using Microsoft.DotNet.Cli.ToolManifest; using Microsoft.DotNet.Cli.ToolPackage; @@ -42,8 +41,6 @@ internal class ToolExecuteCommand(ParseResult result, ToolManifestFinder? toolMa public override int Execute() { - _verbosity.ApplyVerbosityOptions(); - VersionRange versionRange = _parseResult.GetVersionRange(); PackageId packageId = new PackageId(_packageToolIdentityArgument.Id); diff --git a/src/Cli/dotnet/Commands/Tool/Install/ToolInstallGlobalOrToolPathCommand.cs b/src/Cli/dotnet/Commands/Tool/Install/ToolInstallGlobalOrToolPathCommand.cs index 5d9f20202db9..9ee9120eb642 100644 --- a/src/Cli/dotnet/Commands/Tool/Install/ToolInstallGlobalOrToolPathCommand.cs +++ b/src/Cli/dotnet/Commands/Tool/Install/ToolInstallGlobalOrToolPathCommand.cs @@ -84,8 +84,6 @@ public ToolInstallGlobalOrToolPathCommand( _toolPath = parseResult.GetValue(ToolInstallCommandParser.ToolPathOption); _architectureOption = parseResult.GetValue(ToolInstallCommandParser.ArchitectureOption); - _verbosity.ApplyVerbosityOptions(); - _forwardRestoreArguments = parseResult.OptionValuesToBeForwarded(ToolInstallCommandParser.GetCommand()); _environmentPathInstruction = environmentPathInstruction diff --git a/src/Cli/dotnet/Commands/Tool/Restore/ToolRestoreCommand.cs b/src/Cli/dotnet/Commands/Tool/Restore/ToolRestoreCommand.cs index 26684e0f6934..e2622d8d9fb1 100644 --- a/src/Cli/dotnet/Commands/Tool/Restore/ToolRestoreCommand.cs +++ b/src/Cli/dotnet/Commands/Tool/Restore/ToolRestoreCommand.cs @@ -70,8 +70,6 @@ public ToolRestoreCommand( _verbosity = VerbosityOptions.minimal; } - _verbosity.ApplyVerbosityOptions(); - _restoreActionConfig = new RestoreActionConfig(DisableParallel: result.GetValue(ToolCommandRestorePassThroughOptions.DisableParallelOption), NoCache: result.GetValue(ToolCommandRestorePassThroughOptions.NoCacheOption) || result.GetValue(ToolCommandRestorePassThroughOptions.NoHttpCacheOption), IgnoreFailedSources: result.GetValue(ToolCommandRestorePassThroughOptions.IgnoreFailedSourcesOption), diff --git a/src/Cli/dotnet/Commands/Workload/WorkloadCommandBase.cs b/src/Cli/dotnet/Commands/Workload/WorkloadCommandBase.cs index 2d0f0fea253f..44b441349be3 100644 --- a/src/Cli/dotnet/Commands/Workload/WorkloadCommandBase.cs +++ b/src/Cli/dotnet/Commands/Workload/WorkloadCommandBase.cs @@ -98,8 +98,6 @@ public WorkloadCommandBase( ? parseResult.GetValue(CommonOptions.VerbosityOption(VerbosityOptions.normal)) : parseResult.GetValue(verbosityOptions) ; - Verbosity.ApplyVerbosityOptions(); - ILogger nugetLogger = Verbosity.IsDetailedOrDiagnostic() ? new NuGetConsoleLogger() : new NullLogger(); Reporter = reporter ?? Utils.Reporter.Output; diff --git a/src/Cli/dotnet/CommonOptions.cs b/src/Cli/dotnet/CommonOptions.cs index 4816872b8e59..1ebc6659655e 100644 --- a/src/Cli/dotnet/CommonOptions.cs +++ b/src/Cli/dotnet/CommonOptions.cs @@ -4,6 +4,7 @@ using System.Collections.ObjectModel; using System.CommandLine; using System.CommandLine.Completions; +using System.CommandLine.Invocation; using System.CommandLine.Parsing; using System.CommandLine.StaticCompletions; using Microsoft.DotNet.Cli.CommandLine; @@ -145,7 +146,8 @@ public static Option VerbosityOption(VerbosityOptions defaultV { Description = CliStrings.VerbosityOptionDescription, HelpName = CliStrings.LevelArgumentName, - DefaultValueFactory = _ => defaultVerbosity + DefaultValueFactory = _ => defaultVerbosity, + Action = new ApplyVerbosityAction() } .ForwardAsSingle(o => $"--verbosity:{o}") .AggregateRepeatedTokens(); @@ -154,7 +156,8 @@ public static Option VerbosityOption(VerbosityOptions defaultV new Option("--verbosity", "-v", "--v", "-verbosity", "/v", "/verbosity") { Description = CliStrings.VerbosityOptionDescription, - HelpName = CliStrings.LevelArgumentName + HelpName = CliStrings.LevelArgumentName, + Action = new ApplyVerbosityAction() } .ForwardAsSingle(o => $"--verbosity:{o}") .AggregateRepeatedTokens(); @@ -164,7 +167,8 @@ public static Option VerbosityOption(VerbosityOptions defaultV { Description = CliStrings.VerbosityOptionDescription, HelpName = CliStrings.LevelArgumentName, - Hidden = true + Hidden = true, + Action = new ApplyVerbosityAction() } .ForwardAsSingle(o => $"--verbosity:{o}") .AggregateRepeatedTokens(); @@ -493,6 +497,62 @@ public static string GetCurrentRuntimeId() private static string GetOsFromRid(string rid) => rid.Substring(0, rid.LastIndexOf("-", StringComparison.InvariantCulture)); private static string GetArchFromRid(string rid) => rid.Substring(rid.LastIndexOf("-", StringComparison.InvariantCulture) + 1, rid.Length - rid.LastIndexOf("-", StringComparison.InvariantCulture) - 1); + + /// + /// Action that sets DOTNET_CLI_CONTEXT_VERBOSE environment variable when verbosity is diagnostic. + /// + private class ApplyVerbosityAction : SynchronousCommandLineAction + { + public override int Invoke(ParseResult parseResult) + { + // Debug: Console.Error.WriteLine("ApplyVerbosityAction.Invoke called"); + + // Try to get the verbosity value from any verbosity option in the parse result + foreach (var symbolResult in parseResult.CommandResult.Children) + { + if (symbolResult is OptionResult optionResult && + optionResult.Option.Name == "--verbosity") + { + var value = optionResult.GetValueOrDefault(); + + // Handle both VerbosityOptions and VerbosityOptions? + VerbosityOptions verbosity; + if (value is VerbosityOptions v) + { + verbosity = v; + } + else if (value != null) + { + // Try to parse as VerbosityOptions + if (Enum.TryParse(value.ToString(), out verbosity)) + { + // Success + } + else + { + return 0; + } + } + else + { + return 0; + } + + if (verbosity.IsDiagnostic()) + { + // Debug: Console.Error.WriteLine("Setting DOTNET_CLI_CONTEXT_VERBOSE"); + Environment.SetEnvironmentVariable(CommandLoggingContext.Variables.Verbose, bool.TrueString); + CommandLoggingContext.SetVerbose(true); + Reporter.Reset(); + } + + break; + } + } + + return 0; + } + } } diff --git a/src/Cli/dotnet/Extensions/CommonOptionsExtensions.cs b/src/Cli/dotnet/Extensions/CommonOptionsExtensions.cs index fe7403923867..fa35d6db2e63 100644 --- a/src/Cli/dotnet/Extensions/CommonOptionsExtensions.cs +++ b/src/Cli/dotnet/Extensions/CommonOptionsExtensions.cs @@ -70,33 +70,6 @@ public static bool IsDiagnostic(this VerbosityOptions verbosity) verbosity.Equals(VerbosityOptions.diagnostic); } - /// - /// Applies the verbosity options to the command logging context. - /// When verbosity is set to diagnostic, also enables verbose details via DOTNET_CLI_CONTEXT_VERBOSE. - /// - public static void ApplyVerbosityOptions(this VerbosityOptions verbosity) - { - if (verbosity.IsDiagnostic()) - { - Environment.SetEnvironmentVariable(CommandLoggingContext.Variables.Verbose, bool.TrueString); - CommandLoggingContext.SetVerbose(true); - // Reset the reporter to apply the new verbose setting - Reporter.Reset(); - } - } - - /// - /// Applies the verbosity options to the command logging context. - /// When verbosity is set to diagnostic, also enables verbose details via DOTNET_CLI_CONTEXT_VERBOSE. - /// - public static void ApplyVerbosityOptions(this VerbosityOptions? verbosity) - { - if (verbosity.HasValue) - { - verbosity.Value.ApplyVerbosityOptions(); - } - } - /// /// Converts to Microsoft.Extensions.Logging.. /// From ae21ca7b1e038b79df341e22974422dcc82ade76 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 16 Dec 2025 22:56:27 +0000 Subject: [PATCH 6/8] Fix Action to be non-terminating for proper invocation The Action needs Terminating = false so that command execution continues after the action runs. Verified that DOTNET_CLI_CONTEXT_VERBOSE is now properly set to "True" when using -v diag or -v diagnostic across build, restore, and other commands. Co-authored-by: baronfel <573979+baronfel@users.noreply.github.com> --- src/Cli/dotnet/CommonOptions.cs | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/Cli/dotnet/CommonOptions.cs b/src/Cli/dotnet/CommonOptions.cs index 1ebc6659655e..2ea6f2ccc17b 100644 --- a/src/Cli/dotnet/CommonOptions.cs +++ b/src/Cli/dotnet/CommonOptions.cs @@ -503,6 +503,8 @@ public static string GetCurrentRuntimeId() /// private class ApplyVerbosityAction : SynchronousCommandLineAction { + public override bool Terminating => false; + public override int Invoke(ParseResult parseResult) { // Debug: Console.Error.WriteLine("ApplyVerbosityAction.Invoke called"); From 339c1f4b66d61954c27298a9e489d061b8415d42 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 16 Dec 2025 23:01:34 +0000 Subject: [PATCH 7/8] Remove debug comments and clean up code per review - Removed commented debug statements - Simplified success case in enum parsing - All functionality remains working as verified by tests Co-authored-by: baronfel <573979+baronfel@users.noreply.github.com> --- src/Cli/dotnet/CommonOptions.cs | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/src/Cli/dotnet/CommonOptions.cs b/src/Cli/dotnet/CommonOptions.cs index 2ea6f2ccc17b..75bc4d5c5cd0 100644 --- a/src/Cli/dotnet/CommonOptions.cs +++ b/src/Cli/dotnet/CommonOptions.cs @@ -507,8 +507,6 @@ private class ApplyVerbosityAction : SynchronousCommandLineAction public override int Invoke(ParseResult parseResult) { - // Debug: Console.Error.WriteLine("ApplyVerbosityAction.Invoke called"); - // Try to get the verbosity value from any verbosity option in the parse result foreach (var symbolResult in parseResult.CommandResult.Children) { @@ -523,17 +521,9 @@ public override int Invoke(ParseResult parseResult) { verbosity = v; } - else if (value != null) + else if (value != null && Enum.TryParse(value.ToString(), out verbosity)) { - // Try to parse as VerbosityOptions - if (Enum.TryParse(value.ToString(), out verbosity)) - { - // Success - } - else - { - return 0; - } + // Successfully parsed verbosity from value } else { @@ -542,7 +532,6 @@ public override int Invoke(ParseResult parseResult) if (verbosity.IsDiagnostic()) { - // Debug: Console.Error.WriteLine("Setting DOTNET_CLI_CONTEXT_VERBOSE"); Environment.SetEnvironmentVariable(CommandLoggingContext.Variables.Verbose, bool.TrueString); CommandLoggingContext.SetVerbose(true); Reporter.Reset(); From e7a82e76175829e0cc8597e4b8f95f92a1df344d Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Tue, 16 Dec 2025 23:23:44 +0000 Subject: [PATCH 8/8] Simplify Action by passing option reference directly Per @baronfel's feedback, refactored ApplyVerbosityAction to accept the verbosity option as a constructor parameter. This allows direct use of parseResult.GetValue(verbosityOption) instead of searching through all children. Changes: - Made ApplyVerbosityAction generic to handle both VerbosityOptions and VerbosityOptions? types - Pass option reference to Action constructor - Simplified Invoke logic to directly get value from parse result - Changed HiddenVerbosityOption from field to property to support proper initialization with Action Verified working with dotnet build/restore -v diag Co-authored-by: baronfel <573979+baronfel@users.noreply.github.com> --- src/Cli/dotnet/CommonOptions.cs | 99 ++++++++++++++++----------------- 1 file changed, 47 insertions(+), 52 deletions(-) diff --git a/src/Cli/dotnet/CommonOptions.cs b/src/Cli/dotnet/CommonOptions.cs index 75bc4d5c5cd0..133964a095f7 100644 --- a/src/Cli/dotnet/CommonOptions.cs +++ b/src/Cli/dotnet/CommonOptions.cs @@ -141,37 +141,46 @@ public static string[] SplitMSBuildValues(string? defaultValue, ArgumentResult a return allValues.Distinct(StringComparer.OrdinalIgnoreCase).ToArray(); } - public static Option VerbosityOption(VerbosityOptions defaultVerbosity) => - new Option("--verbosity", "-v") + public static Option VerbosityOption(VerbosityOptions defaultVerbosity) + { + var option = new Option("--verbosity", "-v") { Description = CliStrings.VerbosityOptionDescription, HelpName = CliStrings.LevelArgumentName, - DefaultValueFactory = _ => defaultVerbosity, - Action = new ApplyVerbosityAction() - } - .ForwardAsSingle(o => $"--verbosity:{o}") - .AggregateRepeatedTokens(); + DefaultValueFactory = _ => defaultVerbosity + }; + option.Action = new ApplyVerbosityAction(option); + return option.ForwardAsSingle(o => $"--verbosity:{o}") + .AggregateRepeatedTokens(); + } - public static Option VerbosityOption() => - new Option("--verbosity", "-v", "--v", "-verbosity", "/v", "/verbosity") + public static Option VerbosityOption() + { + var option = new Option("--verbosity", "-v", "--v", "-verbosity", "/v", "/verbosity") { Description = CliStrings.VerbosityOptionDescription, - HelpName = CliStrings.LevelArgumentName, - Action = new ApplyVerbosityAction() - } - .ForwardAsSingle(o => $"--verbosity:{o}") - .AggregateRepeatedTokens(); + HelpName = CliStrings.LevelArgumentName + }; + option.Action = new ApplyVerbosityAction(option); + return option.ForwardAsSingle(o => $"--verbosity:{o}") + .AggregateRepeatedTokens(); + } - public static Option HiddenVerbosityOption = - new Option("--verbosity", "-v", "--v", "-verbosity", "/v", "/verbosity") + public static Option HiddenVerbosityOption + { + get { - Description = CliStrings.VerbosityOptionDescription, - HelpName = CliStrings.LevelArgumentName, - Hidden = true, - Action = new ApplyVerbosityAction() + var option = new Option("--verbosity", "-v", "--v", "-verbosity", "/v", "/verbosity") + { + Description = CliStrings.VerbosityOptionDescription, + HelpName = CliStrings.LevelArgumentName, + Hidden = true + }; + option.Action = new ApplyVerbosityAction(option); + return option.ForwardAsSingle(o => $"--verbosity:{o}") + .AggregateRepeatedTokens(); } - .ForwardAsSingle(o => $"--verbosity:{o}") - .AggregateRepeatedTokens(); + } public static Option FrameworkOption(string description) => new Option("--framework", "-f") @@ -501,43 +510,29 @@ public static string GetCurrentRuntimeId() /// /// Action that sets DOTNET_CLI_CONTEXT_VERBOSE environment variable when verbosity is diagnostic. /// - private class ApplyVerbosityAction : SynchronousCommandLineAction + private class ApplyVerbosityAction : SynchronousCommandLineAction { + private readonly Option _verbosityOption; + + public ApplyVerbosityAction(Option verbosityOption) + { + _verbosityOption = verbosityOption; + } + public override bool Terminating => false; public override int Invoke(ParseResult parseResult) { - // Try to get the verbosity value from any verbosity option in the parse result - foreach (var symbolResult in parseResult.CommandResult.Children) + var value = parseResult.GetValue(_verbosityOption); + + // Handle both VerbosityOptions and VerbosityOptions? + if (value is VerbosityOptions verbosity) { - if (symbolResult is OptionResult optionResult && - optionResult.Option.Name == "--verbosity") + if (verbosity.IsDiagnostic()) { - var value = optionResult.GetValueOrDefault(); - - // Handle both VerbosityOptions and VerbosityOptions? - VerbosityOptions verbosity; - if (value is VerbosityOptions v) - { - verbosity = v; - } - else if (value != null && Enum.TryParse(value.ToString(), out verbosity)) - { - // Successfully parsed verbosity from value - } - else - { - return 0; - } - - if (verbosity.IsDiagnostic()) - { - Environment.SetEnvironmentVariable(CommandLoggingContext.Variables.Verbose, bool.TrueString); - CommandLoggingContext.SetVerbose(true); - Reporter.Reset(); - } - - break; + Environment.SetEnvironmentVariable(CommandLoggingContext.Variables.Verbose, bool.TrueString); + CommandLoggingContext.SetVerbose(true); + Reporter.Reset(); } }