Skip to content

Commit f18e789

Browse files
Upgrades to System.CommandLine beta 5. Closes #1265 (#1297)
1 parent 03d242d commit f18e789

25 files changed

+521
-549
lines changed

DevProxy.Abstractions/DevProxy.Abstractions.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
<PackageReference Include="Microsoft.OpenApi.Readers" Version="1.6.24" />
2323
<PackageReference Include="Newtonsoft.Json.Schema" Version="4.0.1" />
2424
<PackageReference Include="Prompty.Core" Version="0.2.2-beta" />
25-
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
25+
<PackageReference Include="System.CommandLine" Version="2.0.0-beta5.25306.1" />
2626
<PackageReference Include="Unobtanium.Web.Proxy" Version="0.1.5" />
2727
</ItemGroup>
2828

DevProxy.Abstractions/Extensions/CommandLineExtensions.cs

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace System.CommandLine.Parsing;
88

99
public static class CommandLineExtensions
1010
{
11-
public static T? GetValueForOption<T>(this ParseResult parseResult, string optionName, IReadOnlyList<Option> options)
11+
public static T? GetValue<T>(this ParseResult parseResult, string optionName, IList<Option> options)
1212
{
1313
ArgumentNullException.ThrowIfNull(parseResult);
1414

@@ -20,7 +20,21 @@ public static class CommandLineExtensions
2020
throw new InvalidOperationException($"Could not find option with name {optionName} and value type {typeof(T).Name}");
2121
}
2222

23-
return parseResult.GetValueForOption(option);
23+
return parseResult.GetValue(option);
24+
}
25+
26+
public static T? GetValueOrDefault<T>(this ParseResult parseResult, string optionName)
27+
{
28+
ArgumentNullException.ThrowIfNull(parseResult);
29+
30+
try
31+
{
32+
return parseResult.GetValue<T>(optionName);
33+
}
34+
catch (Exception ex) when (ex is InvalidCastException or ArgumentException or InvalidOperationException)
35+
{
36+
return default;
37+
}
2438
}
2539

2640
public static IEnumerable<T> OrderByName<T>(this IEnumerable<T> symbols) where T : Symbol
@@ -37,7 +51,7 @@ public static void AddCommands(this Command command, IEnumerable<Command> subcom
3751

3852
foreach (var subcommand in subcommands)
3953
{
40-
command.AddCommand(subcommand);
54+
command.Add(subcommand);
4155
}
4256
}
4357

@@ -48,7 +62,7 @@ public static void AddOptions(this Command command, IEnumerable<Option> options)
4862

4963
foreach (var option in options)
5064
{
51-
command.AddOption(option);
65+
command.Add(option);
5266
}
5367
}
5468

DevProxy.Abstractions/Proxy/ProxyEvents.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44

55
using DevProxy.Abstractions.Utils;
66
using System.CommandLine;
7-
using System.CommandLine.Invocation;
87
using System.Text.Json.Serialization;
98
using Titanium.Web.Proxy.EventArguments;
109

@@ -48,12 +47,10 @@ public class InitArgs
4847
public required IServiceProvider ServiceProvider { get; init; }
4948
}
5049

51-
public class OptionsLoadedArgs(InvocationContext context, IReadOnlyList<Option> options)
50+
public class OptionsLoadedArgs(ParseResult parseResult)
5251
{
53-
public InvocationContext Context { get; set; } = context ??
54-
throw new ArgumentNullException(nameof(context));
55-
public IReadOnlyList<Option> Options { get; set; } = options ??
56-
throw new ArgumentNullException(nameof(options));
52+
public ParseResult ParseResult { get; set; } = parseResult ??
53+
throw new ArgumentNullException(nameof(parseResult));
5754
}
5855

5956
public class RequestLog

DevProxy.Abstractions/packages.lock.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,9 @@
9999
},
100100
"System.CommandLine": {
101101
"type": "Direct",
102-
"requested": "[2.0.0-beta4.22272.1, )",
103-
"resolved": "2.0.0-beta4.22272.1",
104-
"contentHash": "1uqED/q2H0kKoLJ4+hI2iPSBSEdTuhfCYADeJrAqERmiGQ2NNacYKRNEQ+gFbU4glgVyK8rxI+ZOe1onEtr/Pg=="
102+
"requested": "[2.0.0-beta5.25306.1, )",
103+
"resolved": "2.0.0-beta5.25306.1",
104+
"contentHash": "ce0wuowuh13Cd7GXqLCq77/YWlxQMxrVCMIO/2/QUP6CdP/JWnlYSN/N3/55wwGsUwa9CvPuT8ddjgyypUr5ag=="
105105
},
106106
"Unobtanium.Web.Proxy": {
107107
"type": "Direct",

DevProxy.Plugins/Behavior/GenericRandomErrorPlugin.cs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -69,22 +69,24 @@ public override async Task InitializeAsync(InitArgs e)
6969

7070
public override Option[] GetOptions()
7171
{
72-
var _rateOption = new Option<int?>(_rateOptionName, "The percentage of chance that a request will fail");
73-
_rateOption.AddAlias("-f");
74-
_rateOption.ArgumentHelpName = "failure rate";
75-
_rateOption.AddValidator((input) =>
72+
var _rateOption = new Option<int?>(_rateOptionName, "-f")
73+
{
74+
Description = "The percentage of chance that a request will fail",
75+
HelpName = "failure-rate"
76+
};
77+
_rateOption.Validators.Add((input) =>
7678
{
7779
try
7880
{
79-
var value = input.GetValueForOption(_rateOption);
81+
var value = input.GetValue(_rateOption);
8082
if (value.HasValue && (value < 0 || value > 100))
8183
{
82-
input.ErrorMessage = $"{value} is not a valid failure rate. Specify a number between 0 and 100";
84+
input.AddError($"{value} is not a valid failure rate. Specify a number between 0 and 100");
8385
}
8486
}
8587
catch (InvalidOperationException ex)
8688
{
87-
input.ErrorMessage = ex.Message;
89+
input.AddError(ex.Message);
8890
}
8991
});
9092

@@ -97,9 +99,7 @@ public override void OptionsLoaded(OptionsLoadedArgs e)
9799

98100
base.OptionsLoaded(e);
99101

100-
var context = e.Context;
101-
102-
var rate = context.ParseResult.GetValueForOption<int?>(_rateOptionName, e.Options);
102+
var rate = e.ParseResult.GetValueOrDefault<int?>(_rateOptionName);
103103
if (rate is not null)
104104
{
105105
Configuration.Rate = rate.Value;

DevProxy.Plugins/Behavior/GraphRandomErrorPlugin.cs

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -105,29 +105,31 @@ public sealed class GraphRandomErrorPlugin(
105105

106106
public override Option[] GetOptions()
107107
{
108-
var _allowedErrors = new Option<IEnumerable<int>>(_allowedErrorsOptionName, "List of errors that Dev Proxy may produce")
108+
var _allowedErrors = new Option<IEnumerable<int>>(_allowedErrorsOptionName, "-a")
109109
{
110-
ArgumentHelpName = "allowed errors",
111-
AllowMultipleArgumentsPerToken = true
110+
AllowMultipleArgumentsPerToken = true,
111+
Description = "List of errors that Dev Proxy may produce",
112+
HelpName = "allowed-errors"
112113
};
113-
_allowedErrors.AddAlias("-a");
114114

115-
var _rateOption = new Option<int?>(_rateOptionName, "The percentage of chance that a request will fail");
116-
_rateOption.AddAlias("-f");
117-
_rateOption.ArgumentHelpName = "failure rate";
118-
_rateOption.AddValidator((input) =>
115+
var _rateOption = new Option<int?>(_rateOptionName, "-f")
116+
{
117+
Description = "The percentage of chance that a request will fail",
118+
HelpName = "failure-rate"
119+
};
120+
_rateOption.Validators.Add((input) =>
119121
{
120122
try
121123
{
122-
var value = input.GetValueForOption(_rateOption);
124+
var value = input.GetValue(_rateOption);
123125
if (value.HasValue && (value < 0 || value > 100))
124126
{
125-
input.ErrorMessage = $"{value} is not a valid failure rate. Specify a number between 0 and 100";
127+
input.AddError($"{value} is not a valid failure rate. Specify a number between 0 and 100");
126128
}
127129
}
128130
catch (InvalidOperationException ex)
129131
{
130-
input.ErrorMessage = ex.Message;
132+
input.AddError(ex.Message);
131133
}
132134
});
133135

@@ -140,10 +142,10 @@ public override void OptionsLoaded(OptionsLoadedArgs e)
140142

141143
base.OptionsLoaded(e);
142144

143-
var context = e.Context;
145+
var parseResult = e.ParseResult;
144146

145147
// Configure the allowed errors
146-
var allowedErrors = context.ParseResult.GetValueForOption<IEnumerable<int>?>(_allowedErrorsOptionName, e.Options);
148+
var allowedErrors = parseResult.GetValueOrDefault<IEnumerable<int>?>(_allowedErrorsOptionName);
147149
if (allowedErrors?.Any() ?? false)
148150
{
149151
Configuration.AllowedErrors = [.. allowedErrors];
@@ -157,7 +159,7 @@ public override void OptionsLoaded(OptionsLoadedArgs e)
157159
}
158160
}
159161

160-
var rate = context.ParseResult.GetValueForOption<int?>(_rateOptionName, e.Options);
162+
var rate = parseResult.GetValueOrDefault<int?>(_rateOptionName);
161163
if (rate is not null)
162164
{
163165
Configuration.Rate = rate.Value;

DevProxy.Plugins/DevProxy.Plugins.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
<Private>false</Private>
4949
<ExcludeAssets>runtime</ExcludeAssets>
5050
</PackageReference>
51-
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1">
51+
<PackageReference Include="System.CommandLine" Version="2.0.0-beta5.25306.1">
5252
<Private>false</Private>
5353
<ExcludeAssets>runtime</ExcludeAssets>
5454
</PackageReference>

DevProxy.Plugins/Mocking/MockResponsePlugin.cs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -72,15 +72,16 @@ public override async Task InitializeAsync(InitArgs e)
7272

7373
public override Option[] GetOptions()
7474
{
75-
var _noMocks = new Option<bool?>(_noMocksOptionName, "Disable loading mock requests")
75+
var _noMocks = new Option<bool?>(_noMocksOptionName, "-n")
7676
{
77-
ArgumentHelpName = "no mocks"
77+
Description = "Disable loading mock requests",
78+
HelpName = "no-mocks"
7879
};
79-
_noMocks.AddAlias("-n");
8080

81-
var _mocksFile = new Option<string?>(_mocksFileOptionName, "Provide a file populated with mock responses")
81+
var _mocksFile = new Option<string?>(_mocksFileOptionName)
8282
{
83-
ArgumentHelpName = "mocks file"
83+
Description = "Provide a file populated with mock responses",
84+
HelpName = "mocks-file"
8485
};
8586

8687
return [_noMocks, _mocksFile];
@@ -92,10 +93,10 @@ public override void OptionsLoaded(OptionsLoadedArgs e)
9293

9394
base.OptionsLoaded(e);
9495

95-
var context = e.Context;
96+
var parseResult = e.ParseResult;
9697

9798
// allow disabling of mocks as a command line option
98-
var noMocks = context.ParseResult.GetValueForOption<bool?>(_noMocksOptionName, e.Options);
99+
var noMocks = parseResult.GetValueOrDefault<bool?>(_noMocksOptionName);
99100
if (noMocks.HasValue)
100101
{
101102
Configuration.NoMocks = noMocks.Value;
@@ -107,7 +108,7 @@ public override void OptionsLoaded(OptionsLoadedArgs e)
107108
}
108109

109110
// update the name of the mocks file to load from if supplied
110-
var mocksFile = context.ParseResult.GetValueForOption<string?>(_mocksFileOptionName, e.Options);
111+
var mocksFile = parseResult.GetValueOrDefault<string?>(_mocksFileOptionName);
111112
if (mocksFile is not null)
112113
{
113114
Configuration.MocksFile = mocksFile;

DevProxy.Plugins/Reporting/ExecutionSummaryPlugin.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -42,15 +42,16 @@ public sealed class ExecutionSummaryPlugin(
4242

4343
public override Option[] GetOptions()
4444
{
45-
var groupBy = new Option<SummaryGroupBy?>(_groupByOptionName, "Specifies how the information should be grouped in the summary. Available options: `url` (default), `messageType`.")
45+
var groupBy = new Option<SummaryGroupBy?>(_groupByOptionName)
4646
{
47-
ArgumentHelpName = "summary-group-by"
47+
Description = "Specifies how the information should be grouped in the summary. Available options: `url` (default), `messageType`.",
48+
HelpName = "summary-group-by"
4849
};
49-
groupBy.AddValidator(input =>
50+
groupBy.Validators.Add(input =>
5051
{
5152
if (!Enum.TryParse<SummaryGroupBy>(input.Tokens[0].Value, true, out var groupBy))
5253
{
53-
input.ErrorMessage = $"{input.Tokens[0].Value} is not a valid option to group by. Allowed values are: {string.Join(", ", Enum.GetNames<SummaryGroupBy>())}";
54+
input.AddError($"{input.Tokens[0].Value} is not a valid option to group by. Allowed values are: {string.Join(", ", Enum.GetNames<SummaryGroupBy>())}");
5455
}
5556
});
5657

@@ -63,9 +64,9 @@ public override void OptionsLoaded(OptionsLoadedArgs e)
6364

6465
base.OptionsLoaded(e);
6566

66-
var context = e.Context;
67+
var parseResult = e.ParseResult;
6768

68-
var groupBy = context.ParseResult.GetValueForOption<SummaryGroupBy?>(_groupByOptionName, e.Options);
69+
var groupBy = parseResult.GetValueOrDefault<SummaryGroupBy?>(_groupByOptionName);
6970
if (groupBy is not null)
7071
{
7172
Configuration.GroupBy = groupBy.Value;

DevProxy.Plugins/packages.lock.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -85,9 +85,9 @@
8585
},
8686
"System.CommandLine": {
8787
"type": "Direct",
88-
"requested": "[2.0.0-beta4.22272.1, )",
89-
"resolved": "2.0.0-beta4.22272.1",
90-
"contentHash": "1uqED/q2H0kKoLJ4+hI2iPSBSEdTuhfCYADeJrAqERmiGQ2NNacYKRNEQ+gFbU4glgVyK8rxI+ZOe1onEtr/Pg=="
88+
"requested": "[2.0.0-beta5.25306.1, )",
89+
"resolved": "2.0.0-beta5.25306.1",
90+
"contentHash": "ce0wuowuh13Cd7GXqLCq77/YWlxQMxrVCMIO/2/QUP6CdP/JWnlYSN/N3/55wwGsUwa9CvPuT8ddjgyypUr5ag=="
9191
},
9292
"System.IdentityModel.Tokens.Jwt": {
9393
"type": "Direct",
@@ -603,7 +603,7 @@
603603
"Microsoft.OpenApi.Readers": "[1.6.24, )",
604604
"Newtonsoft.Json.Schema": "[4.0.1, )",
605605
"Prompty.Core": "[0.2.2-beta, )",
606-
"System.CommandLine": "[2.0.0-beta4.22272.1, )",
606+
"System.CommandLine": "[2.0.0-beta5.25306.1, )",
607607
"Unobtanium.Web.Proxy": "[0.1.5, )"
608608
}
609609
}

0 commit comments

Comments
 (0)