Refactored exception dialog.
[svjatoslav_commons.git] / src / main / java / eu / svjatoslav / commons / commandline / parameterparser / parameter / StringParameter.java
index 64c56fc..7a72cf3 100755 (executable)
@@ -1,12 +1,7 @@
 /*
- * Svjatoslav Commons - shared library of common functionality.
- * Copyright ©2012-2017, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 3 of the GNU Lesser General Public License
- * or later as published by the Free Software Foundation.
+ * Svjatoslav Commons - shared library of common functionality. Author: Svjatoslav Agejenko.
+ * This project is released under Creative Commons Zero (CC0) license.
  */
-
 package eu.svjatoslav.commons.commandline.parameterparser.parameter;
 
 import eu.svjatoslav.commons.commandline.parameterparser.ArgumentCount;
@@ -14,8 +9,16 @@ import eu.svjatoslav.commons.commandline.parameterparser.Parameter;
 
 public class StringParameter extends Parameter<String, StringParameter> {
 
+    public final String defaultValue;
+
     public StringParameter(final String description) {
         super(description, ArgumentCount.SINGLE);
+        defaultValue = null;
+    }
+    public StringParameter(final String description, String defaultValue) {
+        super(description, ArgumentCount.SINGLE);
+        this.defaultValue = defaultValue;
+        this.setSpecified(true);
     }
 
     @Override
@@ -26,11 +29,12 @@ public class StringParameter extends Parameter<String, StringParameter> {
     @Override
     public String getValue() {
 
-        if (arguments.size() != 1)
-            throw new RuntimeException("Parameter " + description
-                    + " shall have exactly 1 argument.");
+        if (arguments.isEmpty() && (defaultValue != null)) return defaultValue;
+
+        if (arguments.size() == 1) return arguments.get(0);
 
-        return arguments.get(0);
+        throw new RuntimeException("Parameter " + description
+                + " shall have exactly 1 argument.");
     }
 
     @Override