Jelajahi Sumber

littel refactoring

Wilfried Klaas 6 tahun lalu
induk
melakukan
a21bb734b7

+ 14 - 0
src/main/java/de/mcs/utils/jsap/Command.java

@@ -0,0 +1,14 @@
+package de.mcs.utils.jsap;
+
+import static java.lang.annotation.RetentionPolicy.RUNTIME;
+
+import java.lang.annotation.ElementType;
+import java.lang.annotation.Retention;
+import java.lang.annotation.Target;
+
+@Retention(RUNTIME)
+@Target(ElementType.TYPE)
+public @interface Command {
+
+  String help();
+}

+ 49 - 20
src/main/java/de/mcs/utils/jsap/ProcessCommandline.java → src/main/java/de/mcs/utils/jsap/CommandlineProcessor.java

@@ -33,7 +33,7 @@ import java.util.Set;
 import org.reflections.Reflections;
 import org.reflections.Reflections;
 import org.reflections.scanners.FieldAnnotationsScanner;
 import org.reflections.scanners.FieldAnnotationsScanner;
 import org.reflections.scanners.MethodAnnotationsScanner;
 import org.reflections.scanners.MethodAnnotationsScanner;
-import org.reflections.util.FilterBuilder;
+import org.reflections.scanners.TypeAnnotationsScanner;
 
 
 import com.martiansoftware.jsap.FlaggedOption;
 import com.martiansoftware.jsap.FlaggedOption;
 import com.martiansoftware.jsap.JSAP;
 import com.martiansoftware.jsap.JSAP;
@@ -47,18 +47,18 @@ import com.martiansoftware.jsap.stringparsers.StringStringParser;
  * @author wklaa_000
  * @author wklaa_000
  *
  *
  */
  */
-public class ProcessCommandline {
-
-  public static final FilterBuilder TestModelFilter = new FilterBuilder().include("de.mcs.*\\$.*");
+public class CommandlineProcessor {
 
 
   private static JSAP parser;
   private static JSAP parser;
   private static JSAPResult commandLineArgs;
   private static JSAPResult commandLineArgs;
 
 
   private static Map<String, Method> parameterMethods;
   private static Map<String, Method> parameterMethods;
+  private static Reflections reflections;
+  private static Command helpContext;
 
 
-  public static void processCommandline(String[] args) {
+  public static void processCommandline(Class class1, String[] args) {
     parameterMethods = new HashMap<>();
     parameterMethods = new HashMap<>();
-    parseParameters(args);
+    parseParameters(class1, args);
     checkParams();
     checkParams();
     processParameters();
     processParameters();
   };
   };
@@ -97,7 +97,7 @@ public class ProcessCommandline {
   }
   }
 
 
   private static void processSwitchOption(String name, Method method) {
   private static void processSwitchOption(String name, Method method) {
-    Switch annotation = method.getAnnotation(Switch.class);
+    SwitchOption annotation = method.getAnnotation(SwitchOption.class);
     if (annotation != null) {
     if (annotation != null) {
       try {
       try {
         method.invoke(null, commandLineArgs.getBoolean(name));
         method.invoke(null, commandLineArgs.getBoolean(name));
@@ -128,11 +128,23 @@ public class ProcessCommandline {
 
 
   }
   }
 
 
+  /**
+   * showing the actual help page in console.
+   */
+  public static void showHelp() {
+    showHelp(null);
+  }
+
   /**
   /**
    * showing the actual help page in console.
    * showing the actual help page in console.
    */
    */
   public static void showHelp(String message) {
   public static void showHelp(String message) {
-    System.out.println(message);
+    if (helpContext != null) {
+      System.out.println(helpContext.help());
+    }
+    if (message != null) {
+      System.out.println(message);
+    }
     System.out.println(parser.getHelp());
     System.out.println(parser.getHelp());
   }
   }
 
 
@@ -140,12 +152,18 @@ public class ProcessCommandline {
    * This function parses the commandline parameters. If the parameter -h or
    * This function parses the commandline parameters. If the parameter -h or
    * --help is avalible, generate a little help text.
    * --help is avalible, generate a little help text.
    * 
    * 
+   * @param class1
+   * 
    * @param args
    * @param args
    *          Commadline arguments
    *          Commadline arguments
    */
    */
-  public static void parseParameters(final String[] args) {
+  public static void parseParameters(Class class1, final String[] args) {
+    reflections = new Reflections("", new TypeAnnotationsScanner(), new MethodAnnotationsScanner(),
+        new FieldAnnotationsScanner());
+
     parser = new JSAP();
     parser = new JSAP();
     try {
     try {
+      registerHelpContext(class1);
       // registering the parameters with default values
       // registering the parameters with default values
       registerDefaultParameter();
       registerDefaultParameter();
       // parsing the commadline
       // parsing the commadline
@@ -158,6 +176,13 @@ public class ProcessCommandline {
     }
     }
   }
   }
 
 
+  private static void registerHelpContext(Class class1) {
+    Command annotation = (Command) class1.getAnnotation(Command.class);
+    if (annotation != null) {
+      helpContext = annotation;
+    }
+  }
+
   /**
   /**
    * registering the parameters to the JSAP Parser.
    * registering the parameters to the JSAP Parser.
    * 
    * 
@@ -166,16 +191,15 @@ public class ProcessCommandline {
    */
    */
   private static void registerDefaultParameter() throws JSAPException {
   private static void registerDefaultParameter() throws JSAPException {
     Map<Integer, UnflaggedOption> unflaggedOptions = new HashMap<>();
     Map<Integer, UnflaggedOption> unflaggedOptions = new HashMap<>();
-    Reflections reflections = new Reflections("", new MethodAnnotationsScanner(), new FieldAnnotationsScanner());
-
-    // JSAPHelper.registerAsSwitch(parser, HELP);
 
 
     // MethodAnnotationsScanner
     // MethodAnnotationsScanner
-    Set<Method> methodes = reflections.getMethodsAnnotatedWith(Switch.class);
+    Set<Method> methodes = reflections.getMethodsAnnotatedWith(SwitchOption.class);
     for (Method method : methodes) {
     for (Method method : methodes) {
-      Switch annotation = method.getAnnotation(Switch.class);
-      System.out.printf("short: %s, long: %s, name: %s, help: %s, def: %s, req: %s\r\n", annotation.shortKey(),
-          annotation.longKey(), annotation.name(), annotation.help(), annotation.defaultValue(), annotation.required());
+      SwitchOption annotation = method.getAnnotation(SwitchOption.class);
+      // System.out.printf("short: %s, long: %s, name: %s, help: %s, def: %s,
+      // req: %s\r\n", annotation.shortKey(),
+      // annotation.longKey(), annotation.name(), annotation.help(),
+      // annotation.defaultValue(), annotation.required());
 
 
       com.martiansoftware.jsap.Switch swtOption = new com.martiansoftware.jsap.Switch(annotation.name(),
       com.martiansoftware.jsap.Switch swtOption = new com.martiansoftware.jsap.Switch(annotation.name(),
           annotation.shortKey(), annotation.longKey());
           annotation.shortKey(), annotation.longKey());
@@ -186,6 +210,7 @@ public class ProcessCommandline {
       parser.registerParameter(swtOption);
       parser.registerParameter(swtOption);
       parameterMethods.put(annotation.name(), method);
       parameterMethods.put(annotation.name(), method);
     }
     }
+
     // // FieldAnnotationsScanner
     // // FieldAnnotationsScanner
     // Set<Field> fields = reflections.getFieldsAnnotatedWith(Switch.class);
     // Set<Field> fields = reflections.getFieldsAnnotatedWith(Switch.class);
     // for (Field field : fields) {
     // for (Field field : fields) {
@@ -196,8 +221,10 @@ public class ProcessCommandline {
     methodes = reflections.getMethodsAnnotatedWith(StringOption.class);
     methodes = reflections.getMethodsAnnotatedWith(StringOption.class);
     for (Method method : methodes) {
     for (Method method : methodes) {
       StringOption annotation = method.getAnnotation(StringOption.class);
       StringOption annotation = method.getAnnotation(StringOption.class);
-      System.out.printf("short: %s, long: %s, name: %s, help: %s, def: %s, req: %s\r\n", annotation.shortKey(),
-          annotation.longKey(), annotation.name(), annotation.help(), annotation.defaultValue(), annotation.required());
+      // System.out.printf("short: %s, long: %s, name: %s, help: %s, def: %s,
+      // req: %s\r\n", annotation.shortKey(),
+      // annotation.longKey(), annotation.name(), annotation.help(),
+      // annotation.defaultValue(), annotation.required());
 
 
       StringStringParser stringParser = StringStringParser.getParser();
       StringStringParser stringParser = StringStringParser.getParser();
       if (annotation.index() > 0) {
       if (annotation.index() > 0) {
@@ -218,8 +245,10 @@ public class ProcessCommandline {
     methodes = reflections.getMethodsAnnotatedWith(FileOption.class);
     methodes = reflections.getMethodsAnnotatedWith(FileOption.class);
     for (Method method : methodes) {
     for (Method method : methodes) {
       FileOption annotation = method.getAnnotation(FileOption.class);
       FileOption annotation = method.getAnnotation(FileOption.class);
-      System.out.printf("short: %s, long: %s, name: %s, help: %s, def: %s, req: %s\r\n", annotation.shortKey(),
-          annotation.longKey(), annotation.name(), annotation.help(), annotation.defaultValue(), annotation.required());
+      // System.out.printf("short: %s, long: %s, name: %s, help: %s, def: %s,
+      // req: %s\r\n", annotation.shortKey(),
+      // annotation.longKey(), annotation.name(), annotation.help(),
+      // annotation.defaultValue(), annotation.required());
 
 
       FileStringParser fileStringParser = FileStringParser.getParser();
       FileStringParser fileStringParser = FileStringParser.getParser();
       fileStringParser.setMustBeDirectory(annotation.mustBeDirectory());
       fileStringParser.setMustBeDirectory(annotation.mustBeDirectory());

+ 1 - 1
src/main/java/de/mcs/utils/jsap/Switch.java → src/main/java/de/mcs/utils/jsap/SwitchOption.java

@@ -34,7 +34,7 @@ import java.lang.annotation.Target;
  * @author wklaa_000
  * @author wklaa_000
  *
  *
  */
  */
-public @interface Switch {
+public @interface SwitchOption {
   char shortKey();
   char shortKey();
 
 
   String longKey();
   String longKey();