|
4 | 4 | using System.Collections.Generic;
|
5 | 5 | using System.Collections.Immutable;
|
6 | 6 | using System.CommandLine;
|
7 |
| -using System.CommandLine.Invocation; |
8 |
| -using System.CommandLine.NamingConventionBinder; |
9 |
| -using System.CommandLine.Parsing; |
10 | 7 | using System.IO;
|
11 | 8 | using System.Linq;
|
12 | 9 | using System.Net;
|
@@ -62,77 +59,82 @@ private static string GetSourceLinkVersion()
|
62 | 59 | return attribute.InformationalVersion.Split('+').First();
|
63 | 60 | }
|
64 | 61 |
|
65 |
| - private static CliRootCommand GetRootCommand() |
| 62 | + private static RootCommand GetRootCommand() |
66 | 63 | {
|
67 |
| - var authArg = new CliOption<string>("--auth", "-a") |
| 64 | + var pathArg = new Argument<string>("path") |
| 65 | + { |
| 66 | + Description = "Path to an assembly or .pdb" |
| 67 | + }; |
| 68 | + var authArg = new Option<string>("--auth", "-a") |
68 | 69 | {
|
69 | 70 | Description = "Authentication method"
|
70 | 71 | };
|
71 | 72 | authArg.AcceptOnlyFromAmong(AuthenticationMethod.Basic);
|
72 | 73 |
|
73 |
| - var userArg = new CliOption<string>("--user", "-u") |
| 74 | + var authEncodingArg = new Option<Encoding>("--auth-encoding", "-e") |
| 75 | + { |
| 76 | + CustomParser = arg => Encoding.GetEncoding(arg.Tokens.Single().Value), |
| 77 | + Description = "Encoding to use for authentication value" |
| 78 | + }; |
| 79 | + |
| 80 | + var userArg = new Option<string>("--user", "-u") |
74 | 81 | {
|
75 | 82 | Description = "Username to use to authenticate",
|
76 | 83 | Arity = ArgumentArity.ExactlyOne
|
77 | 84 | };
|
78 | 85 |
|
79 |
| - var passwordArg = new CliOption<string>("--password", "-p") |
| 86 | + var passwordArg = new Option<string>("--password", "-p") |
80 | 87 | {
|
81 | 88 | Description = "Password to use to authenticate",
|
82 | 89 | Arity = ArgumentArity.ExactlyOne
|
83 | 90 | };
|
84 | 91 |
|
85 |
| - var offlineArg = new CliOption<bool>("--offline") |
| 92 | + var offlineArg = new Option<bool>("--offline") |
86 | 93 | {
|
87 | 94 | Description = "Offline mode - skip validation of sourcelink URL targets"
|
88 | 95 | };
|
89 | 96 |
|
90 |
| - var test = new CliCommand("test", "TODO") |
| 97 | + var test = new Command("test", "TODO") |
91 | 98 | {
|
92 |
| - new CliArgument<string>("path") |
93 |
| - { |
94 |
| - Description = "Path to an assembly or .pdb" |
95 |
| - }, |
| 99 | + pathArg, |
96 | 100 | authArg,
|
97 |
| - new CliOption<Encoding>("--auth-encoding", "-e") |
98 |
| - { |
99 |
| - CustomParser = arg => Encoding.GetEncoding(arg.Tokens.Single().Value), |
100 |
| - Description = "Encoding to use for authentication value" |
101 |
| - }, |
| 101 | + authEncodingArg, |
102 | 102 | userArg,
|
103 | 103 | passwordArg,
|
104 | 104 | offlineArg,
|
105 | 105 | };
|
106 |
| - test.Action = CommandHandler.Create<string, string?, Encoding?, string?, string?, bool, ParseResult>(TestAsync); |
| 106 | + |
| 107 | + test.SetAction((parseResult, cancellationToken) => |
| 108 | + { |
| 109 | + string path = parseResult.GetValue(pathArg)!; |
| 110 | + string? authMethod = parseResult.GetValue(authArg); |
| 111 | + Encoding? authEncoding = parseResult.GetValue(authEncodingArg); |
| 112 | + string? user = parseResult.GetValue(userArg); |
| 113 | + string? password = parseResult.GetValue(passwordArg); |
| 114 | + bool offline = parseResult.GetValue(offlineArg); |
| 115 | + |
| 116 | + return TestAsync(path, authMethod, authEncoding, user, password, offline, parseResult, cancellationToken); |
| 117 | + }); |
107 | 118 |
|
108 |
| - var printJson = new CliCommand("print-json", "Print Source Link JSON stored in the PDB") |
| 119 | + var printJson = new Command("print-json", "Print Source Link JSON stored in the PDB") |
109 | 120 | {
|
110 |
| - new CliArgument<string>("path") |
111 |
| - { |
112 |
| - Description = "Path to an assembly or .pdb" |
113 |
| - } |
| 121 | + pathArg |
114 | 122 | };
|
115 |
| - printJson.Action = CommandHandler.Create<string, ParseResult>(PrintJsonAsync); |
| 123 | + printJson.SetAction((parseResult, ct) => PrintJsonAsync(parseResult.GetValue(pathArg)!, parseResult)); |
116 | 124 |
|
117 |
| - var printDocuments = new CliCommand("print-documents", "TODO") |
| 125 | + var printDocuments = new Command("print-documents", "TODO") |
118 | 126 | {
|
119 |
| - new CliArgument<string>("path") |
120 |
| - { |
121 |
| - Description = "Path to an assembly or .pdb" |
122 |
| - } |
| 127 | + pathArg |
123 | 128 | };
|
124 |
| - printDocuments.Action = CommandHandler.Create<string, ParseResult>(PrintDocumentsAsync); |
| 129 | + printDocuments.SetAction((parseResult, ct) => PrintDocumentsAsync(parseResult.GetValue(pathArg)!, parseResult)); |
125 | 130 |
|
126 |
| - var printUrls = new CliCommand("print-urls", "TODO") |
| 131 | + var printUrls = new Command("print-urls", "TODO") |
127 | 132 | {
|
128 |
| - new CliArgument<string>("path") |
129 |
| - { |
130 |
| - Description = "Path to an assembly or .pdb" |
131 |
| - } |
| 133 | + pathArg |
132 | 134 | };
|
133 |
| - printUrls.Action = CommandHandler.Create<string, ParseResult>(PrintUrlsAsync); |
| 135 | + printUrls.SetAction((parseResult, ct) => PrintUrlsAsync(parseResult.GetValue(pathArg)!, parseResult)); |
134 | 136 |
|
135 |
| - var root = new CliRootCommand() |
| 137 | + var root = new RootCommand() |
136 | 138 | {
|
137 | 139 | test,
|
138 | 140 | printJson,
|
@@ -176,20 +178,14 @@ private static async Task<int> TestAsync(
|
176 | 178 | string? user,
|
177 | 179 | string? password,
|
178 | 180 | bool offline,
|
179 |
| - ParseResult parseResult) |
| 181 | + ParseResult parseResult, |
| 182 | + CancellationToken cancellationToken) |
180 | 183 | {
|
181 | 184 | var authenticationHeader = (authMethod != null) ? GetAuthenticationHeader(authMethod, authEncoding ?? Encoding.ASCII, user!, password!) : null;
|
182 | 185 |
|
183 |
| - var cancellationSource = new CancellationTokenSource(); |
184 |
| - Console.CancelKeyPress += (sender, e) => |
185 |
| - { |
186 |
| - e.Cancel = true; |
187 |
| - cancellationSource.Cancel(); |
188 |
| - }; |
189 |
| - |
190 | 186 | try
|
191 | 187 | {
|
192 |
| - return await new Program(parseResult).TestAsync(path, authenticationHeader, offline, cancellationSource.Token).ConfigureAwait(false); |
| 188 | + return await new Program(parseResult).TestAsync(path, authenticationHeader, offline, cancellationToken).ConfigureAwait(false); |
193 | 189 | }
|
194 | 190 | catch (OperationCanceledException)
|
195 | 191 | {
|
|
0 commit comments