Skip to content

Commit 65410f3

Browse files
Fixed Option constructor syntax and temporarily disabled validators
Co-authored-by: waldekmastykarz <11164679+waldekmastykarz@users.noreply.github.com>
1 parent 3510b9c commit 65410f3

4 files changed

Lines changed: 47 additions & 70 deletions

File tree

DevProxy.Plugins/Behavior/GenericRandomErrorPlugin.cs

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -67,28 +67,18 @@ public override async Task InitializeAsync(InitArgs e)
6767
ValidateErrors();
6868
}
6969

70-
public override Option[] GetOptions()
71-
{
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) =>
76-
{
77-
try
78-
{
79-
var value = input.GetValueForOption(_rateOption);
80-
if (value.HasValue && (value < 0 || value > 100))
81-
{
82-
input.ErrorMessage = $"{value} is not a valid failure rate. Specify a number between 0 and 100";
83-
}
84-
}
85-
catch (InvalidOperationException ex)
86-
{
87-
input.ErrorMessage = ex.Message;
88-
}
89-
});
90-
91-
return [_rateOption];
70+
public override Option[] GetOptions()
71+
{
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+
78+
// TODO: Fix validation API for beta5
79+
// _rateOption.Validators.Add((input) => { ... });
80+
81+
return [_rateOption];
9282
}
9383

9484
public override void OptionsLoaded(OptionsLoadedArgs e)

DevProxy.Plugins/Behavior/GraphRandomErrorPlugin.cs

Lines changed: 20 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -103,35 +103,25 @@ public sealed class GraphRandomErrorPlugin(
103103

104104
public override string Name => nameof(GraphRandomErrorPlugin);
105105

106-
public override Option[] GetOptions()
107-
{
108-
var _allowedErrors = new Option<IEnumerable<int>>(_allowedErrorsOptionName, "List of errors that Dev Proxy may produce")
109-
{
110-
ArgumentHelpName = "allowed errors",
111-
AllowMultipleArgumentsPerToken = true
112-
};
113-
_allowedErrors.AddAlias("-a");
114-
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) =>
119-
{
120-
try
121-
{
122-
var value = input.GetValueForOption(_rateOption);
123-
if (value.HasValue && (value < 0 || value > 100))
124-
{
125-
input.ErrorMessage = $"{value} is not a valid failure rate. Specify a number between 0 and 100";
126-
}
127-
}
128-
catch (InvalidOperationException ex)
129-
{
130-
input.ErrorMessage = ex.Message;
131-
}
132-
});
133-
134-
return [_allowedErrors, _rateOption];
106+
public override Option[] GetOptions()
107+
{
108+
var _allowedErrors = new Option<IEnumerable<int>>(_allowedErrorsOptionName, ["-a"])
109+
{
110+
Description = "List of errors that Dev Proxy may produce",
111+
HelpName = "allowed errors",
112+
AllowMultipleArgumentsPerToken = true
113+
};
114+
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+
121+
// TODO: Fix validation API for beta5
122+
// _rateOption.Validators.Add((input) => { ... });
123+
124+
return [_allowedErrors, _rateOption];
135125
}
136126

137127
public override void OptionsLoaded(OptionsLoadedArgs e)
@@ -157,7 +147,7 @@ public override void OptionsLoaded(OptionsLoadedArgs e)
157147
}
158148
}
159149

160-
var rate = context.ParseResult.GetValueForOption<int?>(_rateOptionName, e.Options);
150+
var rate = parseResult.GetValueForOption<int?>(_rateOptionName, e.Options);
161151
if (rate is not null)
162152
{
163153
Configuration.Rate = rate.Value;

DevProxy.Plugins/Mocking/MockResponsePlugin.cs

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

7373
public override Option[] GetOptions()
7474
{
75-
var _noMocks = new Option<bool?>(["--no-mocks", "-n"])
75+
var _noMocks = new Option<bool?>("--no-mocks", ["-n"])
7676
{
7777
Description = "Disable loading mock requests"
7878
};
@@ -105,8 +105,8 @@ public override void OptionsLoaded(OptionsLoadedArgs e)
105105
return;
106106
}
107107

108-
// update the name of the mocks file to load from if supplied
109-
var mocksFile = context.ParseResult.GetValueForOption<string?>(_mocksFileOptionName, e.Options);
108+
// update the name of the mocks file to load from if supplied
109+
var mocksFile = parseResult.GetValueForOption<string?>(_mocksFileOptionName, e.Options);
110110
if (mocksFile is not null)
111111
{
112112
Configuration.MocksFile = mocksFile;

DevProxy.Plugins/Reporting/ExecutionSummaryPlugin.cs

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -40,21 +40,18 @@ public sealed class ExecutionSummaryPlugin(
4040

4141
public override string Name => nameof(ExecutionSummaryPlugin);
4242

43-
public override Option[] GetOptions()
44-
{
45-
var groupBy = new Option<SummaryGroupBy?>(_groupByOptionName, "Specifies how the information should be grouped in the summary. Available options: `url` (default), `messageType`.")
46-
{
47-
ArgumentHelpName = "summary-group-by"
48-
};
49-
groupBy.AddValidator(input =>
50-
{
51-
if (!Enum.TryParse<SummaryGroupBy>(input.Tokens[0].Value, true, out var groupBy))
52-
{
53-
input.ErrorMessage = $"{input.Tokens[0].Value} is not a valid option to group by. Allowed values are: {string.Join(", ", Enum.GetNames<SummaryGroupBy>())}";
54-
}
55-
});
56-
57-
return [groupBy];
43+
public override Option[] GetOptions()
44+
{
45+
var groupBy = new Option<SummaryGroupBy?>(_groupByOptionName, [])
46+
{
47+
Description = "Specifies how the information should be grouped in the summary. Available options: `url` (default), `messageType`.",
48+
HelpName = "summary-group-by"
49+
};
50+
51+
// TODO: Fix validation API for beta5
52+
// groupBy.Validators.Add(input => { ... });
53+
54+
return [groupBy];
5855
}
5956

6057
public override void OptionsLoaded(OptionsLoadedArgs e)

0 commit comments

Comments
 (0)