Skip to content

Commit dda7732

Browse files
edvilmedsplaisted
authored andcommitted
Add --interactive option
1 parent aebf421 commit dda7732

17 files changed

+171
-6
lines changed

src/Cli/dotnet/CliStrings.resx

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -812,4 +812,10 @@ For a list of locations searched, specify the "-d" option before the tool name.<
812812
<value>Cannot specify --version when the package argument already contains a version.</value>
813813
<comment>{Locked="--version"}</comment>
814814
</data>
815+
<data name="YesOptionDescription" xml:space="preserve">
816+
<value>Overrides confirmation prompt with "yes" value. </value>
817+
</data>
818+
<data name="NoOptionDescription" xml:space="preserve">
819+
<value>Overrides confirmation prompt with "no" value.</value>
820+
</data>
815821
</root>

src/Cli/dotnet/Commands/Tool/Run/ToolRunCommandParser.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#nullable disable
55

66
using System.CommandLine;
7+
using Microsoft.DotNet.Cli;
78
using Microsoft.DotNet.Cli.Commands.Tool.Install;
89
using Microsoft.DotNet.Cli.Commands.Tool.Common;
910

@@ -42,6 +43,10 @@ internal static class ToolRunCommandParser
4243

4344
public static readonly Option<VerbosityOptions> FromSourceVerbosityOption = CommonOptions.VerbosityOption;
4445

46+
public static readonly Option<bool> FromSourceInteractiveOption = CommonOptions.InteractiveOption();
47+
48+
public static readonly Option<bool> FromSourceYesOption = CommonOptions.YesOption;
49+
4550
private static readonly Command Command = ConstructCommand();
4651

4752
public static Command GetCommand()
@@ -62,9 +67,10 @@ private static Command ConstructCommand()
6267
command.Options.Add(FromSourceSourceOption);
6368
command.Options.Add(FromSourceAddSourceOption);
6469
command.Options.Add(FromSourceVerbosityOption);
70+
command.Options.Add(FromSourceInteractiveOption);
71+
command.Options.Add(FromSourceYesOption);
6572

6673
command.Options.Add(ToolCommandRestorePassThroughOptions.IgnoreFailedSourcesOption);
67-
command.Options.Add(ToolCommandRestorePassThroughOptions.InteractiveRestoreOption);
6874

6975
command.SetAction((parseResult) => new ToolRunCommand(parseResult).Execute());
7076

src/Cli/dotnet/Commands/Tool/Run/ToolRunFromSourceCommand.cs

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,15 @@ namespace Microsoft.DotNet.Cli.Commands.Tool.Run
1313
internal class ToolRunFromSourceCommand(ParseResult result) : CommandBase(result)
1414
{
1515
private readonly string? _toolCommandName = result.GetValue(ToolRunCommandParser.CommandNameArgument);
16-
private readonly IEnumerable<string>? _forwardArguments = result.GetValue(ToolRunCommandParser.CommandArgument);
16+
private readonly IEnumerable<string> _forwardArguments = result.GetValue(ToolRunCommandParser.CommandArgument) ?? Enumerable.Empty<string>();
1717
private readonly bool _allowRollForward = result.GetValue(ToolRunCommandParser.RollForwardOption);
1818
private readonly string? _configFile = result.GetValue(ToolRunCommandParser.FromSourceConfigFile);
1919
private readonly string[] _sources = result.GetValue(ToolRunCommandParser.FromSourceSourceOption) ?? [];
2020
private readonly string[] _addSource = result.GetValue(ToolRunCommandParser.FromSourceAddSourceOption) ?? [];
2121
private readonly bool _ignoreFailedSources = result.GetValue(ToolCommandRestorePassThroughOptions.IgnoreFailedSourcesOption);
22-
private readonly bool _interactive = result.GetValue(ToolCommandRestorePassThroughOptions.InteractiveRestoreOption);
22+
private readonly bool _interactive = result.GetValue(ToolRunCommandParser.FromSourceInteractiveOption);
2323
private readonly VerbosityOptions _verbosity = result.GetValue(ToolRunCommandParser.FromSourceVerbosityOption);
24+
private readonly bool _yes = result.GetValue(ToolRunCommandParser.FromSourceYesOption);
2425

2526
public override int Execute()
2627
{
@@ -57,19 +58,27 @@ public override int Execute()
5758

5859
CommandSpec commandSpec = MuxerCommandSpecMaker.CreatePackageCommandSpecUsingMuxer(toolPackage.Command.Executable.ToString(), _forwardArguments);
5960
var command = CommandFactoryUsingResolver.Create(commandSpec);
60-
61-
Console.WriteLine('-' * Console.WindowWidth);
62-
6361
var result = command.Execute();
6462
return result.ExitCode;
6563
}
6664

6765
private bool UserAgreedToRunFromSource()
6866
{
67+
if (_yes)
68+
{
69+
return true;
70+
}
71+
72+
if (!_interactive)
73+
{
74+
return false;
75+
}
76+
6977
// TODO: Use a better way to ask for user input
7078
Console.Write(CliCommandStrings.ToolRunFromSourceUserConfirmationPrompt);
7179
bool userAccepted = Console.ReadKey().Key == ConsoleKey.Y;
7280
Console.WriteLine();
81+
Console.WriteLine(new string('-', CliCommandStrings.ToolRunFromSourceUserConfirmationPrompt.Length));
7382
return userAccepted;
7483
}
7584
}

src/Cli/dotnet/CommonOptions.cs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,20 @@ namespace Microsoft.DotNet.Cli;
1212

1313
internal static class CommonOptions
1414
{
15+
public static Option<bool> YesOption =
16+
new DynamicOption<bool>("--yes", "-y")
17+
{
18+
Description = CliStrings.YesOptionDescription,
19+
Arity = ArgumentArity.Zero
20+
};
21+
22+
public static Option<bool> NoOption =
23+
new DynamicOption<bool>("--no")
24+
{
25+
Description = CliStrings.NoOptionDescription,
26+
Arity = ArgumentArity.Zero
27+
};
28+
1529
public static Option<string[]> PropertiesOption =
1630
// these are all of the forms that the property switch can be understood by in MSBuild
1731
new ForwardedOption<string[]>("--property", "-property", "/property", "/p", "-p", "--p")

src/Cli/dotnet/xlf/CliStrings.cs.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/xlf/CliStrings.de.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/xlf/CliStrings.es.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/xlf/CliStrings.fr.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/xlf/CliStrings.it.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/Cli/dotnet/xlf/CliStrings.ja.xlf

Lines changed: 10 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)