From: Svjatoslav Agejenko Date: Sun, 27 Oct 2013 19:17:21 +0000 (+0200) Subject: further improvements to commandline parser API X-Git-Tag: svjatoslavcommons-1.8~129 X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=a2cdc870766514bc5b40e645eaabfd6ab51ba614;p=svjatoslav_commons.git further improvements to commandline parser API --- diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parameter.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parameter.java index 22cc927..d9e78da 100755 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parameter.java +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parameter.java @@ -117,13 +117,6 @@ public class Parameter { return buffer.toString(); } - public File getArgumentAsFile() { - if (arguments.size() != 1) - throw new RuntimeException("Parameter " + description - + " shall have exactly 1 argument."); - return new File(arguments.get(0)); - } - public List getArgumentsAsFiles() { final ArrayList result = new ArrayList(); diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parser.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parser.java index 075ca11..f5ecf72 100755 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parser.java +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parser.java @@ -14,6 +14,7 @@ import java.util.ArrayList; import eu.svjatoslav.commons.commandline.parameterparser.parameter.DirectoryParameter; import eu.svjatoslav.commons.commandline.parameterparser.parameter.FileParameter; import eu.svjatoslav.commons.commandline.parameterparser.parameter.IntegerParameter; +import eu.svjatoslav.commons.commandline.parameterparser.parameter.NullParameter; import eu.svjatoslav.commons.commandline.parameterparser.parameter.StringParameter; /** @@ -29,6 +30,14 @@ public class Parser { parameters.add(parameter); } + public Parameter createCustomParameter(final Argument paramaterType, + final String description) { + final Parameter parameter = new Parameter(paramaterType, description, + ArgumentCount.SINGLE); + parameters.add(parameter); + return parameter; + } + public DirectoryParameter createDirectoryParameter(final String description) { final DirectoryParameter parameter = new DirectoryParameter(description); parameters.add(parameter); @@ -47,6 +56,12 @@ public class Parser { return parameter; } + public NullParameter createNullParameter(final String description) { + final NullParameter parameter = new NullParameter(description); + parameters.add(parameter); + return parameter; + } + public StringParameter createStringParameter(final String description) { final StringParameter parameter = new StringParameter(description); parameters.add(parameter); diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/NullParameter.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/NullParameter.java new file mode 100644 index 0000000..cadadd1 --- /dev/null +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/NullParameter.java @@ -0,0 +1,27 @@ +package eu.svjatoslav.commons.commandline.parameterparser.parameter; + +import eu.svjatoslav.commons.commandline.parameterparser.ArgumentCount; +import eu.svjatoslav.commons.commandline.parameterparser.Parameter; + +public class NullParameter extends Parameter { + + public NullParameter(final String description) { + super(null, description, ArgumentCount.SINGLE); + } + + @Override + public NullParameter addAliases(final String... aliasArray) { + super.addAliases(aliasArray); + return this; + } + + public boolean getValue() { + return isParameterSpecified(); + } + + @Override + public NullParameter setMandatory() { + mandatory = true; + return this; + } +} 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 a862cbb..29967bc 100644 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/StringParameter.java +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/StringParameter.java @@ -30,4 +30,5 @@ public class StringParameter extends Parameter { mandatory = true; return this; } + }