Skip to content

Commit 82f34fe

Browse files
lduchosalclaude
andcommitted
fix: resolve SonarCloud issues (S927, S1192, S3776, CA1859, S2701, IDE0034, IDE0060, IDE0028)
- S927: rename formatter Write parameters to match interface ("output") - S1192: extract repeated string literals into constants in Program.cs - S3776: extract WriteUsageHelp/ComputeMaxFlagWidth to reduce cognitive complexity - CA1859: use specific return types for private methods in ActionComputer - S2701/MSTEST0037: use Assert.IsTrue/IsFalse instead of Assert.AreEqual(bool) - IDE0034: simplify default(BigInteger) to default - IDE0060: discard unused parameter in TestOperatorAddOverflow - IDE0028: use collection initializer in GetHashCodeUnitTest Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 4429c70 commit 82f34fe

File tree

8 files changed

+123
-104
lines changed

8 files changed

+123
-104
lines changed

src/ConsoleApplication/ActionComputer.cs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static ActionOutput Compute(ProgramContext ac, ArgParsed[] argsList)
4242
};
4343
}
4444

45-
private static ActionOutput ComputeSubnet(ProgramContext ac)
45+
private static ActionOutput.NetworkGroups ComputeSubnet(ProgramContext ac)
4646
{
4747
var groups = new List<List<IPNetwork2>>();
4848
foreach (var ipnetwork in ac.Networks)
@@ -87,7 +87,7 @@ private static ActionOutput ComputeWideSupernet(ProgramContext ac)
8787
return new ActionOutput.Networks { Items = [result] };
8888
}
8989

90-
private static ActionOutput ComputeContain(ProgramContext ac)
90+
private static ActionOutput.ContainResults ComputeContain(ProgramContext ac)
9191
{
9292
if (ac.ContainNetwork is null)
9393
{
@@ -108,7 +108,7 @@ private static ActionOutput ComputeContain(ProgramContext ac)
108108
return new ActionOutput.ContainResults { Items = results };
109109
}
110110

111-
private static ActionOutput ComputeOverlap(ProgramContext ac)
111+
private static ActionOutput.OverlapResults ComputeOverlap(ProgramContext ac)
112112
{
113113
if (ac.OverlapNetwork is null)
114114
{
@@ -129,7 +129,7 @@ private static ActionOutput ComputeOverlap(ProgramContext ac)
129129
return new ActionOutput.OverlapResults { Items = results };
130130
}
131131

132-
private static ActionOutput ComputeSubtract(ProgramContext ac)
132+
private static ActionOutput.SubtractResults ComputeSubtract(ProgramContext ac)
133133
{
134134
if (ac.SubtractNetwork is null)
135135
{
@@ -159,7 +159,7 @@ private static IEnumerable<string> EnumerateIPs(ProgramContext ac)
159159
}
160160
}
161161

162-
private static ActionOutput BuildUsageInfo(ProgramContext ac, ArgParsed[] argsList)
162+
private static ActionOutput.UsageInfo BuildUsageInfo(ProgramContext ac, ArgParsed[] argsList)
163163
{
164164
var groupOrder = new List<string>();
165165
var groups = new Dictionary<string, List<UsageOption>>();

src/ConsoleApplication/JsonFormatter.cs

Lines changed: 24 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,22 @@ public void Write(ActionOutput output, ProgramContext ac)
4141
}
4242

4343
/// <inheritdoc/>
44-
public void Write(ActionOutput.Networks n, ProgramContext ac)
44+
public void Write(ActionOutput.Networks output, ProgramContext ac)
4545
{
46-
WriteNetworks(n.Items, ac);
46+
WriteNetworks(output.Items, ac);
4747
}
4848

4949
/// <inheritdoc/>
50-
public void Write(ActionOutput.NetworkGroups g, ProgramContext ac)
50+
public void Write(ActionOutput.NetworkGroups output, ProgramContext ac)
5151
{
52-
for (int i = 0; i < g.Groups.Count; i++)
52+
for (int i = 0; i < output.Groups.Count; i++)
5353
{
54-
var group = g.Groups[i];
54+
var group = output.Groups[i];
5555
if (group.Count == 0)
5656
{
5757
_json.WriteStartObject();
5858
_json.WriteString("error",
59-
$"Unable to subnet ipnetwork {g.InputNetworks[i]} into cidr {g.SubnetCidr}");
59+
$"Unable to subnet ipnetwork {output.InputNetworks[i]} into cidr {output.SubnetCidr}");
6060
_json.WriteEndObject();
6161
}
6262
else
@@ -69,15 +69,15 @@ public void Write(ActionOutput.NetworkGroups g, ProgramContext ac)
6969
}
7070

7171
/// <inheritdoc/>
72-
public void Write(ActionOutput.SubtractResults sub, ProgramContext ac)
72+
public void Write(ActionOutput.SubtractResults output, ProgramContext ac)
7373
{
74-
WriteNetworks(sub.Items, ac);
74+
WriteNetworks(output.Items, ac);
7575
}
7676

7777
/// <inheritdoc/>
78-
public void Write(ActionOutput.ContainResults c, ProgramContext ac)
78+
public void Write(ActionOutput.ContainResults output, ProgramContext ac)
7979
{
80-
foreach (var r in c.Items)
80+
foreach (var r in output.Items)
8181
{
8282
_json.WriteStartObject();
8383
_json.WriteString("network", r.Network);
@@ -88,9 +88,9 @@ public void Write(ActionOutput.ContainResults c, ProgramContext ac)
8888
}
8989

9090
/// <inheritdoc/>
91-
public void Write(ActionOutput.OverlapResults o, ProgramContext ac)
91+
public void Write(ActionOutput.OverlapResults output, ProgramContext ac)
9292
{
93-
foreach (var r in o.Items)
93+
foreach (var r in output.Items)
9494
{
9595
_json.WriteStartObject();
9696
_json.WriteString("network", r.Network);
@@ -101,28 +101,28 @@ public void Write(ActionOutput.OverlapResults o, ProgramContext ac)
101101
}
102102

103103
/// <inheritdoc/>
104-
public void Write(ActionOutput.IpAddresses ip, ProgramContext ac)
104+
public void Write(ActionOutput.IpAddresses output, ProgramContext ac)
105105
{
106-
foreach (string addr in ip.Items)
106+
foreach (string addr in output.Items)
107107
{
108108
_json.WriteStringValue(addr);
109109
}
110110
}
111111

112112
/// <inheritdoc/>
113-
public void Write(ActionOutput.Error e, ProgramContext ac)
113+
public void Write(ActionOutput.Error output, ProgramContext ac)
114114
{
115115
_json.WriteStartObject();
116-
_json.WriteString("error", e.Message);
116+
_json.WriteString("error", output.Message);
117117
_json.WriteEndObject();
118118
}
119119

120120
/// <inheritdoc/>
121-
public void Write(ActionOutput.UsageInfo usage, ProgramContext ac)
121+
public void Write(ActionOutput.UsageInfo output, ProgramContext ac)
122122
{
123-
if (usage.Errors.Count > 0)
123+
if (output.Errors.Count > 0)
124124
{
125-
foreach (string error in usage.Errors)
125+
foreach (string error in output.Errors)
126126
{
127127
_json.WriteStartObject();
128128
_json.WriteString("error", error);
@@ -133,12 +133,12 @@ public void Write(ActionOutput.UsageInfo usage, ProgramContext ac)
133133
}
134134

135135
_json.WriteStartObject();
136-
_json.WriteString("version", usage.Version);
137-
_json.WriteString("synopsis", usage.Synopsis);
136+
_json.WriteString("version", output.Version);
137+
_json.WriteString("synopsis", output.Synopsis);
138138

139139
_json.WritePropertyName("optionGroups");
140140
_json.WriteStartArray();
141-
foreach (var group in usage.OptionGroups)
141+
foreach (var group in output.OptionGroups)
142142
{
143143
_json.WriteStartObject();
144144
_json.WriteString("name", group.Name);
@@ -171,10 +171,10 @@ public void Write(ActionOutput.UsageInfo usage, ProgramContext ac)
171171
_json.WritePropertyName("positionalArgs");
172172
_json.WriteStartObject();
173173
_json.WriteString("name", "networks");
174-
_json.WriteString("description", usage.NetworksDescription);
174+
_json.WriteString("description", output.NetworksDescription);
175175
_json.WritePropertyName("examples");
176176
_json.WriteStartArray();
177-
foreach (string example in usage.NetworksExamples)
177+
foreach (string example in output.NetworksExamples)
178178
{
179179
_json.WriteStringValue(example);
180180
}

src/ConsoleApplication/Program.cs

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,28 +13,34 @@ namespace System.Net;
1313
/// </summary>
1414
public static class Program
1515
{
16-
private static readonly Dictionary<int, ArgParsed> Args = new ();
16+
private const string GroupPrint = "Print options";
17+
private const string GroupOutput = "Output options";
18+
private const string GroupParse = "Parse options";
19+
private const string GroupActions = "Actions";
20+
private const string ArgNameNetwork = "network";
21+
22+
private static readonly Dictionary<int, ArgParsed> Args = [];
1723

1824
private static readonly ArgParsed[] ArgsList =
1925
[
2026
// Print options
21-
new ArgParsed('i', "Print options", "network", (ac, _) => { ac.IPNetwork = true; }),
22-
new ArgParsed('n', "Print options", "network address", (ac, _) => { ac.Network = true; }),
23-
new ArgParsed('m', "Print options", "netmask", (ac, _) => { ac.Netmask = true; }),
24-
new ArgParsed('c', "Print options", "cidr", (ac, _) => { ac.Cidr = true; }),
25-
new ArgParsed('b', "Print options", "broadcast", (ac, _) => { ac.Broadcast = true; }),
26-
new ArgParsed('f', "Print options", "first usable ip address", (ac, _) => { ac.FirstUsable = true; }),
27-
new ArgParsed('l', "Print options", "last usable ip address", (ac, _) => { ac.LastUsable = true; }),
28-
new ArgParsed('u', "Print options", "number of usable ip addresses", (ac, _) => { ac.Usable = true; }),
29-
new ArgParsed('t', "Print options", "total number of ip addresses", (ac, _) => { ac.Total = true; }),
27+
new ArgParsed('i', GroupPrint, ArgNameNetwork, (ac, _) => { ac.IPNetwork = true; }),
28+
new ArgParsed('n', GroupPrint, "network address", (ac, _) => { ac.Network = true; }),
29+
new ArgParsed('m', GroupPrint, "netmask", (ac, _) => { ac.Netmask = true; }),
30+
new ArgParsed('c', GroupPrint, "cidr", (ac, _) => { ac.Cidr = true; }),
31+
new ArgParsed('b', GroupPrint, "broadcast", (ac, _) => { ac.Broadcast = true; }),
32+
new ArgParsed('f', GroupPrint, "first usable ip address", (ac, _) => { ac.FirstUsable = true; }),
33+
new ArgParsed('l', GroupPrint, "last usable ip address", (ac, _) => { ac.LastUsable = true; }),
34+
new ArgParsed('u', GroupPrint, "number of usable ip addresses", (ac, _) => { ac.Usable = true; }),
35+
new ArgParsed('t', GroupPrint, "total number of ip addresses", (ac, _) => { ac.Total = true; }),
3036

3137
// Output options
32-
new ArgParsed('j', "Output options", "JSON output", (ac, _) => { ac.Json = true; }),
38+
new ArgParsed('j', GroupOutput, "JSON output", (ac, _) => { ac.Json = true; }),
3339

3440
// Parse options
35-
new ArgParsed('D', "Parse options", "IPv4 only - use default cidr (ClassA/8, ClassB/16, ClassC/24)",
41+
new ArgParsed('D', GroupParse, "IPv4 only - use default cidr (ClassA/8, ClassB/16, ClassC/24)",
3642
(ac, _) => { ac.CidrParse = CidrParse.Default; }),
37-
new ArgParsed('d', "Parse options", "use cidr if not provided (default /32)", (ac, arg) =>
43+
new ArgParsed('d', GroupParse, "use cidr if not provided (default /32)", (ac, arg) =>
3844
{
3945
if (!IPNetwork2.TryParseCidr(arg, Sockets.AddressFamily.InterNetwork, out byte? cidr))
4046
{
@@ -48,10 +54,10 @@ public static class Program
4854
}, argName: "cidr"),
4955

5056
// Actions
51-
new ArgParsed('h', "Actions", "help message",
57+
new ArgParsed('h', GroupActions, "help message",
5258
(ac, _) => { ac.Action = Action.Usage; },
5359
example: "ipnetwork -h"),
54-
new ArgParsed('s', "Actions", "split network into cidr subnets", (ac, arg) =>
60+
new ArgParsed('s', GroupActions, "split network into cidr subnets", (ac, arg) =>
5561
{
5662
if (!IPNetwork2.TryParseCidr(arg, Sockets.AddressFamily.InterNetwork, out byte? cidr))
5763
{
@@ -63,16 +69,16 @@ public static class Program
6369
ac.Action = Action.Subnet;
6470
ac.SubnetCidr = (byte)cidr;
6571
}, argName: "cidr", example: "ipnetwork -s 24 10.0.0.0/8"),
66-
new ArgParsed('w', "Actions", "supernet networks into smallest possible subnets",
72+
new ArgParsed('w', GroupActions, "supernet networks into smallest possible subnets",
6773
(ac, _) => { ac.Action = Action.Supernet; },
6874
example: "ipnetwork -w 10.0.0.0/24 10.0.1.0/24"),
69-
new ArgParsed('W', "Actions", "supernet networks into one single subnet",
75+
new ArgParsed('W', GroupActions, "supernet networks into one single subnet",
7076
(ac, _) => { ac.Action = Action.WideSupernet; },
7177
example: "ipnetwork -W 10.0.0.0/24 10.0.10.0/24"),
72-
new ArgParsed('x', "Actions", "list all ip addresses in networks",
78+
new ArgParsed('x', GroupActions, "list all ip addresses in networks",
7379
(ac, _) => { ac.Action = Action.ListIPAddress; },
7480
example: "ipnetwork -x 10.0.0.0/30"),
75-
new ArgParsed('C', "Actions", "network contain networks", (ac, arg) =>
81+
new ArgParsed('C', GroupActions, "network contain networks", (ac, arg) =>
7682
{
7783
if (!TryParseIPNetwork(arg, ac.CidrParse, ac.CidrParsed, out IPNetwork2? ipnetwork))
7884
{
@@ -83,8 +89,8 @@ public static class Program
8389

8490
ac.Action = Action.ContainNetwork;
8591
ac.ContainNetwork = ipnetwork;
86-
}, argName: "network", example: "ipnetwork -C 10.0.0.0/8 10.0.1.0/24"),
87-
new ArgParsed('o', "Actions", "network overlap networks", (ac, arg) =>
92+
}, argName: ArgNameNetwork, example: "ipnetwork -C 10.0.0.0/8 10.0.1.0/24"),
93+
new ArgParsed('o', GroupActions, "network overlap networks", (ac, arg) =>
8894
{
8995
if (!TryParseIPNetwork(arg, ac.CidrParse, ac.CidrParsed, out IPNetwork2? ipnetwork))
9096
{
@@ -95,8 +101,8 @@ public static class Program
95101

96102
ac.Action = Action.OverlapNetwork;
97103
ac.OverlapNetwork = ipnetwork;
98-
}, argName: "network", example: "ipnetwork -o 10.0.0.0/8 192.168.0.0/16"),
99-
new ArgParsed('S', "Actions", "subtract network from networks", (ac, arg) =>
104+
}, argName: ArgNameNetwork, example: "ipnetwork -o 10.0.0.0/8 192.168.0.0/16"),
105+
new ArgParsed('S', GroupActions, "subtract network from networks", (ac, arg) =>
100106
{
101107
if (!TryParseIPNetwork(arg, ac.CidrParse, ac.CidrParsed, out IPNetwork2? ipnetwork))
102108
{
@@ -107,7 +113,7 @@ public static class Program
107113

108114
ac.Action = Action.SubtractNetwork;
109115
ac.SubtractNetwork = ipnetwork;
110-
}, argName: "network", example: "ipnetwork -S 10.0.1.0/24 10.0.0.0/23"),
116+
}, argName: ArgNameNetwork, example: "ipnetwork -S 10.0.1.0/24 10.0.0.0/23"),
111117

112118
// Hidden
113119
new ArgParsed('?', (_, _) => { }),

0 commit comments

Comments
 (0)