Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 25 additions & 16 deletions src/Cli/dotnet/ParserOptionActions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

using System.CommandLine;
using System.CommandLine.Invocation;
using Microsoft.DotNet.Cli.CommandLine;
using Microsoft.DotNet.Cli.Commands.Workload;
using Microsoft.DotNet.Cli.Extensions;
using Microsoft.DotNet.Cli.Help;
Expand All @@ -24,24 +25,26 @@ internal abstract class InvocableOptionAction(Option option) : SynchronousComman
public Option Option { get; } = option;
}

internal class HandleDiagnosticAction(Option option) : InvocableOptionAction(option)
internal class HandleDiagnosticAction(Option<bool> option) : InvocableOptionAction(option)
{
public override bool Terminating => false;

// S.CL will always invoke this option action because:
// 1. A boolean option has a default value of `false`.
// 2. The option has Terminating set to `false`.
// When a default value exists (non-null) and the option is not terminating, the option action is always invoked.
public override int Invoke(ParseResult parseResult)
{
// Required because of: https://github.com/dotnet/command-line-api/pull/2708
// S.CL now always invokes non-terminating option actions for implicit (default-valued) options.
// Meaning, this action always runs independent of the option being provided or not.
if (parseResult.GetResult(Option) is not { } result || result.Implicit
// This check is necessary as per comment above.
if (!parseResult.HasOption(Option) || !parseResult.GetValue(option)
// Only set verbose output on built-in commands.
|| !parseResult.IsDotnetBuiltInCommand())
{
Comment thread
MiYanni marked this conversation as resolved.
return 0;
}

// Determine whether the diagnostic option should be attached to the dotnet command or the subcommand.
if (DiagOptionPrecedesSubcommand(parseResult.Tokens.Select(t => t.Value), parseResult.RootSubCommandResult()))
if (OptionPrecedesSubcommand(parseResult.Tokens.Select(t => t.Value), parseResult.RootSubCommandResult()))
{
Environment.SetEnvironmentVariable(CommandLoggingContext.Variables.Verbose, bool.TrueString);
CommandLoggingContext.SetVerbose(true);
Expand All @@ -58,7 +61,7 @@ public override int Invoke(ParseResult parseResult)
return 0;
}

private static bool DiagOptionPrecedesSubcommand(IEnumerable<string> tokens, string subCommand)
private bool OptionPrecedesSubcommand(IEnumerable<string> tokens, string subCommand)
{
if (string.IsNullOrEmpty(subCommand))
{
Expand All @@ -72,8 +75,7 @@ private static bool DiagOptionPrecedesSubcommand(IEnumerable<string> tokens, str
return false;
}

if (Parser.RootCommand.DiagOption.Name == token
|| Parser.RootCommand.DiagOption.Aliases.Contains(token))
if (Option.Name == token || Option.Aliases.Contains(token))
{
return true;
}
Expand Down Expand Up @@ -101,14 +103,15 @@ public override int Invoke(ParseResult parseResult)
}
}

internal class PrintVersionAction(Option option) : InvocableOptionAction(option)
internal class PrintVersionAction(Option<bool> option) : InvocableOptionAction(option)
{
public override bool Terminating => true;

public override int Invoke(ParseResult parseResult)
{
// Only print for top-level commands.
if (!parseResult.IsTopLevelDotnetCommand())
if (!parseResult.HasOption(Option) || !parseResult.GetValue(option)
// Only print for top-level commands.
|| !parseResult.IsTopLevelDotnetCommand())
{
return 0;
}
Expand All @@ -119,14 +122,15 @@ public override int Invoke(ParseResult parseResult)
}
}

internal class PrintInfoAction(Option option) : InvocableOptionAction(option)
internal class PrintInfoAction(Option<bool> option) : InvocableOptionAction(option)
{
public override bool Terminating => true;

public override int Invoke(ParseResult parseResult)
{
// Only print for top-level commands.
if (!parseResult.IsTopLevelDotnetCommand())
if (!parseResult.HasOption(Option) || !parseResult.GetValue(option)
// Only print for top-level commands.
|| !parseResult.IsTopLevelDotnetCommand())
{
return 0;
}
Expand Down Expand Up @@ -162,12 +166,17 @@ public override int Invoke(ParseResult parseResult)
}
}

internal class PrintCliSchemaAction(Option option) : InvocableOptionAction(option)
internal class PrintCliSchemaAction(Option<bool> option) : InvocableOptionAction(option)
{
public override bool Terminating => true;

public override int Invoke(ParseResult parseResult)
{
if (!parseResult.HasOption(Option) || !parseResult.GetValue(option))
{
return 0;
}

CliSchema.PrintCliSchema(parseResult, parseResult.InvocationConfiguration.Output, Program.TelemetryInstance);

return 0;
Expand Down
Loading