From f05caf52a82cdd174ded6bc6d8042200221b18d6 Mon Sep 17 00:00:00 2001 From: Svjatoslav Agejenko Date: Sun, 27 Oct 2013 17:26:43 +0200 Subject: [PATCH] more usable commandline parser API --- .../parameterparser/Parameter.java | 9 +-- .../commandline/parameterparser/Parser.java | 21 +++++ .../parameter/DirectoryParameter.java | 81 +++++++++++++++++++ .../parameter/ExistanceType.java | 14 ++++ .../parameter/FileParameter.java | 81 +++++++++++++++++++ .../parameter/IntegerParameter.java | 31 +++++++ .../parameter/StringParameter.java | 6 ++ .../type/ExistingDirectory.java | 31 ------- .../parameterparser/type/ExistingFile.java | 31 ------- .../type/NonExistingDirectory.java | 31 ------- .../parameterparser/type/NonExistingFile.java | 31 ------- .../parameterparser/ParserTest.java | 5 +- 12 files changed, 238 insertions(+), 134 deletions(-) create mode 100644 src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/DirectoryParameter.java create mode 100644 src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/ExistanceType.java create mode 100644 src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/FileParameter.java create mode 100644 src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/IntegerParameter.java delete mode 100755 src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/ExistingDirectory.java delete mode 100755 src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/ExistingFile.java delete mode 100755 src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/NonExistingDirectory.java delete mode 100755 src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/NonExistingFile.java 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 831806d..22cc927 100755 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parameter.java +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parameter.java @@ -18,7 +18,7 @@ public class Parameter { /** * Indicates that at least one argument is mandatory for this parameter. */ - private boolean mandatory; + protected boolean mandatory; private final Argument argumentType; @@ -124,13 +124,6 @@ public class Parameter { 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 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 4ab529d..075ca11 100755 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parser.java +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/Parser.java @@ -11,6 +11,9 @@ package eu.svjatoslav.commons.commandline.parameterparser; 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; /** @@ -26,6 +29,24 @@ public class Parser { 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); diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/DirectoryParameter.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/DirectoryParameter.java new file mode 100644 index 0000000..8607234 --- /dev/null +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/DirectoryParameter.java @@ -0,0 +1,81 @@ +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; + } + +} diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/ExistanceType.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/ExistanceType.java new file mode 100644 index 0000000..3b150aa --- /dev/null +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/ExistanceType.java @@ -0,0 +1,14 @@ +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; + } + +} diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/FileParameter.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/FileParameter.java new file mode 100644 index 0000000..7a48e2c --- /dev/null +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/FileParameter.java @@ -0,0 +1,81 @@ +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; + } + +} diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/IntegerParameter.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/IntegerParameter.java new file mode 100644 index 0000000..7af305b --- /dev/null +++ b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/parameter/IntegerParameter.java @@ -0,0 +1,31 @@ +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; + } +} 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 e04b0f3..a862cbb 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 @@ -24,4 +24,10 @@ public class StringParameter extends Parameter { return arguments.get(0); } + + @Override + public StringParameter setMandatory() { + mandatory = true; + return this; + } } diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/ExistingDirectory.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/ExistingDirectory.java deleted file mode 100755 index 2e66255..0000000 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/ExistingDirectory.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * 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; - } - -} diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/ExistingFile.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/ExistingFile.java deleted file mode 100755 index d2ce6f4..0000000 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/ExistingFile.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * 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; - } - -} diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/NonExistingDirectory.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/NonExistingDirectory.java deleted file mode 100755 index d8a02bb..0000000 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/NonExistingDirectory.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * 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; - } - -} diff --git a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/NonExistingFile.java b/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/NonExistingFile.java deleted file mode 100755 index f3e441e..0000000 --- a/src/main/java/eu/svjatoslav/commons/commandline/parameterparser/type/NonExistingFile.java +++ /dev/null @@ -1,31 +0,0 @@ -/* - * 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; - } - -} diff --git a/src/test/java/eu/svjatoslav/commons/commandline/parameterparser/ParserTest.java b/src/test/java/eu/svjatoslav/commons/commandline/parameterparser/ParserTest.java index e8d1078..c61533e 100755 --- a/src/test/java/eu/svjatoslav/commons/commandline/parameterparser/ParserTest.java +++ b/src/test/java/eu/svjatoslav/commons/commandline/parameterparser/ParserTest.java @@ -31,8 +31,9 @@ public class ParserTest { 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"); -- 2.20.1