Skip to content

Commit 8fa7e9c

Browse files
committed
Restore command names case as in v3
This commit restores the kebab-case used for command names as in v3. Utils.unCamelify copied verbatim from v3. Resolves #1281
1 parent 6ad1448 commit 8fa7e9c

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

spring-shell-core/src/main/java/org/springframework/shell/core/command/annotation/support/CommandFactoryBean.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public Command getObject() {
7777

7878
// get command metadata
7979
String name = String.join(" ", command.name());
80-
name = name.isEmpty() ? this.method.getName() : name;
80+
name = name.isEmpty() ? Utils.unCamelify(this.method.getName()) : name;
8181
String description = command.description();
8282
description = description.isEmpty() ? "N/A" : description;
8383
String help = command.help();

spring-shell-core/src/main/java/org/springframework/shell/core/utils/Utils.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,23 @@
3939
*/
4040
public class Utils {
4141

42+
/**
43+
* Turn CamelCaseText into gnu-style-lowercase.
44+
*/
45+
public static String unCamelify(CharSequence original) {
46+
StringBuilder result = new StringBuilder(original.length());
47+
boolean wasLowercase = false;
48+
for (int i = 0; i < original.length(); i++) {
49+
char ch = original.charAt(i);
50+
if (Character.isUpperCase(ch) && wasLowercase) {
51+
result.append('-');
52+
}
53+
wasLowercase = Character.isLowerCase(ch);
54+
result.append(Character.toLowerCase(ch));
55+
}
56+
return result.toString();
57+
}
58+
4259
/**
4360
* Get a formatted string of available non-hidden commands from the command registry.
4461
* @param commandRegistry the command registry

spring-shell-core/src/test/java/org/springframework/shell/core/UtilsTests.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@
2323
import org.springframework.shell.core.command.CommandRegistry;
2424
import org.springframework.shell.core.utils.Utils;
2525

26+
import static org.assertj.core.api.Assertions.assertThat;
27+
2628
/**
2729
* Tests for {@link Utils}.
2830
*
@@ -31,6 +33,14 @@
3133
*/
3234
class UtilsTests {
3335

36+
@Test
37+
public void testUnCamelify() throws Exception {
38+
assertThat(Utils.unCamelify("HelloWorld")).isEqualTo("hello-world");
39+
assertThat(Utils.unCamelify("helloWorld")).isEqualTo("hello-world");
40+
assertThat(Utils.unCamelify("helloWorldHowAreYou")).isEqualTo("hello-world-how-are-you");
41+
assertThat(Utils.unCamelify("URL")).isEqualTo("url");
42+
}
43+
3444
@Test
3545
void testFormatAvailableCommands() {
3646
CommandRegistry commandRegistry = new CommandRegistry();

0 commit comments

Comments
 (0)