/**
* Indicates that at least one argument is mandatory for this parameter.
*/
- private boolean mandatory;
+ protected boolean mandatory;
private final Argument argumentType;
return new File(arguments.get(0));
}
- public int getArgumentAsInteger() {
- if (arguments.size() != 1)
- throw new RuntimeException("Parameter " + description
- + " shall have exactly 1 argument.");
- return Integer.parseInt(arguments.get(0));
- }
-
public List<File> getArgumentsAsFiles() {
final ArrayList<File> result = new ArrayList<File>();
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.StringParameter;
/**
parameters.add(parameter);
}
+ public DirectoryParameter createDirectoryParameter(final String description) {
+ final DirectoryParameter parameter = new DirectoryParameter(description);
+ parameters.add(parameter);
+ return parameter;
+ }
+
+ public FileParameter createFileParameter(final String description) {
+ final FileParameter parameter = new FileParameter(description);
+ parameters.add(parameter);
+ return parameter;
+ }
+
+ public IntegerParameter createIntegerParameter(final String description) {
+ final IntegerParameter parameter = new IntegerParameter(description);
+ parameters.add(parameter);
+ return parameter;
+ }
+
public StringParameter createStringParameter(final String description) {
final StringParameter parameter = new StringParameter(description);
parameters.add(parameter);
--- /dev/null
+package eu.svjatoslav.commons.commandline.parameterparser.parameter;
+
+import java.io.File;
+
+import eu.svjatoslav.commons.commandline.parameterparser.Argument;
+import eu.svjatoslav.commons.commandline.parameterparser.ArgumentCount;
+import eu.svjatoslav.commons.commandline.parameterparser.Parameter;
+import eu.svjatoslav.commons.commandline.parameterparser.type.StringArgument;
+
+public class DirectoryParameter extends Parameter implements Argument {
+
+ private ExistanceType existanceType = ExistanceType.DOES_NOT_MATTER;
+
+ public DirectoryParameter(final String description) {
+ super(new StringArgument(), description, ArgumentCount.SINGLE);
+ }
+
+ @Override
+ public DirectoryParameter addAliases(final String... aliasArray) {
+ super.addAliases(aliasArray);
+ return this;
+ }
+
+ @Override
+ public java.lang.String describeFormat() {
+ return existanceType.description + "directory";
+ }
+
+ public File getValue() {
+
+ if (arguments.size() != 1)
+ throw new RuntimeException("Parameter " + description
+ + " shall have exactly 1 argument.");
+
+ return new File(arguments.get(0));
+ }
+
+ public DirectoryParameter mustExist() {
+ existanceType = ExistanceType.MUST_EXIST;
+ return this;
+ }
+
+ public DirectoryParameter mustNotExist() {
+ existanceType = ExistanceType.MUST_NOT_EXIST;
+ return this;
+ }
+
+ @Override
+ public DirectoryParameter setMandatory() {
+ mandatory = true;
+ return this;
+ }
+
+ @Override
+ public boolean validate(final java.lang.String value) {
+ final File file = new File(value);
+
+ if (existanceType == ExistanceType.MUST_EXIST) {
+ if (file.exists() && file.isDirectory())
+ return true;
+ return false;
+ }
+
+ if (existanceType == ExistanceType.MUST_NOT_EXIST) {
+ if (file.exists())
+ return false;
+ return true;
+ }
+
+ if (existanceType == ExistanceType.DOES_NOT_MATTER) {
+ if (file.exists())
+ if (file.isFile())
+ return false;
+
+ return true;
+ }
+
+ return false;
+ }
+
+}
--- /dev/null
+package eu.svjatoslav.commons.commandline.parameterparser.parameter;
+
+public enum ExistanceType {
+
+ MUST_EXIST("existing "), MUST_NOT_EXIST("not existing "), DOES_NOT_MATTER(
+ "");
+
+ public final String description;
+
+ ExistanceType(final String description) {
+ this.description = description;
+ }
+
+}
--- /dev/null
+package eu.svjatoslav.commons.commandline.parameterparser.parameter;
+
+import java.io.File;
+
+import eu.svjatoslav.commons.commandline.parameterparser.Argument;
+import eu.svjatoslav.commons.commandline.parameterparser.ArgumentCount;
+import eu.svjatoslav.commons.commandline.parameterparser.Parameter;
+import eu.svjatoslav.commons.commandline.parameterparser.type.StringArgument;
+
+public class FileParameter extends Parameter implements Argument {
+
+ private ExistanceType existanceType = ExistanceType.DOES_NOT_MATTER;
+
+ public FileParameter(final String description) {
+ super(new StringArgument(), description, ArgumentCount.SINGLE);
+ }
+
+ @Override
+ public FileParameter addAliases(final String... aliasArray) {
+ super.addAliases(aliasArray);
+ return this;
+ }
+
+ @Override
+ public java.lang.String describeFormat() {
+ return existanceType.description + "file";
+ }
+
+ public File getValue() {
+
+ if (arguments.size() != 1)
+ throw new RuntimeException("Parameter " + description
+ + " shall have exactly 1 argument.");
+
+ return new File(arguments.get(0));
+ }
+
+ public FileParameter mustExist() {
+ existanceType = ExistanceType.MUST_EXIST;
+ return this;
+ }
+
+ public FileParameter mustNotExist() {
+ existanceType = ExistanceType.MUST_NOT_EXIST;
+ return this;
+ }
+
+ @Override
+ public FileParameter setMandatory() {
+ mandatory = true;
+ return this;
+ }
+
+ @Override
+ public boolean validate(final java.lang.String value) {
+ final File file = new File(value);
+
+ if (existanceType == ExistanceType.MUST_EXIST) {
+ if (file.exists() && file.isFile())
+ return true;
+ return false;
+ }
+
+ if (existanceType == ExistanceType.MUST_NOT_EXIST) {
+ if (file.exists())
+ return false;
+ return true;
+ }
+
+ if (existanceType == ExistanceType.DOES_NOT_MATTER) {
+ if (file.exists())
+ if (file.isDirectory())
+ return false;
+
+ return true;
+ }
+
+ return false;
+ }
+
+}
--- /dev/null
+package eu.svjatoslav.commons.commandline.parameterparser.parameter;
+
+import eu.svjatoslav.commons.commandline.parameterparser.ArgumentCount;
+import eu.svjatoslav.commons.commandline.parameterparser.Parameter;
+import eu.svjatoslav.commons.commandline.parameterparser.type.IntegerArgument;
+
+public class IntegerParameter extends Parameter {
+
+ public IntegerParameter(final String description) {
+ super(new IntegerArgument(), description, ArgumentCount.SINGLE);
+ }
+
+ @Override
+ public IntegerParameter addAliases(final String... aliasArray) {
+ super.addAliases(aliasArray);
+ return this;
+ }
+
+ public int getValue() {
+ if (arguments.size() != 1)
+ throw new RuntimeException("Parameter " + description
+ + " shall have exactly 1 argument.");
+ return Integer.parseInt(arguments.get(0));
+ }
+
+ @Override
+ public IntegerParameter setMandatory() {
+ mandatory = true;
+ return this;
+ }
+}
return arguments.get(0);
}
+
+ @Override
+ public StringParameter setMandatory() {
+ mandatory = true;
+ return this;
+ }
}
+++ /dev/null
-/*
- * Svjatoslav Commons - shared library of common functionality.
- * Copyright ©2012-2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 2 of the GNU General Public License
- * as published by the Free Software Foundation.
- */
-
-package eu.svjatoslav.commons.commandline.parameterparser.type;
-
-import java.io.File;
-
-import eu.svjatoslav.commons.commandline.parameterparser.Argument;
-
-public class ExistingDirectory implements Argument {
-
- @Override
- public java.lang.String describeFormat() {
- return "existing directory";
- }
-
- @Override
- public boolean validate(final java.lang.String value) {
- final File file2 = new File(value);
- if (file2.exists() && file2.isDirectory())
- return true;
- return false;
- }
-
-}
+++ /dev/null
-/*
- * Svjatoslav Commons - shared library of common functionality.
- * Copyright ©2012-2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 2 of the GNU General Public License
- * as published by the Free Software Foundation.
- */
-
-package eu.svjatoslav.commons.commandline.parameterparser.type;
-
-import java.io.File;
-
-import eu.svjatoslav.commons.commandline.parameterparser.Argument;
-
-public class ExistingFile implements Argument {
-
- @Override
- public java.lang.String describeFormat() {
- return "existing file";
- }
-
- @Override
- public boolean validate(final java.lang.String value) {
- final File file = new File(value);
- if (file.exists() && file.isFile())
- return true;
- return false;
- }
-
-}
+++ /dev/null
-/*
- * Svjatoslav Commons - shared library of common functionality.
- * Copyright ©2012-2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 2 of the GNU General Public License
- * as published by the Free Software Foundation.
- */
-
-package eu.svjatoslav.commons.commandline.parameterparser.type;
-
-import java.io.File;
-
-import eu.svjatoslav.commons.commandline.parameterparser.Argument;
-
-public class NonExistingDirectory implements Argument {
-
- @Override
- public java.lang.String describeFormat() {
- return "non existing directory";
- }
-
- @Override
- public boolean validate(final java.lang.String value) {
- final File file4 = new File(value);
- if (!file4.exists())
- return true;
- return false;
- }
-
-}
+++ /dev/null
-/*
- * Svjatoslav Commons - shared library of common functionality.
- * Copyright ©2012-2013, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- *
- * This program is free software; you can redistribute it and/or
- * modify it under the terms of version 2 of the GNU General Public License
- * as published by the Free Software Foundation.
- */
-
-package eu.svjatoslav.commons.commandline.parameterparser.type;
-
-import java.io.File;
-
-import eu.svjatoslav.commons.commandline.parameterparser.Argument;
-
-public class NonExistingFile implements Argument {
-
- @Override
- public java.lang.String describeFormat() {
- return "non existing file";
- }
-
- @Override
- public boolean validate(final java.lang.String value) {
- final File file3 = new File(value);
- if (!file3.exists())
- return true;
- return false;
- }
-
-}
public void testParse() {
// define allowed parameters
- final StringParameter helpParameter = parser.createStringParameter(
- "Show help screen").addAliases("--help", "-h");
+ final StringParameter helpParameter = parser
+ .createStringParameter("Show help screen")
+ .addAliases("--help", "-h").setMandatory();
final StringParameter compileParameter = parser.createStringParameter(
"Compile code").addAliases("--compile", "-c");