Skip to content
This repository was archived by the owner on Jun 3, 2025. It is now read-only.

Commit f149b10

Browse files
author
Aleksi Salmela
committed
Implement the command categories.
1 parent 6a2d639 commit f149b10

5 files changed

Lines changed: 78 additions & 14 deletions

File tree

src/main/java/fi/helsinki/cs/tmc/cli/Application.java

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
import org.apache.commons.cli.CommandLine;
1515
import org.apache.commons.cli.GnuParser;
16+
import org.apache.commons.cli.OptionBuilder;
1617
import org.apache.commons.cli.Options;
1718
import org.apache.commons.cli.ParseException;
1819
import org.slf4j.Logger;
@@ -23,6 +24,8 @@
2324
import java.util.Date;
2425
import java.util.List;
2526
import java.util.Map;
27+
import java.util.Set;
28+
import org.apache.commons.cli.Option;
2629

2730
/**
2831
* The application class for the program.
@@ -49,11 +52,26 @@ public Application(CliContext context) {
4952
this.context = context;
5053
this.io = context.getIo();
5154

52-
options.addOption("h", "help", false, "Display help information about tmc-cli");
55+
options.addOption(
56+
OptionBuilder.withLongOpt("help-all")
57+
.withDescription("Display all help information of tmc-cli")
58+
.create());
59+
options.addOption("h", "help", false, "Display help information");
5360
options.addOption("v", "version", false, "Give the version of the tmc-cli");
5461
options.addOption("u", "force-update", false, "Force the auto-update");
5562
options.addOption("d", "no-update", false, "Disable temporarily the auto-update");
5663

64+
Set<String> helpCategories = CommandFactory.getCommandCategories();
65+
for (String category : helpCategories) {
66+
if (category.equals("") || category.equals("hidden")) {
67+
continue;
68+
}
69+
options.addOption(
70+
OptionBuilder.withLongOpt("help-" + category)
71+
.withDescription("Display " + category + " help information")
72+
.create());
73+
}
74+
5775
//TODO implement the inTests as context.property
5876
if (!context.inTests()) {
5977
shutdownHandler = new ShutdownHandler(context.getIo());
@@ -103,6 +121,17 @@ private String[] parseArgs(String[] args) {
103121
return null;
104122
}
105123

124+
// handle help flags
125+
for (Option opt : line.getOptions()) {
126+
String helpPrefix = "help-";
127+
if (!opt.getLongOpt().startsWith(helpPrefix)) {
128+
continue;
129+
}
130+
131+
String helpCategory = opt.getLongOpt().substring(helpPrefix.length());
132+
runCommand("help", new String[] {helpCategory});
133+
return null;
134+
}
106135
if (showHelp) {
107136
// don't run the help sub-command with -h switch
108137
if (commandName.equals("help")) {

src/main/java/fi/helsinki/cs/tmc/cli/command/HelpCommand.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,7 @@ public void run(CliContext context, CommandLine args) {
4242

4343
private List<String> getCommandStrings() {
4444
List<String> strings = new ArrayList<>();
45-
Set<Class<Command>> commands = CommandFactory.getCommands();
46-
commands.remove(castToCommandClass(ShellHelperCommand.class));
47-
commands.remove(castToCommandClass(DocumentCommand.class));
45+
List<Class<Command>> commands = CommandFactory.getCommands();
4846

4947
longestNameLength = longestName(commands);
5048
for (Class<Command> commandClass : commands) {
@@ -69,7 +67,7 @@ private String createCommandString(Command command) {
6967
return builder.toString();
7068
}
7169

72-
private int longestName(Set<Class<Command>> commandList) {
70+
private int longestName(List<Class<Command>> commandList) {
7371
int longest = 0;
7472
for (Class<Command> commandClass : commandList) {
7573
Command command = CommandFactory.getCommand(commandClass);

src/main/java/fi/helsinki/cs/tmc/cli/core/CommandAnnotationProcessor.java

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ public class CommandAnnotationProcessor extends AbstractProcessor {
2828

2929
private static final String CLASS_NAME = "CommandList";
3030
private static final String PACKAGE_NAME = "fi.helsinki.cs.tmc.cli.core";
31+
private static final String COMMAND_PACKAGE_NAME = "fi.helsinki.cs.tmc.cli.command";
3132
private static final String TAB = " ";
3233

34+
//TODO reorder
3335
private void generateSourceFile(Map<String, String> map) throws IOException {
3436
JavaFileObject jfo =
3537
processingEnv.getFiler().createSourceFile(PACKAGE_NAME + "." + CLASS_NAME);
@@ -43,25 +45,41 @@ private void generateSourceFile(Map<String, String> map) throws IOException {
4345
for (Entry<String, String> entry : map.entrySet()) {
4446
bwriter.append("import ").append(entry.getValue()).append(";\n");
4547
}
46-
bwriter.append("//CHECKSTYLE:ON\n");
4748

4849
bwriter.append("\npublic class " + CLASS_NAME + " {\n");
4950
bwriter.append(TAB + "static {\n");
5051
for (Entry<String, String> entry : map.entrySet()) {
51-
String[] parts = entry.getValue().split("\\.");
52-
if (parts.length == 0) {
52+
String classPath = entry.getValue();
53+
String[] parts = classPath.split("\\.");
54+
if (parts.length < 2) {
5355
continue;
5456
}
55-
// print out the lines that add the commands to the command factory.
57+
5658
String className = parts[parts.length - 1];
59+
String packageName = "?";
60+
if (classPath.startsWith(COMMAND_PACKAGE_NAME)) {
61+
//remove class name and dot
62+
packageName = classPath.substring(
63+
0,
64+
classPath.length() - className.length() - 1);
65+
//remove prefix
66+
packageName = packageName.substring(COMMAND_PACKAGE_NAME.length());
67+
//remove the first dot
68+
if(packageName.startsWith(".")) {
69+
packageName = packageName.substring(1);
70+
}
71+
}
5772
bwriter.append(TAB + TAB + "CommandFactory.addCommand(\"")
5873
.append(entry.getKey())
74+
.append("\", \"")
75+
.append(packageName)
5976
.append("\", ")
6077
.append(className)
6178
.append(".class);\n");
6279
}
6380
bwriter.append(TAB + "}\n");
6481
bwriter.append("}\n");
82+
bwriter.append("//CHECKSTYLE:ON\n");
6583
bwriter.flush();
6684
}
6785
}

src/main/java/fi/helsinki/cs/tmc/cli/core/CommandFactory.java

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@
33
import org.slf4j.LoggerFactory;
44

55
import java.lang.annotation.Annotation;
6+
import java.util.ArrayList;
67
import java.util.HashMap;
78
import java.util.HashSet;
9+
import java.util.List;
810
import java.util.Map;
911
import java.util.Set;
1012

@@ -15,6 +17,7 @@ public class CommandFactory {
1517

1618
private static final org.slf4j.Logger logger = LoggerFactory.getLogger(CommandFactory.class);
1719
private static final Map<String, Class<Command>> commands = new HashMap<>();
20+
private static final Map<String, List<Class<Command>>> packages = new HashMap<>();
1821

1922
static {
2023
/* Force load the CommandList so that it's static initialization block is executed.
@@ -33,11 +36,19 @@ public class CommandFactory {
3336
* This method is used for generating the commands list from the annotations.
3437
*
3538
* @param name the name visible to the user
39+
* @param packageName the package name that is used to categorize the commands
3640
* @param commandClass the class of the command objects
3741
*/
38-
public static void addCommand(String name, Class commandClass) {
42+
public static void addCommand(String name, String packageName, Class commandClass) {
3943
Class<Command> klass = castToCommandClass(commandClass);
4044
CommandFactory.commands.put(name, klass);
45+
46+
List<Class<Command>> list = CommandFactory.packages.get(packageName);
47+
if (list == null) {
48+
list = new ArrayList<>();
49+
CommandFactory.packages.put(packageName, list);
50+
}
51+
list.add(klass);
4152
}
4253

4354
/**
@@ -94,8 +105,16 @@ public static Command getCommand(Class<Command> commandClass) {
94105
*
95106
* @return Set of commands.
96107
*/
97-
public static Set<Class<Command>> getCommands() {
98-
return new HashSet<>(CommandFactory.commands.values());
108+
public static List<Class<Command>> getCommands() {
109+
return new ArrayList<>(CommandFactory.commands.values());
110+
}
111+
112+
public static Set<String> getCommandCategories() {
113+
return packages.keySet();
114+
}
115+
116+
public static List<Class<Command>> getCategoryCommands(String category) {
117+
return packages.get(category);
99118
}
100119

101120
@SuppressWarnings("unchecked")

src/test/java/fi/helsinki/cs/tmc/cli/core/CommandAnnotationProcessorTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ public void generateCorrectCodeWithTwoCommands() {
191191

192192
Set<TypeElement> annotations = new HashSet<>();
193193
assertTrue(processor.process(annotations, roundEnv));
194-
assertThat("" + stringWriter, containsString("(\"commmand1\", TestTest1.class)"));
195-
assertThat("" + stringWriter, containsString("(\"commmand2\", TestTest2.class)"));
194+
assertThat("" + stringWriter, containsString("(\"commmand1\", \"?\", TestTest1.class)"));
195+
assertThat("" + stringWriter, containsString("(\"commmand2\", \"?\", TestTest2.class)"));
196196
}
197197
}

0 commit comments

Comments
 (0)