44#nullable disable
55
66using System ;
7+ using System . CommandLine ;
78using System . Globalization ;
8- using Microsoft . Extensions . CommandLineUtils ;
99using NuGet . Commands ;
10- using NuGet . Common ;
1110using NuGet . Configuration ;
1211using NuGet . Credentials ;
1312
1413namespace NuGet . CommandLine . XPlat
1514{
1615 internal static class DeleteCommand
1716 {
18- public static void Register ( CommandLineApplication app , Func < ILogger > getLogger )
17+ private static readonly Option < string > SourceOption = new Option < string > ( "--source" , "-s" )
1918 {
20- app . Command ( "delete" , delete =>
19+ Arity = ArgumentArity . ZeroOrOne ,
20+ Description = Strings . Source_Description ,
21+ } ;
22+
23+ private static readonly Option < bool > NonInteractiveOption = new Option < bool > ( "--non-interactive" )
24+ {
25+ Arity = ArgumentArity . Zero ,
26+ Description = Strings . NonInteractive_Description ,
27+ } ;
28+
29+ private static readonly Option < string > ApiKeyOption = new Option < string > ( "--api-key" , "-k" )
30+ {
31+ Arity = ArgumentArity . ZeroOrOne ,
32+ Description = Strings . ApiKey_Description ,
33+ } ;
34+
35+ private static readonly Option < bool > NoServiceEndpointOption = new Option < bool > ( "--no-service-endpoint" )
36+ {
37+ Arity = ArgumentArity . Zero ,
38+ Description = Strings . NoServiceEndpoint_Description ,
39+ } ;
40+
41+ private static readonly Option < bool > InteractiveOption = new Option < bool > ( "--interactive" )
42+ {
43+ Arity = ArgumentArity . Zero ,
44+ Description = Strings . NuGetXplatCommand_Interactive ,
45+ } ;
46+
47+ private static readonly Argument < string > PackageIdArgument = new Argument < string > ( "PackageId" )
48+ {
49+ Arity = ArgumentArity . ExactlyOne ,
50+ Description = Strings . Delete_PackageIdAndVersion_Description ,
51+ } ;
52+
53+ private static readonly Argument < string > PackageVersionArgument = new Argument < string > ( "PackageVersion" )
54+ {
55+ Arity = ArgumentArity . ExactlyOne ,
56+ Description = Strings . Delete_PackageIdAndVersion_Description ,
57+ } ;
58+
59+ internal static void Register ( Command parent , Func < ILoggerWithColor > getLogger )
60+ {
61+ var deleteCmd = new Command ( "delete" , Strings . Delete_Description ) ;
62+
63+ deleteCmd . Options . Add ( SourceOption ) ;
64+ deleteCmd . Options . Add ( NonInteractiveOption ) ;
65+ deleteCmd . Options . Add ( ApiKeyOption ) ;
66+ deleteCmd . Options . Add ( NoServiceEndpointOption ) ;
67+ deleteCmd . Options . Add ( InteractiveOption ) ;
68+ deleteCmd . Arguments . Add ( PackageIdArgument ) ;
69+ deleteCmd . Arguments . Add ( PackageVersionArgument ) ;
70+
71+ deleteCmd . SetAction ( async ( parseResult , cancellationToken ) =>
2172 {
22- delete . Description = Strings . Delete_Description ;
23- delete . HelpOption ( XPlatUtility . HelpOption ) ;
24-
25- delete . Option (
26- CommandConstants . ForceEnglishOutputOption ,
27- Strings . ForceEnglishOutput_Description ,
28- CommandOptionType . NoValue ) ;
29-
30- var source = delete . Option (
31- "-s|--source <source>" ,
32- Strings . Source_Description ,
33- CommandOptionType . SingleValue ) ;
34-
35- var nonInteractive = delete . Option (
36- "--non-interactive" ,
37- Strings . NonInteractive_Description ,
38- CommandOptionType . NoValue ) ;
39-
40- var apikey = delete . Option (
41- "-k|--api-key <apiKey>" ,
42- Strings . ApiKey_Description ,
43- CommandOptionType . SingleValue ) ;
44-
45- var arguments = delete . Argument (
46- "[root]" ,
47- Strings . Delete_PackageIdAndVersion_Description ,
48- multipleValues : true ) ;
49-
50- var noServiceEndpointDescription = delete . Option (
51- "--no-service-endpoint" ,
52- Strings . NoServiceEndpoint_Description ,
53- CommandOptionType . NoValue ) ;
54-
55- var interactive = delete . Option (
56- "--interactive" ,
57- Strings . NuGetXplatCommand_Interactive ,
58- CommandOptionType . NoValue ) ;
59-
60- delete . OnExecute ( async ( ) =>
61- {
62- if ( arguments . Values . Count < 2 )
63- {
64- throw new ArgumentException ( Strings . Delete_MissingArguments ) ;
65- }
66-
67- string packageId = arguments . Values [ 0 ] ;
68- string packageVersion = arguments . Values [ 1 ] ;
69- string sourcePath = source . Value ( ) ;
70- string apiKeyValue = apikey . Value ( ) ;
71- bool nonInteractiveValue = nonInteractive . HasValue ( ) ;
72- bool noServiceEndpoint = noServiceEndpointDescription . HasValue ( ) ;
73-
74- DefaultCredentialServiceUtility . SetupDefaultCredentialService ( getLogger ( ) , ! interactive . HasValue ( ) ) ;
73+ string packageId = parseResult . GetValue ( PackageIdArgument ) ;
74+ string packageVersion = parseResult . GetValue ( PackageVersionArgument ) ;
75+ string sourcePath = parseResult . GetValue ( SourceOption ) ;
76+ string apiKeyValue = parseResult . GetValue ( ApiKeyOption ) ;
77+ bool nonInteractiveValue = parseResult . GetValue ( NonInteractiveOption ) ;
78+ bool noServiceEndpoint = parseResult . GetValue ( NoServiceEndpointOption ) ;
79+ bool interactiveValue = parseResult . GetValue ( InteractiveOption ) ;
80+
81+ DefaultCredentialServiceUtility . SetupDefaultCredentialService ( getLogger ( ) , ! interactiveValue ) ;
7582
7683#pragma warning disable CS0618 // Type or member is obsolete
77- PackageSourceProvider sourceProvider = new PackageSourceProvider ( XPlatUtility . GetSettingsForCurrentWorkingDirectory ( ) , enablePackageSourcesChangedEvent : false ) ;
84+ PackageSourceProvider sourceProvider = new PackageSourceProvider ( XPlatUtility . GetSettingsForCurrentWorkingDirectory ( ) , enablePackageSourcesChangedEvent : false ) ;
7885#pragma warning restore CS0618 // Type or member is obsolete
7986
80- await DeleteRunner . Run (
81- sourceProvider . Settings ,
82- sourceProvider ,
83- packageId ,
84- packageVersion ,
85- sourcePath ,
86- apiKeyValue ,
87- nonInteractiveValue ,
88- noServiceEndpoint ,
89- Confirm ,
90- getLogger ( ) ) ;
91-
92- return 0 ;
93- } ) ;
87+ await DeleteRunner . Run (
88+ sourceProvider . Settings ,
89+ sourceProvider ,
90+ packageId ,
91+ packageVersion ,
92+ sourcePath ,
93+ apiKeyValue ,
94+ nonInteractiveValue ,
95+ noServiceEndpoint ,
96+ Confirm ,
97+ getLogger ( ) ) ;
98+
99+ return 0 ;
94100 } ) ;
101+
102+ parent . Subcommands . Add ( deleteCmd ) ;
95103 }
96104
97105 private static bool Confirm ( string description )
@@ -103,7 +111,7 @@ private static bool Confirm(string description)
103111 Console . ForegroundColor = ConsoleColor . Yellow ;
104112 Console . WriteLine ( string . Format ( CultureInfo . CurrentCulture , Strings . ConsoleConfirmMessage , description ) ) ;
105113 var result = Console . ReadLine ( ) ;
106- return result . StartsWith ( Strings . ConsoleConfirmMessageAccept , StringComparison . OrdinalIgnoreCase ) ;
114+ return result != null && result . StartsWith ( Strings . ConsoleConfirmMessageAccept , StringComparison . OrdinalIgnoreCase ) ;
107115 }
108116 finally
109117 {
0 commit comments