Skip to content

package-add: Use PackageIdentityArgument #48402

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
40 changes: 2 additions & 38 deletions src/Cli/dotnet/Commands/Package/Add/PackageAddCommandParser.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,44 +13,8 @@ namespace Microsoft.DotNet.Cli.Commands.Package.Add;

internal static class PackageAddCommandParser
{
public static PackageIdentity ParsePackageIdentity(ArgumentResult packageArgResult)
{
// per the Arity of the CmdPackageArgument's Arity, we should have exactly one token -
// this is a safety net for if we change the Arity in the future and forget to update this parser.
if (packageArgResult.Tokens.Count != 1)
{
throw new ArgumentException($"Expected exactly one token, but got {packageArgResult.Tokens.Count}.");
}
var token = packageArgResult.Tokens[0].Value;
var indexOfAt = token.IndexOf('@');
if (indexOfAt == -1)
{
// no version specified, so we just return the package id
return new PackageIdentity(token, null);
}
// we have a version specified, so we need to split the token into id and version
else
{
var id = token[0..indexOfAt];
var versionString = token[(indexOfAt + 1)..];
if (SemanticVersion.TryParse(versionString, out var version))
{
return new PackageIdentity(id, new NuGetVersion(version.Major, version.Minor, version.Patch, version.ReleaseLabels, version.Metadata));
}
else
{
throw new ArgumentException(string.Format(CliCommandStrings.InvalidSemVerVersionString, versionString));
}
};
}

public static readonly Argument<PackageIdentity> CmdPackageArgument = new DynamicArgument<PackageIdentity>(CliCommandStrings.CmdPackage)
{
Description = CliCommandStrings.CmdPackageDescription,
Arity = ArgumentArity.ExactlyOne,
CustomParser = ParsePackageIdentity,

}.AddCompletions((context) =>
public static readonly Argument<PackageIdentity> CmdPackageArgument = CommonArguments.PackageIdentityArgument(true)
.AddCompletions((context) =>
{
// we should take --prerelease flags into account for version completion
var allowPrerelease = context.ParseResult.GetValue(PrereleaseOption);
Expand Down
10 changes: 5 additions & 5 deletions src/Cli/dotnet/CommonArguments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,19 @@ internal class CommonArguments
{
HelpName = "PACKAGE_ID",
Description = CliStrings.PackageIdentityArgumentDescription,
CustomParser = ParsePackageIdentity,
CustomParser = (ArgumentResult argumentResult) => ParsePackageIdentity(argumentResult.Tokens[0]?.Value),
Arity = requireArgument ? ArgumentArity.ExactlyOne : ArgumentArity.ZeroOrOne,
};

private static PackageIdentity? ParsePackageIdentity(ArgumentResult argumentResult)
private static PackageIdentity? ParsePackageIdentity(string packageIdentity)
{
if (argumentResult.Tokens.Count == 0)
if (string.IsNullOrEmpty(packageIdentity))
{
return null;
}

string[] splitToken = argumentResult.Tokens[0].Value.Split('@');
var (packageId, versionString) = (splitToken.ElementAtOrDefault(0), splitToken.ElementAtOrDefault(1));
string[] splitPackageIdentity = packageIdentity.Split('@');
var (packageId, versionString) = (splitPackageIdentity.ElementAtOrDefault(0), splitPackageIdentity.ElementAtOrDefault(1));

if (string.IsNullOrEmpty(packageId))
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1059,7 +1059,6 @@ _testhost_package_add() {
COMPREPLY=()

opts="--version --framework --no-restore --source --package-directory --interactive --prerelease --project --help"
opts="$opts $(${COMP_WORDS[0]} complete --position ${COMP_POINT} ${COMP_LINE} 2>/dev/null | tr '\n' ' ')"

if [[ $COMP_CWORD == "$1" ]]; then
COMPREPLY=( $(compgen -W "$opts" -- "$cur") )
Expand Down Expand Up @@ -2224,4 +2223,4 @@ _testhost_completions_script() {



complete -F _testhost testhost
complete -F _testhost testhost
Original file line number Diff line number Diff line change
Expand Up @@ -610,10 +610,6 @@ Register-ArgumentCompleter -Native -CommandName 'testhost' -ScriptBlock {
[CompletionResult]::new('--help', '-h', [CompletionResultType]::ParameterName, "Show command line help.")
)
$completions += $staticCompletions
$text = $commandAst.ToString()
$dotnetCompleteResults = @(dotnet complete --position $cursorPosition "$text") | Where-Object { $_ -NotMatch "^-|^/" }
$dynamicCompletions = $dotnetCompleteResults | Foreach-Object { [CompletionResult]::new($_, $_, [CompletionResultType]::ParameterValue, $_) }
$completions += $dynamicCompletions
break
}
'testhost;package;list' {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -596,7 +596,7 @@ _testhost() {
'--project=[The project file to operate on. If a file is not specified, the command will search the current directory for one.]: : ' \
'--help[Show command line help.]' \
'-h[Show command line help.]' \
':PACKAGE_NAME -- The package reference to add. This can be in the form of just the package identifier, for example '\''Newtonsoft.Json'\'', or a package identifier and version separated by '\''@'\'', for example '\''[email protected]'\'':->dotnet_dynamic_complete' \
':packageId -- Package reference in the form of a package identifier like '\''Newtonsoft.Json'\'' or package identifier and version separated by '\''@'\'' like '\''[email protected]'\''.: ' \
&& ret=0
case $state in
(dotnet_dynamic_complete)
Expand Down
Loading