Skip to content

Commit 9b6377e

Browse files
committed
Feat: Exclude invalid commands and simplify name parsing
Filtered out commands with "UNDEFINED" names or short names when generating the command dictionary. Simplified the auto-generated kebab-case names to use only the first letter of each segment. These changes improve code clarity and prevent invalid entries in the command dictionary.
1 parent 9997f94 commit 9b6377e

File tree

3 files changed

+10
-4
lines changed

3 files changed

+10
-4
lines changed

src/CodeOfChaos.CliArgsParser.Generators/CommandDto.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ private static string GetName(INamedTypeSymbol symbol) {
3636

3737
if (cliDataAttribute?.ConstructorArguments.ElementAtOrDefault(0).Value is string cliDataName) return cliDataName;
3838
return attributes.Any(attr => attr.IsDisplayName(TypeNames.AutoNameAttribute))
39-
? symbol.Name.ToKebabCase()
39+
? string.Join("", symbol.Name.ToKebabCase().Split('-').Select(s => s[0]))
4040
: "UNDEFINED";
4141
}
4242

src/CodeOfChaos.CliArgsParser.Generators/CommandGenerator.cs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,14 @@ private static void GenerateSources(SourceProductionContext context, (Compilatio
7272
builder.AppendLine();
7373
builder.AppendLine($"public static class __{containingAssemblyName.Replace(".", "")}__CliArgsParserDictionary {{");
7474
builder.AppendLineIndented("public static readonly Dictionary<string, Type> Commands = new Dictionary<string, Type>() {");
75-
builder.ForEachAppendLineIndented(data, dto => $" {{ {dto.Name}, typeof({dto.Symbol.ToDisplayString()}) }},");
76-
builder.ForEachAppendLineIndented(data, dto => $" {{ {dto.ShortName}, typeof({dto.Symbol.ToDisplayString()}) }},");
75+
builder.ForEachAppendLineIndented(
76+
data.Where(d => d.Name != "UNDEFINED".ToQuotedString()),
77+
dto => $" {{ {dto.Name}, typeof({dto.Symbol.ToDisplayString()}) }},"
78+
);
79+
builder.ForEachAppendLineIndented(
80+
data.Where(d => d.ShortName != "UNDEFINED".ToQuotedString()),
81+
dto => $" {{ {dto.ShortName}, typeof({dto.Symbol.ToDisplayString()}) }},"
82+
);
7783
builder.AppendLineIndented("};");
7884
builder.AppendLine("}");
7985
context.AddSource($"{containingAssemblyName}CliArgsParser.g.cs", builder.ToStringAndClear());

tests/Tests.CodeOfChaos.CliArgsParser/CliArgsParserTests.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,6 @@ public async Task ExecuteAsync_Test() {
2121
await parser.ExecuteAsync(input);
2222

2323
// Assert
24-
24+
2525
}
2626
}

0 commit comments

Comments
 (0)