Skip to content

Commit f8585da

Browse files
authored
fix bug with dash inside ToSymbolName() function (#53)
1 parent 078f049 commit f8585da

File tree

3 files changed

+36
-4
lines changed

3 files changed

+36
-4
lines changed

src/HelloWorld/HelloWorldClass.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ namespace Hello;
55

66
public class HelloWorldClass
77
{
8-
[Command("hi")]
8+
[Command("hello-world")]
99
public void Execute(
1010
string message,
1111
string option1 = "opt 1 default value",

src/System.CommandLine.Minimal.SourceGenerator/CommandOptionsWriter.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ public static class Configure{{binder.CommandNameTitleCase}}BuilderExtensions
164164
// if instance method, qualify with 'commandService'
165165
methodQualifier = "commandService";
166166
// get command service from dependency injections
167-
sb.AppendLine($" var commandService = services.GetRequiredService<{binder.ClassNamespace}.{binder.ClassName}>();");
167+
sb.AppendLine($" var commandService = services.GetRequiredService<{binder.FullClassName}>();");
168168
}
169169

170170
// *** The parameter creation fro command (see above additions to createParametersSb)

src/System.CommandLine.Minimal.SourceGenerator/Conventions/SymbolFormattingExtensions.cs

Lines changed: 34 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
using System.Globalization;
2+
using System.Linq;
23
using System.Text.RegularExpressions;
34

45
namespace System.CommandLine.Minimal.SourceGeneration.Conventions;
56

67
internal static class SymbolFormattingExtensions
78
{
9+
static readonly char[] unallowedChars = [
10+
'!', '@', '#', '$', '%', '^', '&', '*', '(', ')',
11+
'-', '+', '=', '{', '}', '[', ']', '|', '\\', ':', ';',
12+
'\'', '"', '<', '>', ',', '.', '?', '/', '~', '`',
13+
' ', '\t', '\n', '\r' ];
14+
815
/// <summary>
916
/// Conventionally convert a parameter name from a function into an help name using title case with spaces.
1017
/// </summary>
@@ -25,7 +32,32 @@ public static string ToHelpName(this string symbolString)
2532
/// <returns></returns>
2633
public static string ToSymbolName(this string symbolString)
2734
{
28-
char c = char.ToUpper(symbolString[0]);
29-
return c + symbolString.Substring(1);
35+
if (string.IsNullOrWhiteSpace(symbolString))
36+
return string.Empty;
37+
38+
bool capitalizeNext = true;
39+
int j = 0;
40+
char[] characters = new char[symbolString.Length];
41+
42+
for (int i = 0; i < symbolString.Length; i++)
43+
{
44+
char c = symbolString[i];
45+
if(unallowedChars.Contains(c))
46+
{
47+
capitalizeNext = true;
48+
continue;
49+
}
50+
else if(capitalizeNext)
51+
{
52+
characters[j++] = char.ToUpperInvariant(c);
53+
capitalizeNext = false;
54+
}
55+
else
56+
{
57+
characters[j++] = c;
58+
}
59+
}
60+
61+
return new(characters, 0, j);
3062
}
3163
}

0 commit comments

Comments
 (0)