Skip to content

Commit 5392572

Browse files
Merge branch 'main' into cancellation-tokens
2 parents ef9c335 + f18e789 commit 5392572

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
@@ -71,22 +71,24 @@ public override async Task InitializeAsync(InitArgs e, CancellationToken cancell
7171

7272
public override Option[] GetOptions()
7373
{
74-
var _rateOption = new Option<int?>(_rateOptionName, "The percentage of chance that a request will fail");
75-
_rateOption.AddAlias("-f");
76-
_rateOption.ArgumentHelpName = "failure rate";
77-
_rateOption.AddValidator((input) =>
74+
var _rateOption = new Option<int?>(_rateOptionName, "-f")
75+
{
76+
Description = "The percentage of chance that a request will fail",
77+
HelpName = "failure-rate"
78+
};
79+
_rateOption.Validators.Add((input) =>
7880
{
7981
try
8082
{
81-
var value = input.GetValueForOption(_rateOption);
83+
var value = input.GetValue(_rateOption);
8284
if (value.HasValue && (value < 0 || value > 100))
8385
{
84-
input.ErrorMessage = $"{value} is not a valid failure rate. Specify a number between 0 and 100";
86+
input.AddError($"{value} is not a valid failure rate. Specify a number between 0 and 100");
8587
}
8688
}
8789
catch (InvalidOperationException ex)
8890
{
89-
input.ErrorMessage = ex.Message;
91+
input.AddError(ex.Message);
9092
}
9193
});
9294

@@ -99,9 +101,7 @@ public override void OptionsLoaded(OptionsLoadedArgs e)
99101

100102
base.OptionsLoaded(e);
101103

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

DevProxy.Plugins/Behavior/GraphRandomErrorPlugin.cs

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

108108
public override Option[] GetOptions()
109109
{
110-
var _allowedErrors = new Option<IEnumerable<int>>(_allowedErrorsOptionName, "List of errors that Dev Proxy may produce")
110+
var _allowedErrors = new Option<IEnumerable<int>>(_allowedErrorsOptionName, "-a")
111111
{
112-
ArgumentHelpName = "allowed errors",
113-
AllowMultipleArgumentsPerToken = true
112+
AllowMultipleArgumentsPerToken = true,
113+
Description = "List of errors that Dev Proxy may produce",
114+
HelpName = "allowed-errors"
114115
};
115-
_allowedErrors.AddAlias("-a");
116116

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

@@ -142,10 +144,10 @@ public override void OptionsLoaded(OptionsLoadedArgs e)
142144

143145
base.OptionsLoaded(e);
144146

145-
var context = e.Context;
147+
var parseResult = e.ParseResult;
146148

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

162-
var rate = context.ParseResult.GetValueForOption<int?>(_rateOptionName, e.Options);
164+
var rate = parseResult.GetValueOrDefault<int?>(_rateOptionName);
163165
if (rate is not null)
164166
{
165167
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
@@ -74,15 +74,16 @@ public override async Task InitializeAsync(InitArgs e, CancellationToken cancell
7474

7575
public override Option[] GetOptions()
7676
{
77-
var _noMocks = new Option<bool?>(_noMocksOptionName, "Disable loading mock requests")
77+
var _noMocks = new Option<bool?>(_noMocksOptionName, "-n")
7878
{
79-
ArgumentHelpName = "no mocks"
79+
Description = "Disable loading mock requests",
80+
HelpName = "no-mocks"
8081
};
81-
_noMocks.AddAlias("-n");
8282

83-
var _mocksFile = new Option<string?>(_mocksFileOptionName, "Provide a file populated with mock responses")
83+
var _mocksFile = new Option<string?>(_mocksFileOptionName)
8484
{
85-
ArgumentHelpName = "mocks file"
85+
Description = "Provide a file populated with mock responses",
86+
HelpName = "mocks-file"
8687
};
8788

8889
return [_noMocks, _mocksFile];
@@ -94,10 +95,10 @@ public override void OptionsLoaded(OptionsLoadedArgs e)
9495

9596
base.OptionsLoaded(e);
9697

97-
var context = e.Context;
98+
var parseResult = e.ParseResult;
9899

99100
// allow disabling of mocks as a command line option
100-
var noMocks = context.ParseResult.GetValueForOption<bool?>(_noMocksOptionName, e.Options);
101+
var noMocks = parseResult.GetValueOrDefault<bool?>(_noMocksOptionName);
101102
if (noMocks.HasValue)
102103
{
103104
Configuration.NoMocks = noMocks.Value;
@@ -109,7 +110,7 @@ public override void OptionsLoaded(OptionsLoadedArgs e)
109110
}
110111

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

DevProxy.Plugins/Reporting/ExecutionSummaryPlugin.cs

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

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

@@ -65,9 +66,9 @@ public override void OptionsLoaded(OptionsLoadedArgs e)
6566

6667
base.OptionsLoaded(e);
6768

68-
var context = e.Context;
69+
var parseResult = e.ParseResult;
6970

70-
var groupBy = context.ParseResult.GetValueForOption<SummaryGroupBy?>(_groupByOptionName, e.Options);
71+
var groupBy = parseResult.GetValueOrDefault<SummaryGroupBy?>(_groupByOptionName);
7172
if (groupBy is not null)
7273
{
7374
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)