diff --git a/README.md b/README.md index e7c12cd..ed2b9ca 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -[![GitHub](https://img.shields.io/badge/GitHub-Java__ArgsParser-blue?logo=github)](https://github.com/AbUndMax/Java_ArgsParser) +[![GitHub](https://img.shields.io/badge/GitHub-Java__ArgsParser-a7752f?logo=github)](https://github.com/AbUndMax/Java_ArgsParser) +[![Java](https://img.shields.io/badge/Java-11+-a7752f)](https://openjdk.org/projects/jdk/11/) [![License](https://img.shields.io/badge/License-CC_BY--NC_4.0-blue)](https://github.com/AbUndMax/Java_ArgsParser/blob/main/LICENSE.md) -[![Java](https://img.shields.io/badge/Java-11+-b07219)](https://openjdk.org/projects/jdk/11/) [![Badge](https://img.shields.io/github/v/release/AbUndMax/Java_ArgsParser?color=brightgreen)](https://github.com/AbUndMax/Java_ArgsParser/releases/latest) # ArgsParser Library diff --git a/src/ArgsParser/ArgsParser.java b/src/ArgsParser/ArgsParser.java index 5cfbc04..e353120 100644 --- a/src/ArgsParser/ArgsParser.java +++ b/src/ArgsParser/ArgsParser.java @@ -8,6 +8,8 @@ */ import ArgsParser.ArgsExceptions.*; + +import java.nio.file.Path; import java.util.*; import java.util.stream.Collectors; @@ -167,13 +169,12 @@ private String makeFlag(String fullFlag, boolean isShortName) { // String Parameter constructors - /** * Adds a new parameter that will be checked in args and assigned to the Parameter instance * @param fullFlag name of the parameter (-- will automatically be added) * @param shortFlag short version of the parameter (- will automatically be added) * @param description description of the parameter, hand empty string "" or null if not needed - * @return the created Parameter instance of type String + * @return the created Parameter instance of type {@link String} * @throws IllegalArgumentException if the flag names are already defined, empty, or reserved (--help/-h) */ public Parameter addMandatoryStringParameter(String fullFlag, String shortFlag, String description) @@ -187,7 +188,7 @@ public Parameter addMandatoryStringParameter(String fullFlag, String sho * @param shortFlag short version of the parameter (- will automatically be added) * @param description description of the parameter, hand empty string "" or null if not needed * @param defaultValue default value of the parameter - * @return the created Parameter instance of type String + * @return the created Parameter instance of type {@link String} * @throws IllegalArgumentException if the flag names are already defined, empty, or reserved (--help/-h) */ public Parameter addDefaultStringParameter(String fullFlag, String shortFlag, @@ -201,7 +202,7 @@ public Parameter addDefaultStringParameter(String fullFlag, String short * @param fullFlag name of the parameter (-- will automatically be added) * @param shortFlag short version of the parameter (- will automatically be added) * @param description description of the parameterhand empty string "" or null if not needed - * @return the created Parameter instance of type String + * @return the created Parameter instance of type {@link String} * @throws IllegalArgumentException if the flag names are already defined, empty, or reserved (--help/-h) */ public Parameter addOptionalStringParameter(String fullFlag, String shortFlag, String description) @@ -216,7 +217,7 @@ public Parameter addOptionalStringParameter(String fullFlag, String shor * @param shortFlag short version of the parameter (- will automatically be added) * @param description description of the parameter, hand empty string "" or null if not needed * @param isMandatory true if parameter is mandatory, false if optional - * @return the created Parameter instance of type String[] + * @return the created Parameter instance of type {@link String[]} * @throws IllegalArgumentException if the flag names are already defined, empty, or reserved (--help/-h) */ public Parameter addStringArrayParameter(String fullFlag, String shortFlag, @@ -234,7 +235,7 @@ public Parameter addStringArrayParameter(String fullFlag, String short * @param shortFlag short version of the parameter (- will automatically be added) * @param description description of the parameter, hand empty string "" or null if not needed * @param defaultValue default value of the parameter - * @return the created Parameter instance of type String[] + * @return the created Parameter instance of type {@link String[]} * @throws IllegalArgumentException if the flag names are already defined, empty, or reserved (--help/-h) */ public Parameter addDefaultStringArrayParameter(String fullFlag, String shortFlag, @@ -248,13 +249,12 @@ public Parameter addDefaultStringArrayParameter(String fullFlag, Strin // Integer Parameter constructors - /** * Adds a new parameter that will be checked in args and assigned to the Parameter instance * @param fullFlag name of the parameter (-- will automatically be added) * @param shortFlag short version of the parameter (- will automatically be added) * @param description description of the parameter, hand empty string "" or null if not needed - * @return the created Parameter instance of type Integer + * @return the created Parameter instance of type {@link Integer[]} * @throws IllegalArgumentException if the flag names are already defined, empty, or reserved (--help/-h) */ public Parameter addMandatoryIntegerParameter(String fullFlag, String shortFlag, String description) @@ -268,7 +268,7 @@ public Parameter addMandatoryIntegerParameter(String fullFlag, String s * @param shortFlag short version of the parameter (- will automatically be added) * @param description description of the parameter, hand empty string "" or null if not needed * @param defaultValue default value of the parameter - * @return the created Parameter instance of type Integer + * @return the created Parameter instance of type {@link Integer[]} * @throws IllegalArgumentException if the flag names are already defined, empty, or reserved (--help/-h) */ public Parameter addDefaultIntegerParameter(String fullFlag, String shortFlag, @@ -282,7 +282,7 @@ public Parameter addDefaultIntegerParameter(String fullFlag, String sho * @param fullFlag name of the parameter (-- will automatically be added) * @param shortFlag short version of the parameter (- will automatically be added) * @param description description of the parameter, hand empty string "" or null if not needed - * @return the created Parameter instance of type Integer + * @return the created Parameter instance of type {@link Integer[]} * @throws IllegalArgumentException if the flag names are already defined, empty, or reserved (--help/-h) */ public Parameter addOptionalIntegerParameter(String fullFlag, String shortFlag, String description) @@ -297,7 +297,7 @@ public Parameter addOptionalIntegerParameter(String fullFlag, String sh * @param shortFlag short version of the parameter (- will automatically be added) * @param description description of the parameter, hand empty string "" or null if not needed * @param isMandatory true if parameter is mandatory, false if optional - * @return the created Parameter instance of type int[] + * @return the created Parameter instance of type {@link Integer[]} * @throws IllegalArgumentException if the flag names are already defined, empty, or reserved (--help/-h) */ public Parameter addIntegerArrayParameter(String fullFlag, String shortFlag, @@ -315,7 +315,7 @@ public Parameter addIntegerArrayParameter(String fullFlag, String sho * @param shortFlag short version of the parameter (- will automatically be added) * @param description description of the parameter, hand empty string "" or null if not needed * @param defaultValue default value of the parameter - * @return the created Parameter instance of type int[] + * @return the created Parameter instance of type {@link Integer[]} * @throws IllegalArgumentException if the flag names are already defined, empty, or reserved (--help/-h) */ public Parameter addDefaultIntegerArrayParameter(String fullFlag, String shortFlag, @@ -329,13 +329,12 @@ public Parameter addDefaultIntegerArrayParameter(String fullFlag, Str // Double Parameter constructors - /** * Adds a new parameter that will be checked in args and assigned to the Parameter instance * @param fullFlag name of the parameter (-- will automatically be added) * @param shortFlag short version of the parameter (- will automatically be added) * @param description description of the parameter, hand empty string "" or null if not needed - * @return the created Parameter instance of type Double + * @return the created Parameter instance of type {@link Double} * @throws IllegalArgumentException if the flag names are already defined, empty, or reserved (--help/-h) */ public Parameter addMandatoryDoubleParameter(String fullFlag, String shortFlag, String description) @@ -349,7 +348,7 @@ public Parameter addMandatoryDoubleParameter(String fullFlag, String sho * @param shortFlag short version of the parameter (- will automatically be added) * @param description description of the parameter, hand empty string "" or null if not needed * @param defaultValue default value of the parameter - * @return the created Parameter instance of type Double + * @return the created Parameter instance of type {@link Double} * @throws IllegalArgumentException if the flag names are already defined, empty, or reserved (--help/-h) */ public Parameter addDefaultDoubleParameter(String fullFlag, String shortFlag, @@ -363,7 +362,7 @@ public Parameter addDefaultDoubleParameter(String fullFlag, String short * @param fullFlag name of the parameter (-- will automatically be added) * @param shortFlag short version of the parameter (- will automatically be added) * @param description description of the parameter, hand empty string "" or null if not needed - * @return the created Parameter instance of type Double + * @return the created Parameter instance of type {@link Double} * @throws IllegalArgumentException if the flag names are already defined, empty, or reserved (--help/-h) */ public Parameter addOptionalDoubleParameter(String fullFlag, String shortFlag, String description) @@ -378,7 +377,7 @@ public Parameter addOptionalDoubleParameter(String fullFlag, String shor * @param shortFlag short version of the parameter (- will automatically be added) * @param description description of the parameter, hand empty string "" or null if not needed * @param isMandatory true if parameter is mandatory, false if optional - * @return the created Parameter instance of type double[] + * @return the created Parameter instance of type {@link Double[]} * @throws IllegalArgumentException if the flag names are already defined, empty, or reserved (--help/-h) */ public Parameter addDoubleArrayParameter(String fullFlag, String shortFlag, @@ -396,7 +395,7 @@ public Parameter addDoubleArrayParameter(String fullFlag, String short * @param shortFlag short version of the parameter (- will automatically be added) * @param description description of the parameter, hand empty string "" or null if not needed * @param defaultValue default value of the parameter - * @return the created Parameter instance of type double[] + * @return the created Parameter instance of type {@link Double[]} * @throws IllegalArgumentException if the flag names are already defined, empty, or reserved (--help/-h) */ public Parameter addDefaultDoubleArrayParameter(String fullFlag, String shortFlag, @@ -410,13 +409,12 @@ public Parameter addDefaultDoubleArrayParameter(String fullFlag, Strin // Boolean Parameter constructors - /** * Adds a new parameter that will be checked in args and assigned to the Parameter instance * @param fullFlag name of the parameter (-- will automatically be added) * @param shortFlag short version of the parameter (- will automatically be added) * @param description description of the parameter, hand empty string "" or null if not needed - * @return the created Parameter instance of type Boolean + * @return the created Parameter instance of type {@link Boolean} * @throws IllegalArgumentException if the flag names are already defined, empty, or reserved (--help/-h) */ public Parameter addMandatoryBooleanParameter(String fullFlag, String shortFlag, String description) @@ -430,7 +428,7 @@ public Parameter addMandatoryBooleanParameter(String fullFlag, String s * @param shortFlag short version of the parameter (- will automatically be added) * @param description description of the parameter, hand empty string "" or null if not needed * @param defaultValue default value of the parameter - * @return the created Parameter instance of type Boolean + * @return the created Parameter instance of type {@link Boolean} * @throws IllegalArgumentException if the flag names are already defined, empty, or reserved (--help/-h) */ public Parameter addDefaultBooleanParameter(String fullFlag, String shortFlag, @@ -444,7 +442,7 @@ public Parameter addDefaultBooleanParameter(String fullFlag, String sho * @param fullFlag name of the parameter (-- will automatically be added) * @param shortFlag short version of the parameter (- will automatically be added) * @param description description of the parameter, hand empty string "" or null if not needed - * @return the created Parameter instance of type Boolean + * @return the created Parameter instance of type {@link Boolean} * @throws IllegalArgumentException if the flag names are already defined, empty, or reserved (--help/-h) */ public Parameter addOptionalBooleanParameter(String fullFlag, String shortFlag, String description) @@ -459,7 +457,7 @@ public Parameter addOptionalBooleanParameter(String fullFlag, String sh * @param shortFlag short version of the parameter (- will automatically be added) * @param description description of the parameter, hand empty string "" or null if not needed * @param isMandatory true if parameter is mandatory, false if optional - * @return the created Parameter instance of type boolean[] + * @return the created Parameter instance of type {@link Boolean[]} * @throws IllegalArgumentException if the flag names are already defined, empty, or reserved (--help/-h) */ public Parameter addBooleanArrayParameter(String fullFlag, String shortFlag, @@ -477,7 +475,7 @@ public Parameter addBooleanArrayParameter(String fullFlag, String sho * @param shortFlag short version of the parameter (- will automatically be added) * @param description description of the parameter, hand empty string "" or null if not needed * @param defaultValue default value of the parameter - * @return the created Parameter instance of type boolean[] + * @return the created Parameter instance of type {@link Boolean[]} * @throws IllegalArgumentException if the flag names are already defined, empty, or reserved (--help/-h) */ public Parameter addDefaultBooleanArrayParameter(String fullFlag, String shortFlag, @@ -491,13 +489,12 @@ public Parameter addDefaultBooleanArrayParameter(String fullFlag, Str // Character Parameter constructors - /** * Adds a new parameter that will be checked in args and assigned to the Parameter instance * @param fullFlag name of the parameter (-- will automatically be added) * @param shortFlag short version of the parameter (- will automatically be added) * @param description description of the parameter, hand empty string "" or null if not needed - * @return the created Parameter instance of type Character + * @return the created Parameter instance of type {@link Character} * @throws IllegalArgumentException if the flag names are already defined, empty, or reserved (--help/-h) */ public Parameter addMandatoryCharacterParameter(String fullFlag, String shortFlag, @@ -511,7 +508,7 @@ public Parameter addMandatoryCharacterParameter(String fullFlag, Stri * @param shortFlag short version of the parameter (- will automatically be added) * @param description description of the parameter, hand empty string "" or null if not needed * @param defaultValue default value of the parameter - * @return the created Parameter instance of type Character + * @return the created Parameter instance of type {@link Character} * @throws IllegalArgumentException if the flag names are already defined, empty, or reserved (--help/-h) */ public Parameter addDefaultCharacterParameter(String fullFlag, String shortFlag, @@ -525,7 +522,7 @@ public Parameter addDefaultCharacterParameter(String fullFlag, String * @param fullFlag name of the parameter (-- will automatically be added) * @param shortFlag short version of the parameter (- will automatically be added) * @param description description of the parameter, hand empty string "" or null if not needed - * @return the created Parameter instance of type Character + * @return the created Parameter instance of type {@link Character} * @throws IllegalArgumentException if the flag names are already defined, empty, or reserved (--help/-h) */ public Parameter addOptionalCharacterParameter(String fullFlag, String shortFlag, String description) @@ -540,7 +537,7 @@ public Parameter addOptionalCharacterParameter(String fullFlag, Strin * @param shortFlag short version of the parameter (- will automatically be added) * @param description description of the parameter, hand empty string "" or null if not needed * @param isMandatory true if parameter is mandatory, false if optional - * @return the created Parameter instance of type char[] + * @return the created Parameter instance of type {@link Character[]} * @throws IllegalArgumentException if the flag names are already defined, empty, or reserved (--help/-h) */ public Parameter addCharacterArrayParameter(String fullFlag, String shortFlag, @@ -558,7 +555,7 @@ public Parameter addCharacterArrayParameter(String fullFlag, String * @param shortFlag short version of the parameter (- will automatically be added) * @param description description of the parameter, hand empty string "" or null if not needed * @param defaultValue default value of the parameter - * @return the created Parameter instance of type char[] + * @return the created Parameter instance of type {@link Character[]} * @throws IllegalArgumentException if the flag names are already defined, empty, or reserved (--help/-h) */ public Parameter addDefaultCharacterArrayParameter(String fullFlag, String shortFlag, @@ -570,16 +567,55 @@ public Parameter addDefaultCharacterArrayParameter(String fullFlag, } - // Command + // Path Parameter + + /** + * Adds a new parameter that will be checked in args and assigned to the Parameter instance + * @param fullFlag name of the parameter (-- will automatically be added) + * @param shortFlag short version of the parameter (- will automatically be added) + * @param description description of the parameter, hand empty string "" or null if not needed + * @return the created Parameter instance of type {@link Path} + * @throws IllegalArgumentException if the flag names are already defined, empty, or reserved (--help/-h) + */ + public Parameter addMandatoryPathParameter(String fullFlag, String shortFlag, String description) { + return createParameter(fullFlag, shortFlag, description, Path.class, true, null); + } + + /** + * Adds a new parameter that will be checked in args and assigned to the Parameter instance + * @param fullFlag name of the parameter (-- will automatically be added) + * @param shortFlag short version of the parameter (- will automatically be added) + * @param description description of the parameter, hand empty string "" or null if not needed + * @param defaultValue a default path object + * @return the created Parameter instance of type {@link Path} + * @throws IllegalArgumentException if the flag names are already defined, empty, or reserved (--help/-h) + */ + public Parameter addDefaultPathParameter(String fullFlag, String shortFlag, String description, Path defaultValue) { + return createParameter(fullFlag, shortFlag, description, Path.class, false, defaultValue); + } + + /** + * Adds a new parameter that will be checked in args and assigned to the Parameter instance + * @param fullFlag name of the parameter (-- will automatically be added) + * @param shortFlag short version of the parameter (- will automatically be added) + * @param description description of the parameter, hand empty string "" or null if not needed + * @return the created Parameter instance of type {@link Path} + * @throws IllegalArgumentException if the flag names are already defined, empty, or reserved (--help/-h) + */ + public Parameter addOptionalPathParameter(String fullFlag, String shortFlag, String description) { + return createParameter(fullFlag, shortFlag, description, Path.class, false, null); + } + // Command + /** * Adds a new command with its full name, short name, and description to the existing command set. * * @param fullCommandName The full name of the command, used as the primary identifier. * @param shortCommandName The abbreviated name of the command, used as a shorthand identifier. * @param description A brief description of what the command does. - * @return The newly created and added Command object. + * @return The newly created and added {@link Command} object. * @throws IllegalArgumentException If the fullCommandName or shortCommandName are already defined, empty, or reserved (--help/-h) */ public Command addCommand(String fullCommandName, String shortCommandName, String description) { diff --git a/src/ArgsParser/Parameter.java b/src/ArgsParser/Parameter.java index 7457a6c..d50ee68 100644 --- a/src/ArgsParser/Parameter.java +++ b/src/ArgsParser/Parameter.java @@ -10,6 +10,7 @@ import ArgsParser.ArgsExceptions.InvalidArgTypeArgsException; import java.lang.reflect.Array; +import java.nio.file.Path; import java.util.*; import java.util.function.Function; @@ -197,7 +198,8 @@ private enum ArgumentConverter { DOUBLE_ARRAY(Double[].class, Double::parseDouble), DOUBLE(Double.class, Double::parseDouble), CHARACTER_ARRAY(Character[].class, s -> s.charAt(0)), - CHARACTER(Character.class, s -> s.charAt(0)); + CHARACTER(Character.class, s -> s.charAt(0)), + PATH(Path.class, Path::of); private final Class typeClass; private final Function mapper; diff --git a/src/test/TestAddParameterMethods.java b/src/test/TestAddParameterMethods.java index ae9fb6f..ed35f46 100644 --- a/src/test/TestAddParameterMethods.java +++ b/src/test/TestAddParameterMethods.java @@ -4,6 +4,8 @@ import ArgsParser.ArgsExceptions.ToggleArgsException; import org.junit.jupiter.api.Test; +import java.nio.file.Path; + import static org.junit.jupiter.api.Assertions.assertEquals; public class TestAddParameterMethods { @@ -378,6 +380,37 @@ public void testAddDefaultCharacterArrayParameterWithArgument() { +// Path parameters + + @Test + public void testAddMandatoryPathParameter() { + String[] args = {"--Path", "/Users/user/Source/Code"}; + ArgsParser parser = new ArgsParser(); + Parameter path = parser.addMandatoryPathParameter("Path", "pathA", "desc"); + parser.parse(args); + assertEquals("/Users/user/Source/Code", path.getArgument().toString()); + } + + @Test + public void testAddDefaultPathParameterWithoutArgument() { + String[] args = new String[0]; + ArgsParser parser = new ArgsParser(); + Parameter path = parser.addDefaultPathParameter("Path", "pathA", "desc", Path.of("home/")); + parser.parse(args); + assertEquals(Path.of("home/"), path.getArgument()); + } + + @Test + public void testAddOptionalPathParameter() { + String[] args = {"--Path", "/Users/user/Source/Code"}; + ArgsParser parser = new ArgsParser(); + Parameter path = parser.addOptionalPathParameter("Path", "pathA", "desc"); + parser.parse(args); + assertEquals("/Users/user/Source/Code", path.getArgument().toString()); + } + + + // Commands @Test