/*
- * Svjatoslav Commons - shared library of common functionality.
- * Copyright ©2012-2014, 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;
import eu.svjatoslav.commons.commandline.parameterparser.Parameter;
-public class StringParameter extends Parameter<StringParameter> {
+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
@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