Skip to content

Commit 5e9d1ab

Browse files
committed
unify some diagnosticsoptions stuff
1 parent a923755 commit 5e9d1ab

File tree

2 files changed

+13
-30
lines changed

2 files changed

+13
-30
lines changed

src/Cli/dotnet/CommonOptionsFactory.cs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33

44
using System.CommandLine;
55
using System.CommandLine.Invocation;
6-
using System.CommandLine.Parsing;
7-
using Microsoft.DotNet.Cli.Extensions;
86
using Microsoft.DotNet.Cli.Utils;
97

108
namespace Microsoft.DotNet.Cli;
@@ -21,27 +19,21 @@ internal static class CommonOptionsFactory
2119
{
2220
Description = CliStrings.SDKDiagnosticsCommandDefinition,
2321
Recursive = recursive,
24-
Arity = ArgumentArity.Zero
22+
Arity = ArgumentArity.Zero,
23+
Action = new SetDiagnosticModeAction()
2524
};
2625

27-
internal class SetDiagnosticModeAction(Option<bool> diagnosticOption) : SynchronousCommandLineAction
26+
/// <summary>
27+
/// Sets a few verbose diagnostics flags across the CLI.
28+
/// Other commands may also use this to set their verbosity flags to a higher value or similar behaviors.
29+
/// </summary>
30+
internal class SetDiagnosticModeAction() : SynchronousCommandLineAction
2831
{
2932
public override int Invoke(ParseResult parseResult)
3033
{
31-
if (parseResult.IsDotnetBuiltInCommand())
32-
{
33-
var diagIsChildOfRoot = parseResult.RootCommandResult.Children.FirstOrDefault((s) => s is OptionResult opt && opt.Option == diagnosticOption) is not null;
34-
35-
// We found --diagnostic or -d, but we still need to determine whether the option should
36-
// be attached to the dotnet command or the subcommand.
37-
if (diagIsChildOfRoot)
38-
{
39-
Environment.SetEnvironmentVariable(CommandLoggingContext.Variables.Verbose, bool.TrueString);
40-
CommandLoggingContext.SetVerbose(true);
41-
Reporter.Reset();
42-
}
43-
}
44-
34+
Environment.SetEnvironmentVariable(CommandLoggingContext.Variables.Verbose, bool.TrueString);
35+
CommandLoggingContext.SetVerbose(true);
36+
Reporter.Reset();
4537
return 0;
4638
}
4739
}

src/Cli/dotnet/Parser.cs

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -197,18 +197,9 @@ private static RootCommand ConfigureCommandLine(RootCommand rootCommand)
197197

198198
rootCommand.SetAction(parseResult =>
199199
{
200-
if (parseResult.GetValue(DiagOption) && parseResult.Tokens.Count == 1)
201-
{
202-
// when user does not specify any args except of diagnostics ("dotnet -d"), we do nothing
203-
// as Program.ProcessArgs already enabled the diagnostic output
204-
return 0;
205-
}
206-
else
207-
{
208-
// when user does not specify any args (just "dotnet"), a usage needs to be printed
209-
parseResult.InvocationConfiguration.Output.WriteLine(CliUsage.HelpText);
210-
return 0;
211-
}
200+
// when user does not specify any args (just "dotnet"), a usage needs to be printed
201+
parseResult.InvocationConfiguration.Output.WriteLine(CliUsage.HelpText);
202+
return 0;
212203
});
213204

214205
return rootCommand;

0 commit comments

Comments
 (0)