Skip to content

Commit 4c9b747

Browse files
committed
Make package identity argument work better with nullability
1 parent 729fe49 commit 4c9b747

File tree

6 files changed

+17
-13
lines changed

6 files changed

+17
-13
lines changed

src/Cli/dotnet/Commands/Package/Add/PackageAddCommandParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace Microsoft.DotNet.Cli.Commands.Package.Add;
1515

1616
internal static class PackageAddCommandParser
1717
{
18-
public static readonly Argument<PackageIdentity> CmdPackageArgument = CommonArguments.PackageIdentityArgument(true)
18+
public static readonly Argument<PackageIdentity> CmdPackageArgument = CommonArguments.RequiredPackageIdentityArgument()
1919
.AddCompletions((context) =>
2020
{
2121
// we should take --prerelease flags into account for version completion

src/Cli/dotnet/Commands/Tool/Execute/ToolExecuteCommand.cs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ namespace Microsoft.DotNet.Cli.Commands.Tool.Execute
1515
{
1616
internal class ToolExecuteCommand(ParseResult result) : CommandBase(result)
1717
{
18-
private readonly PackageIdentity? _packageToolIdentityArgument = result.GetValue(ToolExecuteCommandParser.PackageIdentityArgument);
18+
private readonly PackageIdentity _packageToolIdentityArgument = result.GetRequiredValue(ToolExecuteCommandParser.PackageIdentityArgument);
1919
private readonly IEnumerable<string> _forwardArguments = result.GetValue(ToolExecuteCommandParser.CommandArgument) ?? Enumerable.Empty<string>();
2020
private readonly bool _allowRollForward = result.GetValue(ToolExecuteCommandParser.RollForwardOption);
2121
private readonly string? _configFile = result.GetValue(ToolExecuteCommandParser.ConfigOption);
@@ -29,12 +29,6 @@ internal class ToolExecuteCommand(ParseResult result) : CommandBase(result)
2929

3030
public override int Execute()
3131
{
32-
if (_packageToolIdentityArgument is null)
33-
{
34-
// System.CommandLine will throw an error if the argument is not provided, but we can still check here for clarity.
35-
return 1;
36-
}
37-
3832
if (!UserAgreedToRunFromSource())
3933
{
4034
throw new GracefulException(CliCommandStrings.ToolRunFromSourceUserConfirmationFailed, isUserError: true);

src/Cli/dotnet/Commands/Tool/Execute/ToolExecuteCommandParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace Microsoft.DotNet.Cli.Commands.Tool.Execute
1313
internal static class ToolExecuteCommandParser
1414

1515
{
16-
public static readonly Argument<PackageIdentity?> PackageIdentityArgument = ToolInstallCommandParser.PackageIdentityArgument;
16+
public static readonly Argument<PackageIdentity> PackageIdentityArgument = ToolInstallCommandParser.PackageIdentityArgument;
1717

1818
public static readonly Argument<IEnumerable<string>> CommandArgument = new("commandArguments")
1919
{

src/Cli/dotnet/Commands/Tool/Install/ToolInstallCommandParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ namespace Microsoft.DotNet.Cli.Commands.Tool.Install;
1212

1313
internal static class ToolInstallCommandParser
1414
{
15-
public static readonly Argument<PackageIdentity?> PackageIdentityArgument = CommonArguments.PackageIdentityArgument();
15+
public static readonly Argument<PackageIdentity> PackageIdentityArgument = CommonArguments.RequiredPackageIdentityArgument();
1616

1717
public static readonly Option<string> VersionOption = new("--version")
1818
{

src/Cli/dotnet/Commands/Tool/Update/ToolUpdateCommandParser.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ namespace Microsoft.DotNet.Cli.Commands.Tool.Update;
1010

1111
internal static class ToolUpdateCommandParser
1212
{
13-
public static readonly Argument<PackageIdentity?> PackageIdentityArgument = CommonArguments.PackageIdentityArgument(requireArgument: false);
13+
public static readonly Argument<PackageIdentity?> PackageIdentityArgument = CommonArguments.OptionalPackageIdentityArgument();
1414

1515
public static readonly Option<bool> UpdateAllOption = ToolAppliedOption.UpdateAllOption;
1616

src/Cli/dotnet/CommonArguments.cs

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,25 @@ namespace Microsoft.DotNet.Cli
1111
{
1212
internal class CommonArguments
1313
{
14-
public static DynamicArgument<PackageIdentity?> PackageIdentityArgument(bool requireArgument = true) =>
14+
public static DynamicArgument<PackageIdentity?> OptionalPackageIdentityArgument() =>
1515
new("packageId")
1616
{
1717
HelpName = "PACKAGE_ID",
1818
Description = CliStrings.PackageIdentityArgumentDescription,
1919
CustomParser = (ArgumentResult argumentResult) => ParsePackageIdentityWithVersionSeparator(argumentResult.Tokens[0]?.Value),
20-
Arity = requireArgument ? ArgumentArity.ExactlyOne : ArgumentArity.ZeroOrOne,
20+
Arity = ArgumentArity.ZeroOrOne,
2121
};
2222

23+
public static DynamicArgument<PackageIdentity> RequiredPackageIdentityArgument() =>
24+
new("packageId")
25+
{
26+
HelpName = "PACKAGE_ID",
27+
Description = CliStrings.PackageIdentityArgumentDescription,
28+
CustomParser = (ArgumentResult argumentResult) => ParsePackageIdentityWithVersionSeparator(argumentResult.Tokens[0]?.Value),
29+
Arity = ArgumentArity.ExactlyOne,
30+
};
31+
32+
2333
private static PackageIdentity? ParsePackageIdentityWithVersionSeparator(string? packageIdentity, char versionSeparator = '@')
2434
{
2535
if (string.IsNullOrEmpty(packageIdentity))

0 commit comments

Comments
 (0)