Просмотр исходного кода

Externalize the description strings of the command-line arguments.

Timur Sağlam 4 лет назад
Родитель
Сommit
58e95a3501

+ 38 - 25
jplag/src/main/java/de/jplag/CommandLineArgument.java

@@ -1,8 +1,8 @@
 package de.jplag;
 
 import static de.jplag.options.JPlagOptions.DEFAULT_COMPARISON_MODE;
-import static de.jplag.options.JPlagOptions.DEFAULT_SIMILARITY_THRESHOLD;
 import static de.jplag.options.JPlagOptions.DEFAULT_SHOWN_COMPARISONS;
+import static de.jplag.options.JPlagOptions.DEFAULT_SIMILARITY_THRESHOLD;
 import static net.sourceforge.argparse4j.impl.Arguments.storeTrue;
 
 import java.util.Collection;
@@ -20,46 +20,46 @@ import net.sourceforge.argparse4j.inf.Namespace;
  * @author Timur Saglam
  */
 public enum CommandLineArgument {
-    ROOT_DIRECTORY("rootDir", String.class, "The root-directory that contains all submissions"),
-    LANGUAGE("-l", String.class, "Select the language to parse the submissions", LanguageOption.getDefault().getDisplayName(), LanguageOption.getAllDisplayNames()),
-    BASE_CODE("-bc", String.class, "Name of the subdirectory of the root directory which contains the base code (common framework used in all submissions)"),
-    VERBOSITY("-v", String.class, "Verbosity", "quiet", List.of("parser", "quiet", "long", "details")), // TODO SH: Replace verbosity when integrating a real logging library
-    DEBUG("-d", Boolean.class, "(Debug) parser. Non-parsable files will be stored"),
-    SUBDIRECTORY("-S", String.class, "Look in directories <root-dir>/*/<dir> for programs"),
-    SUFFIXES("-p", String.class, "comma-separated list of all filename suffixes that are included"),
-    EXCLUDE_FILE("-x", String.class, "All files named in this file will be ignored in the comparison (line-separated list)"),
-    MIN_TOKEN_MATCH("-t", Integer.class, "Tunes the comparison sensitivity by adjusting the minimum token required to be counted as matching section. A smaller <n> increases the sensitivity but might lead to more false-positves"),
-    SIMILARITY_THRESHOLD("-m", Float.class, "Match similarity threshold [0-100]: All matches above this threshold will be saved", DEFAULT_SIMILARITY_THRESHOLD),
-    SHOWN_COMPARISONS("-n", Integer.class, "The maximum number of comparisons that will be shown in the generated report, if set to -1 all comparisons will be shown", DEFAULT_SHOWN_COMPARISONS),
-    RESULT_FOLDER("-r", String.class, "Name of directory in which the comparison results will be stored", "result"),
-    COMPARISON_MODE("-c", String.class, "Comparison mode used to compare the programs", DEFAULT_COMPARISON_MODE.getName(), ComparisonMode.allNames());
+    ROOT_DIRECTORY("rootDir", String.class),
+    LANGUAGE("-l", String.class, LanguageOption.getDefault().getDisplayName(), LanguageOption.getAllDisplayNames()),
+    BASE_CODE("-bc", String.class),
+    VERBOSITY("-v", String.class, "quiet", List.of("parser", "quiet", "long", "details")), // TODO SH: Replace verbosity when integrating a real logging library
+    DEBUG("-d", Boolean.class),
+    SUBDIRECTORY("-S", String.class),
+    SUFFIXES("-p", String.class),
+    EXCLUDE_FILE("-x", String.class),
+    MIN_TOKEN_MATCH("-t", Integer.class),
+    SIMILARITY_THRESHOLD("-m", Float.class, DEFAULT_SIMILARITY_THRESHOLD),
+    SHOWN_COMPARISONS("-n", Integer.class, DEFAULT_SHOWN_COMPARISONS),
+    RESULT_FOLDER("-r", String.class, "result"),
+    COMPARISON_MODE("-c", String.class, DEFAULT_COMPARISON_MODE.getName(), ComparisonMode.allNames());
 
     private final String flag;
-    private final String helptext;
+    private final String description;
     private final Optional<Object> defaultValue;
     private final Optional<Collection<String>> choices;
     private final Class<?> type;
 
-    private CommandLineArgument(String flag, Class<?> type, String helpText) {
-        this(flag, type, helpText, Optional.empty(), Optional.empty());
+    private CommandLineArgument(String flag, Class<?> type) {
+        this(flag, type, Optional.empty(), Optional.empty());
     }
 
-    private CommandLineArgument(String flag, Class<?> type, String helpText, Object defaultValue) {
-        this(flag, type, helpText, Optional.of(defaultValue), Optional.empty());
+    private CommandLineArgument(String flag, Class<?> type, Object defaultValue) {
+        this(flag, type, Optional.of(defaultValue), Optional.empty());
     }
 
-    private CommandLineArgument(String flag, Class<?> type, String helpText, Object defaultValue, Collection<String> choices) {
-        this(flag, type, helpText, Optional.of(defaultValue), Optional.of(choices));
+    private CommandLineArgument(String flag, Class<?> type, Object defaultValue, Collection<String> choices) {
+        this(flag, type, Optional.of(defaultValue), Optional.of(choices));
     }
 
-    private CommandLineArgument(String flag, Class<?> type, String helpText, Optional<Object> defaultValue, Optional<Collection<String>> choices) {
+    private CommandLineArgument(String flag, Class<?> type, Optional<Object> defaultValue, Optional<Collection<String>> choices) {
         this.flag = flag;
-        this.helptext = helpText;
         this.type = type;
         this.defaultValue = defaultValue;
         this.choices = choices;
+        this.description = retrieveDescriptionFromMessages();
     }
-    
+
     /**
      * @return the flag name of the command line argument.
      */
@@ -90,7 +90,7 @@ public enum CommandLineArgument {
      * @param parser is that parser.
      */
     public void parseWith(ArgumentParser parser) {
-        Argument argument = parser.addArgument(flag).help(helptext);
+        Argument argument = parser.addArgument(flag).help(description);
         choices.ifPresent(it -> argument.choices(it));
         defaultValue.ifPresent(it -> argument.setDefault(it));
         argument.type(type);
@@ -98,4 +98,17 @@ public enum CommandLineArgument {
             argument.action(storeTrue());
         }
     }
+
+    /**
+     * Dynamically loads the description from the message file. For an option named <code>NEW_OPTION</code> the messages key should
+     * be <code>CommandLineArgument.NewOption</code>.
+     */
+    private String retrieveDescriptionFromMessages() {
+        StringBuilder builder = new StringBuilder();
+        for (String substring : toString().toLowerCase().split("_")) {
+            builder.append(substring.substring(0, 1).toUpperCase());
+            builder.append(substring.substring(1));
+        }
+        return Messages.getString(getClass().getSimpleName() + "." + builder.toString());
+    }
 }

+ 21 - 0
jplag/src/main/java/de/jplag/Messages.java

@@ -0,0 +1,21 @@
+package de.jplag;
+
+import java.util.MissingResourceException;
+import java.util.ResourceBundle;
+
+public class Messages {
+    private static final String BUNDLE_NAME = "de.jplag.messages"; //$NON-NLS-1$
+
+    private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle.getBundle(BUNDLE_NAME);
+
+    private Messages() {
+    }
+
+    public static String getString(String key) {
+        try {
+            return RESOURCE_BUNDLE.getString(key);
+        } catch (MissingResourceException e) {
+            return '!' + key + '!';
+        }
+    }
+}

+ 13 - 0
jplag/src/main/resources/de/jplag/messages.properties

@@ -0,0 +1,13 @@
+CommandLineArgument.BaseCode=Name of the subdirectory of the root directory which contains the base code (common framework used in all submissions)
+CommandLineArgument.ComparisonMode=Comparison mode used to compare the programs
+CommandLineArgument.Debug=Debug parser. Non-parsable files will be stored
+CommandLineArgument.Suffixes=comma-separated list of all filename suffixes that are included
+CommandLineArgument.ExcludeFile=All files named in this file will be ignored in the comparison (line-separated list)
+CommandLineArgument.Language=Select the language to parse the submissions
+CommandLineArgument.MinTokenMatch=Tunes the comparison sensitivity by adjusting the minimum token required to be counted as matching section. A smaller <n> increases the sensitivity but might lead to more false-positves
+CommandLineArgument.ResultFolder=Name of directory in which the comparison results will be stored
+CommandLineArgument.RootDirectory=The root-directory that contains all submissions
+CommandLineArgument.ShownComparisons=The maximum number of comparisons that will be shown in the generated report, if set to -1 all comparisons will be shown
+CommandLineArgument.SimilarityThreshold=Match similarity threshold [0-100]: All matches above this threshold will be saved
+CommandLineArgument.Subdirectory=Look in directories <root-dir>/*/<dir> for programs
+CommandLineArgument.Verbosity=Verbosity of the logging