From 894153349df179c7495471c7d6de98688816401d Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Tue, 19 Oct 2021 18:45:27 +0300 Subject: [PATCH] possibility to specify default value --- .../parameter/StringParameter.java | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/StringParameter.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/StringParameter.java index fee7adb..7a72cf3 100755 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/StringParameter.java +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/StringParameter.java @@ -9,8 +9,16 @@ import eu.svjatoslav.commons.commandline.parameterparser.Parameter; public class StringParameter extends Parameter { + 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 @@ -21,11 +29,12 @@ public class StringParameter extends Parameter { @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 -- 2.20.1