Class Argument<T>
java.lang.Object
com.clumd.projects.java_common_utils.arg_parser.Argument<T>
- Type Parameters:
T- The type this Argument's value represents be post parsing.
This Class represents all the configuration required to parse a single Command Line Argument into a usable output
The Ideal implementation pattern for this is as follows:
public class YourProgramsArgParser {
private static final List<Argument<?>> arguments;
private static final Argument<Void> helpFlag, otherHelpStyleFlags;
private static final Argument<String> someArgumentWithATypedValue;
static {
helpFlag = Argument.<Void>builder()
.uniqueId("help flag")
.shouldShortCircuit(true)
...
.build();
otherHelpStyleFlags = Argument.<Void>builder()
.uniqueId("other flag")
...
.build();
someArgumentWithATypedValue = Argument.<String>builder()
.uniqueId("typed arg")
...
.build();
arguments = List.of(helpFlag,
otherHelpStyleFlags,
someArgumentWithATypedValue);
argParser = new JavaArgParser();
argParser.setBoilerplate(...);
}
public static ObjectToActionArgsWith getArgs(String[] args) throws UnwrappableThrowable {
ObjectToActionArgsWith objectToActionArgsWith = new ObjectToActionArgsWith;
Map<String, Argument<Object>> parsedArguments;
parsedArguments = argParser.parseFromCLI(arguments, args);
for (String argID : parsedArguments.keySet()) {
switch (argID) {
case "help flag" -> {
System.out.println(argParser.getBoilerplate(arguments));
throw new UnwrappableRuntimeException("Instant quit.");
}
case "other flag" -> otherHelpStyleFlags.getArgumentResult();
case "typed arg" -> someArgumentWithATypedValue.getArgumentResult();
default ->
throw new UnwrappableException("Argument {" + argID + "} configured, but not handled when present.");
}
}
return objectToActionArgsWith;
}
}
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionstatic classOverridden Builder for part of the Lombok process. -
Method Summary
Modifier and TypeMethodDescriptionfinal voidattemptValueConversion(String value) This method should be called by the implementingCLIArgParserif a value is provided for this Argument.static <T> Argument.ArgumentBuilder<T>builder()protected booleanbooleanIf this argument needs a value, then this should be the fully validated and parsed output of that value.This function is used to convert an argument from the CLI string form, into the object the running process needs.This should be used as the default value, if nothing is provided for the conversion functionUsed to describe the purpose of this Argument and how it should be used.Used to contain all the word alias' to indicate this optionUsed to contain all the single character alias' to indicate this optionUsed to index the options, must be unique and supplied by the user.This function can be provided to run a verification step that the argument's value is acceptable.inthashCode()booleanhasValue()Used to indicate if this argument can be followed by a value, defaults to falsebooleanUsed to track if a default value has been set for this argument to be used if no CLI value is found.booleanUsed to indicate if this argument is mandatory and MUST be provided for the utilising code to function.voidsetArgumentResult(T argumentResult) If this argument needs a value, then this should be the fully validated and parsed output of that value.voidsetDefaultValue(T defaultValue) This should be used as the default value, if nothing is provided for the conversion functionvoidsetDefaultValueSet(boolean defaultValueSet) Used to track if a default value has been set for this argument to be used if no CLI value is found.booleanUsed to tell a parser that if this argument is detected on the CLI at all, it should ALWAYS be returnable to the caller with all present short circuit args.toString()Overridden to make development cognitively easier by removing the Argument config, and only showing the output for itself.final booleanIf a value is possible for this CLI Argument, then this method will be called to verify that the argument provided is acceptable by the calling process.booleanUsed to indicate if the value to this argument can be optional, defaults to false.
-
Method Details
-
attemptValueConversion
This method should be called by the implementingCLIArgParserif a value is provided for this Argument.- Parameters:
value- The CLI Argument's value to be parsed into the desired Java type, or a null if no argument provided
-
validateValue
public final boolean validateValue()If a value is possible for this CLI Argument, then this method will be called to verify that the argument provided is acceptable by the calling process. This works by calling the implementation-providedvalidationFunctionto decide if it is acceptable- Returns:
- True (default if no function was provided), if the value provided meets the acceptance criteria of the
validationFunction, False if it fails validation
-
toString
Overridden to make development cognitively easier by removing the Argument config, and only showing the output for itself. -
builder
-
getUniqueId
Used to index the options, must be unique and supplied by the user. -
getShortOptions
Used to contain all the single character alias' to indicate this option -
getLongOptions
Used to contain all the word alias' to indicate this option -
isMandatory
public boolean isMandatory()Used to indicate if this argument is mandatory and MUST be provided for the utilising code to function. If you set this value to true, you probably don't want to set a default value. -
shouldShortCircuit
public boolean shouldShortCircuit()Used to tell a parser that if this argument is detected on the CLI at all, it should ALWAYS be returnable to the caller with all present short circuit args. Ideally ignoring any other constraints, such asisMandatory= true; -
hasValue
public boolean hasValue()Used to indicate if this argument can be followed by a value, defaults to false -
valueIsOptional
public boolean valueIsOptional()Used to indicate if the value to this argument can be optional, defaults to false. (e.g. value MUST be provided) -
getDescription
Used to describe the purpose of this Argument and how it should be used. -
getValidationFunction
This function can be provided to run a verification step that the argument's value is acceptable. -
getConversionFunction
This function is used to convert an argument from the CLI string form, into the object the running process needs. -
isDefaultValueSet
public boolean isDefaultValueSet()Used to track if a default value has been set for this argument to be used if no CLI value is found. -
getArgumentResult
If this argument needs a value, then this should be the fully validated and parsed output of that value. -
getDefaultValue
This should be used as the default value, if nothing is provided for the conversion function -
setDefaultValueSet
public void setDefaultValueSet(boolean defaultValueSet) Used to track if a default value has been set for this argument to be used if no CLI value is found. -
setArgumentResult
If this argument needs a value, then this should be the fully validated and parsed output of that value. -
setDefaultValue
This should be used as the default value, if nothing is provided for the conversion function -
equals
-
canEqual
-
hashCode
public int hashCode()
-