Skip to content

Commit 6ffafb2

Browse files
author
Mike McLaughlin
committed
Upgrade to latest System.CommandLine version
1 parent 90c5cb4 commit 6ffafb2

28 files changed

+258
-367
lines changed

diagnostics.sln

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,16 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DotnetCounters.UnitTests",
5757
EndProject
5858
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "EventPipe.UnitTests", "src\tests\eventpipe\EventPipe.UnitTests.csproj", "{CED9ABBA-861E-4C0A-9359-22351208EF27}"
5959
EndProject
60+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Common", "Common", "{298AE119-6625-4604-BDE5-0765DC34C856}"
61+
ProjectSection(SolutionItems) = preProject
62+
src\Tools\Common\CommandExtensions.cs = src\Tools\Common\CommandExtensions.cs
63+
EndProjectSection
64+
EndProject
65+
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Commands", "Commands", "{C457CBCD-3A8D-4402-9A2B-693A0390D3F9}"
66+
ProjectSection(SolutionItems) = preProject
67+
src\Tools\Common\Commands\ProcessStatus.cs = src\Tools\Common\Commands\ProcessStatus.cs
68+
EndProjectSection
69+
EndProject
6070
Global
6171
GlobalSection(SolutionConfigurationPlatforms) = preSolution
6272
Checked|Any CPU = Checked|Any CPU
@@ -961,6 +971,8 @@ Global
961971
{936678B3-3392-4F4F-943C-B6A4BFCBAADC} = {B62728C8-1267-4043-B46F-5537BBAEC692}
962972
{E5A7DC6C-BF8D-418A-BCBD-094EB748FA82} = {03479E19-3F18-49A6-910A-F5041E27E7C0}
963973
{CED9ABBA-861E-4C0A-9359-22351208EF27} = {03479E19-3F18-49A6-910A-F5041E27E7C0}
974+
{298AE119-6625-4604-BDE5-0765DC34C856} = {B62728C8-1267-4043-B46F-5537BBAEC692}
975+
{C457CBCD-3A8D-4402-9A2B-693A0390D3F9} = {298AE119-6625-4604-BDE5-0765DC34C856}
964976
EndGlobalSection
965977
GlobalSection(ExtensibilityGlobals) = postSolution
966978
SolutionGuid = {46465737-C938-44FC-BE1A-4CE139EBB5E0}

eng/Versions.props

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@
2525
<MicrosoftDiagnosticsRuntimeVersion>1.1.46104</MicrosoftDiagnosticsRuntimeVersion>
2626
<MicrosoftDiaSymReaderNativePackageVersion>1.7.0</MicrosoftDiaSymReaderNativePackageVersion>
2727
<MicrosoftDiagnosticsTracingTraceEventVersion>2.0.44</MicrosoftDiagnosticsTracingTraceEventVersion>
28-
<SystemCommandLineExperimentalVersion>0.2.0-alpha.19254.1</SystemCommandLineExperimentalVersion>
29-
<SystemCommandLineRenderingVersion>0.2.0-alpha.19254.1</SystemCommandLineRenderingVersion>
28+
<SystemCommandLineExperimentalVersion>0.3.0-alpha.19602.1</SystemCommandLineExperimentalVersion>
29+
<SystemCommandLineRenderingVersion>0.3.0-alpha.19602.1</SystemCommandLineRenderingVersion>
3030
<SystemMemoryVersion>4.5.3</SystemMemoryVersion>
3131
<XUnitVersion>2.4.1</XUnitVersion>
3232
<XUnitAbstractionsVersion>2.0.3</XUnitAbstractionsVersion>

src/Microsoft.Diagnostics.Repl/Command/CommandProcessor.cs

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -118,32 +118,32 @@ private void BuildCommands(CommandLineBuilder rootBuilder, Type type)
118118
{
119119
command = new Command(commandAttribute.Name, commandAttribute.Help);
120120
var properties = new List<(PropertyInfo, Option)>();
121-
PropertyInfo argument = null;
121+
var arguments = new List<(PropertyInfo, Argument)>();
122122

123123
foreach (PropertyInfo property in type.GetProperties().Where(p => p.CanWrite))
124124
{
125125
var argumentAttribute = (ArgumentAttribute)property.GetCustomAttributes(typeof(ArgumentAttribute), inherit: false).SingleOrDefault();
126126
if (argumentAttribute != null)
127127
{
128-
if (argument != null) {
129-
throw new ArgumentException($"More than one ArgumentAttribute in command class: {type.Name}");
130-
}
131128
IArgumentArity arity = property.PropertyType.IsArray ? ArgumentArity.ZeroOrMore : ArgumentArity.ZeroOrOne;
132129

133-
command.Argument = new Argument {
130+
var argument = new Argument {
134131
Name = argumentAttribute.Name ?? property.Name.ToLowerInvariant(),
135132
Description = argumentAttribute.Help,
136133
ArgumentType = property.PropertyType,
137134
Arity = arity
138135
};
139-
argument = property;
136+
command.AddArgument(argument);
137+
arguments.Add((property, argument));
140138
}
141139
else
142140
{
143141
var optionAttribute = (OptionAttribute)property.GetCustomAttributes(typeof(OptionAttribute), inherit: false).SingleOrDefault();
144142
if (optionAttribute != null)
145143
{
146-
var option = new Option(optionAttribute.Name ?? BuildAlias(property.Name), optionAttribute.Help, new Argument { ArgumentType = property.PropertyType });
144+
var option = new Option(optionAttribute.Name ?? BuildAlias(property.Name), optionAttribute.Help) {
145+
Argument = new Argument { ArgumentType = property.PropertyType }
146+
};
147147
command.AddOption(option);
148148
properties.Add((property, option));
149149

@@ -160,7 +160,7 @@ private void BuildCommands(CommandLineBuilder rootBuilder, Type type)
160160
}
161161
}
162162

163-
var handler = new Handler(this, commandAttribute.AliasExpansion, argument, properties, type);
163+
var handler = new Handler(this, commandAttribute.AliasExpansion, arguments, properties, type);
164164
_commandHandlers.Add(command.Name, handler);
165165
command.Handler = handler;
166166

@@ -201,18 +201,18 @@ class Handler : ICommandHandler
201201
{
202202
private readonly CommandProcessor _commandProcessor;
203203
private readonly string _aliasExpansion;
204-
private readonly PropertyInfo _argument;
204+
private readonly IEnumerable<(PropertyInfo Property, Argument Argument)> _arguments;
205205
private readonly IEnumerable<(PropertyInfo Property, Option Option)> _properties;
206206

207207
private readonly ConstructorInfo _constructor;
208208
private readonly MethodInfo _methodInfo;
209209
private readonly MethodInfo _methodInfoHelp;
210210

211-
public Handler(CommandProcessor commandProcessor, string aliasExpansion, PropertyInfo argument, IEnumerable<(PropertyInfo, Option)> properties, Type type)
211+
public Handler(CommandProcessor commandProcessor, string aliasExpansion, IEnumerable<(PropertyInfo, Argument)> arguments, IEnumerable<(PropertyInfo, Option)> properties, Type type)
212212
{
213213
_commandProcessor = commandProcessor;
214214
_aliasExpansion = aliasExpansion;
215-
_argument = argument;
215+
_arguments = arguments;
216216
_properties = properties;
217217

218218
_constructor = type.GetConstructors().SingleOrDefault((info) => info.GetParameters().Length == 0) ??
@@ -303,19 +303,19 @@ private void SetProperties(InvocationContext context, object instance)
303303
property.Property.SetValue(instance, value);
304304
}
305305

306-
if (context != null && _argument != null)
306+
if (context != null)
307307
{
308-
object value = null;
309-
ArgumentResult result = context.ParseResult.CommandResult.ArgumentResult;
310-
switch (result)
308+
IEnumerable<ArgumentResult> argumentResults = context.ParseResult.CommandResult.Children.OfType<ArgumentResult>();
309+
310+
foreach ((PropertyInfo Property, Argument Argument) argument in _arguments)
311311
{
312-
case SuccessfulArgumentResult successful:
313-
value = successful.Value;
314-
break;
315-
case FailedArgumentResult failed:
316-
throw new InvalidOperationException(failed.ErrorMessage);
312+
ArgumentResult argumentResult = argumentResults.Where((result) => result.Argument == argument.Argument).SingleOrDefault();
313+
if (argumentResult != null)
314+
{
315+
object value = argumentResult.GetValueOrDefault();
316+
argument.Property.SetValue(instance, value);
317+
}
317318
}
318-
_argument.SetValue(instance, value);
319319
}
320320
}
321321

src/Tools/Common/CommandExtensions.cs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Licensed to the .NET Foundation under one or more agreements.
2+
// The .NET Foundation licenses this file to you under the MIT license.
3+
// See the LICENSE file in the project root for more information.
4+
5+
using System.Collections.Generic;
6+
using System.CommandLine;
7+
8+
namespace Microsoft.Tools.Common
9+
{
10+
public static class CommandExtenions
11+
{
12+
public static Command AddOptions(this Command command, params Option[] options)
13+
{
14+
foreach (Option option in options)
15+
{
16+
command.AddOption(option);
17+
}
18+
return command;
19+
}
20+
21+
public static Command AddArguments(this Command command, params Argument[] arguments)
22+
{
23+
foreach (Argument argument in arguments)
24+
{
25+
command.AddArgument(argument);
26+
}
27+
return command;
28+
}
29+
}
30+
}

src/Tools/Common/CommandLineException.cs

Lines changed: 0 additions & 28 deletions
This file was deleted.

src/Tools/Common/Commands/ProcessStatus.cs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,23 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using Microsoft.Diagnostics.Tools.RuntimeClient;
56
using System;
67
using System.CommandLine;
7-
using System.Diagnostics;
8+
using System.CommandLine.Invocation;
89
using System.Linq;
910
using System.Text;
10-
11-
using Microsoft.Diagnostics.Tools.RuntimeClient;
11+
using Process = System.Diagnostics.Process;
1212

1313
namespace Microsoft.Internal.Common.Commands
1414
{
1515
public class ProcessStatusCommandHandler
1616
{
17+
public static Command ProcessStatusCommand(string description) =>
18+
new Command(name: "ps", description) {
19+
Handler = CommandHandler.Create<IConsole>(PrintProcessStatus)
20+
};
21+
1722
/// <summary>
1823
/// Print the current list of available .NET core processes for diagnosis and their statuses
1924
/// </summary>

src/Tools/Common/ConsoleCancellation.cs

Lines changed: 0 additions & 45 deletions
This file was deleted.

src/Tools/Common/DebugUtil.cs

Lines changed: 0 additions & 25 deletions
This file was deleted.

0 commit comments

Comments
 (0)