Skip to content

Commit 3aff795

Browse files
committed
refactor(commands): adapt the commands to the new structure
1 parent bf14f7b commit 3aff795

File tree

4 files changed

+76
-56
lines changed

4 files changed

+76
-56
lines changed

src/Commands/ComparerCommand.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ namespace PowerUtils.BenchmarkDotnet.Reporter.Commands;
1111

1212
public interface IComparerCommand
1313
{
14-
void Execute(string? baseline, string? target, string? meanThreshold, string? allocationThreshold, string[] formats, string output);
14+
int Execute(string? baseline, string? target, string? meanThreshold, string? allocationThreshold, string[] formats, string output);
1515
}
1616

1717
public sealed class ComparerCommand(
@@ -24,7 +24,7 @@ public sealed class ComparerCommand(
2424
private readonly IServiceProvider _provider = provider;
2525

2626

27-
public void Execute(
27+
public int Execute(
2828
string? baseline,
2929
string? target,
3030
string? meanThreshold,
@@ -166,5 +166,8 @@ public void Execute(
166166
.GetRequiredKeyedService<IExporter>(format.ToLower())
167167
.Generate(comparerReport, output);
168168
}
169+
170+
171+
return 0; // Success exit code
169172
}
170173
}

src/Program.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System;
2-
using System.CommandLine;
32
using System.Globalization;
43
using System.Threading;
54
using Microsoft.Extensions.DependencyInjection;
@@ -29,4 +28,4 @@
2928
.AddTransient<IComparerCommand, ComparerCommand>();
3029

3130
var tool = new ToolCommands(serviceCollection.BuildServiceProvider());
32-
tool.Invoke(args);
31+
return tool.Parse(args).Invoke();

src/ToolCommands.cs

Lines changed: 56 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
using System;
22
using System.CommandLine;
3+
using System.Linq;
34
using Microsoft.Extensions.DependencyInjection;
45
using PowerUtils.BenchmarkDotnet.Reporter.Commands;
56

@@ -30,60 +31,77 @@ public ToolCommands(IServiceProvider provider)
3031
output
3132
};
3233

33-
compareCommand
34-
.SetHandler(
35-
provider.GetRequiredService<IComparerCommand>().Execute,
36-
baseline,
37-
target,
38-
meanThreshold,
39-
allocationThreshold,
40-
formats,
41-
output);
42-
43-
Add(compareCommand);
34+
compareCommand.SetAction(parser => provider
35+
.GetRequiredService<IComparerCommand>()
36+
.Execute(
37+
parser.GetValue(baseline)!,
38+
parser.GetValue(target)!,
39+
parser.GetValue(meanThreshold),
40+
parser.GetValue(allocationThreshold),
41+
parser.GetValue(formats)!,
42+
parser.GetValue(output)!));
43+
44+
Subcommands.Add(compareCommand);
4445
}
4546

4647

4748
private static Option<string> _createBaselineOption()
48-
=> new(
49-
["-b", "--baseline"],
50-
"Path to the folder or file with Baseline report.")
49+
=> new("--baseline", "-b")
5150
{
52-
IsRequired = true
51+
Description = "Path to the folder or file with Baseline report.",
52+
Required = true
5353
};
5454

5555
private static Option<string> _createTargetOption()
56-
=> new(
57-
["-t", "--target"],
58-
"Path to the folder or file with target reports.")
56+
=> new("--target", "-t")
5957
{
60-
IsRequired = true
58+
Description = "Path to the folder or file with target reports.",
59+
Required = true
6160
};
6261

6362
private static Option<string> _createMeanThresholdOption()
64-
=> new(
65-
["-tm", "--threshold-mean"],
66-
"Throw an error when the mean threshold is met. Examples: 5%, 10ms, 10μs, 100ns, 1s.");
63+
=> new("--threshold-mean", "-tm")
64+
{
65+
Description = "Throw an error when the mean threshold is met. Examples: 5%, 10ms, 10μs, 100ns, 1s."
66+
};
6767

6868
private static Option<string> _createAllocationThresholdOption()
69-
=> new(
70-
["-ta", "--threshold-allocation"],
71-
"Throw an error when the allocation threshold is met. Examples: 5%, 10b, 10kb, 100mb, 1gb.");
69+
=> new("--threshold-allocation", "-ta")
70+
{
71+
Description = "Throw an error when the allocation threshold is met. Examples: 5%, 10b, 10kb, 100mb, 1gb."
72+
};
7273

7374
private static Option<string[]> _createFormatsOption()
74-
=> new Option<string[]>(
75-
["-f", "--format"],
76-
() => ["console"],
77-
"Output format for the report.")
78-
.FromAmong(
79-
"console",
80-
"markdown",
81-
"json",
82-
"hit-txt");
75+
{
76+
var option = new Option<string[]>("--format", "-f")
77+
{
78+
Description = "Output format for the report.",
79+
DefaultValueFactory = _ => ["console"]
80+
};
81+
82+
option.Validators.Add(result =>
83+
{
84+
var allowedValues = new[] { "console", "markdown", "json", "hit-txt" };
85+
var values = result.GetValue(option);
86+
if(values != null)
87+
{
88+
foreach(var value in values)
89+
{
90+
if(!allowedValues.Contains(value))
91+
{
92+
result.AddError($"Invalid format '{value}'. Allowed values: {string.Join(", ", allowedValues)}");
93+
}
94+
}
95+
}
96+
});
97+
98+
return option;
99+
}
83100

84101
private static Option<string> _createOutputOption()
85-
=> new(
86-
["-o", "--output"],
87-
() => "./BenchmarkReporter",
88-
"Output directory to export the diff report. Default is current directory.");
102+
=> new("--output", "-o")
103+
{
104+
Description = "Output directory to export the diff report. Default is current directory.",
105+
DefaultValueFactory = _ => "./BenchmarkReporter"
106+
};
89107
}

tests/PowerUtils.BenchmarkDotnet.Reporter.Tests/ToolCommandsTest.cs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -66,12 +66,12 @@ public void CompareCommand_ShouldHave_BaselineOption()
6666

6767
// Assert
6868
var compareCommand = toolCommands.Subcommands.Single(c => c.Name == "compare");
69-
var baselineOption = compareCommand.Options.Single(o => o.Name == "baseline");
69+
var baselineOption = compareCommand.Options.Single(o => o.Name == "--baseline");
7070

7171
baselineOption.ValueType.ShouldBe(typeof(string));
72+
baselineOption.Aliases.Count.ShouldBe(1);
7273
baselineOption.Aliases.ShouldContain("-b");
73-
baselineOption.Aliases.ShouldContain("--baseline");
74-
baselineOption.IsRequired.ShouldBeTrue();
74+
baselineOption.Required.ShouldBeTrue();
7575
baselineOption.Description.ShouldBe("Path to the folder or file with Baseline report.");
7676
}
7777

@@ -84,12 +84,12 @@ public void CompareCommand_ShouldHave_TargetOption()
8484

8585
// Assert
8686
var compareCommand = toolCommands.Subcommands.Single(c => c.Name == "compare");
87-
var targetOption = compareCommand.Options.Single(o => o.Name == "target");
87+
var targetOption = compareCommand.Options.Single(o => o.Name == "--target");
8888

8989
targetOption.ValueType.ShouldBe(typeof(string));
90+
targetOption.Aliases.Count.ShouldBe(1);
9091
targetOption.Aliases.ShouldContain("-t");
91-
targetOption.Aliases.ShouldContain("--target");
92-
targetOption.IsRequired.ShouldBeTrue();
92+
targetOption.Required.ShouldBeTrue();
9393
targetOption.Description.ShouldBe("Path to the folder or file with target reports.");
9494
}
9595

@@ -102,11 +102,11 @@ public void CompareCommand_ShouldHave_ThresholdMeanOption()
102102

103103
// Assert
104104
var compareCommand = toolCommands.Subcommands.Single(c => c.Name == "compare");
105-
var meanThresholdOption = compareCommand.Options.Single(o => o.Name == "threshold-mean");
105+
var meanThresholdOption = compareCommand.Options.Single(o => o.Name == "--threshold-mean");
106106

107107
meanThresholdOption.ValueType.ShouldBe(typeof(string));
108+
meanThresholdOption.Aliases.Count.ShouldBe(1);
108109
meanThresholdOption.Aliases.ShouldContain("-tm");
109-
meanThresholdOption.Aliases.ShouldContain("--threshold-mean");
110110
meanThresholdOption.Description.ShouldBe("Throw an error when the mean threshold is met. Examples: 5%, 10ms, 10μs, 100ns, 1s.");
111111
}
112112

@@ -119,11 +119,11 @@ public void CompareCommand_ShouldHave_ThresholdAllocationOption()
119119

120120
// Assert
121121
var compareCommand = toolCommands.Subcommands.Single(c => c.Name == "compare");
122-
var allocationThresholdOption = compareCommand.Options.Single(o => o.Name == "threshold-allocation");
122+
var allocationThresholdOption = compareCommand.Options.Single(o => o.Name == "--threshold-allocation");
123123

124124
allocationThresholdOption.ValueType.ShouldBe(typeof(string));
125+
allocationThresholdOption.Aliases.Count.ShouldBe(1);
125126
allocationThresholdOption.Aliases.ShouldContain("-ta");
126-
allocationThresholdOption.Aliases.ShouldContain("--threshold-allocation");
127127
allocationThresholdOption.Description.ShouldBe("Throw an error when the allocation threshold is met. Examples: 5%, 10b, 10kb, 100mb, 1gb.");
128128
}
129129

@@ -136,11 +136,11 @@ public void CompareCommand_ShouldHave_FormatsOption()
136136

137137
// Assert
138138
var compareCommand = toolCommands.Subcommands.Single(c => c.Name == "compare");
139-
var formatsOption = compareCommand.Options.Single(o => o.Name == "format");
139+
var formatsOption = compareCommand.Options.Single(o => o.Name == "--format");
140140

141141
formatsOption.ValueType.ShouldBe(typeof(string[]));
142+
formatsOption.Aliases.Count.ShouldBe(1);
142143
formatsOption.Aliases.ShouldContain("-f");
143-
formatsOption.Aliases.ShouldContain("--format");
144144
formatsOption.Description.ShouldBe("Output format for the report.");
145145
}
146146

@@ -153,11 +153,11 @@ public void CompareCommand_ShouldHave_OutputOption()
153153

154154
// Assert
155155
var compareCommand = toolCommands.Subcommands.Single(c => c.Name == "compare");
156-
var outputOption = compareCommand.Options.Single(o => o.Name == "output");
156+
var outputOption = compareCommand.Options.Single(o => o.Name == "--output");
157157

158158
outputOption.ValueType.ShouldBe(typeof(string));
159+
outputOption.Aliases.Count.ShouldBe(1);
159160
outputOption.Aliases.ShouldContain("-o");
160-
outputOption.Aliases.ShouldContain("--output");
161161
outputOption.Description.ShouldBe("Output directory to export the diff report. Default is current directory.");
162162
}
163163
}

0 commit comments

Comments
 (0)