Browse Source

adding default values.

Klaas, Wilfried 6 years ago
parent
commit
a395083ddb
1 changed files with 20 additions and 5 deletions
  1. 20 5
      src/main/java/de/mcs/utils/jsap/CommandlineProcessor.java

+ 20 - 5
src/main/java/de/mcs/utils/jsap/CommandlineProcessor.java

@@ -65,7 +65,6 @@ public class CommandlineProcessor {
       processSwitchOption(name, method);
       processSwitchOption(name, method);
       processStringOption(name, method);
       processStringOption(name, method);
       processFileOption(name, method);
       processFileOption(name, method);
-
     });
     });
   }
   }
 
 
@@ -77,6 +76,11 @@ public class CommandlineProcessor {
           File file = commandLineArgs.getFile(annotation.name());
           File file = commandLineArgs.getFile(annotation.name());
           if ((file.getName() != null) && !"".equals(file.getName())) {
           if ((file.getName() != null) && !"".equals(file.getName())) {
             method.invoke(null, file);
             method.invoke(null, file);
+          } else {
+            if (annotation.defaultValue() != null) {
+              file = new File(annotation.defaultValue());
+              method.invoke(null, file);
+            }
           }
           }
         }
         }
       } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e1) {
       } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e1) {
@@ -89,7 +93,14 @@ public class CommandlineProcessor {
     StringOption annotation = method.getAnnotation(StringOption.class);
     StringOption annotation = method.getAnnotation(StringOption.class);
     if (annotation != null) {
     if (annotation != null) {
       try {
       try {
-        method.invoke(null, commandLineArgs.getString(annotation.name()));
+        String value = commandLineArgs.getString(annotation.name());
+        if ((value == null) || value.isEmpty()) {
+          method.invoke(null, commandLineArgs.getString(annotation.name()));
+        } else {
+          if (annotation.defaultValue() != null) {
+            method.invoke(null, annotation.defaultValue());
+          }
+        }
       } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e1) {
       } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e1) {
         e1.printStackTrace();
         e1.printStackTrace();
       }
       }
@@ -100,7 +111,11 @@ public class CommandlineProcessor {
     SwitchOption annotation = method.getAnnotation(SwitchOption.class);
     SwitchOption annotation = method.getAnnotation(SwitchOption.class);
     if (annotation != null) {
     if (annotation != null) {
       try {
       try {
-        method.invoke(null, commandLineArgs.getBoolean(name));
+        if (commandLineArgs.contains(name)) {
+          method.invoke(null, commandLineArgs.getBoolean(name));
+        } else {
+          method.invoke(null, annotation.defaultValue());
+        }
       } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e1) {
       } catch (IllegalAccessException | IllegalArgumentException | InvocationTargetException e1) {
         e1.printStackTrace();
         e1.printStackTrace();
       }
       }
@@ -149,8 +164,8 @@ public class CommandlineProcessor {
   }
   }
 
 
   /**
   /**
-   * This function parses the commandline parameters. If the parameter -h or
-   * --help is avalible, generate a little help text.
+   * This function parses the commandline parameters. If the parameter -h or --help is avalible, generate a little help
+   * text.
    * 
    * 
    * @param mainClass
    * @param mainClass
    * 
    *