Class ArgumentedTool<E extends Enum<E> & ArgumentEnum>

java.lang.Object
tools.ToolClasses.ArgumentedTool<E>
Direct Known Subclasses:
PathplannerSearch

public abstract class ArgumentedTool<E extends Enum<E> & ArgumentEnum> extends Object

ArgumentedTool

A class that represents a tool that takes in arguments.

Using gradle tasks with arguments, it automatically parses the arguments based on an enum that the user defines and passes in.

The enum must implement the ArgumentEnum interface, which must specify a type via ArgumentEnum.getArgumentType(), default value (can be null) via ArgumentEnum.getDefaultValue(), and description for each argument via ArgumentEnum.getDescription(). The order of the arguments is determined by the order of the enum constants.

When parsing, it does the following:

  1. Splits the raw arguments by spaces, unless they are within quotes. (e.g. -Pargs="arg1 'arg 2' arg3" => [arg1, 'arg 2', arg3])
  2. Removes the surrounding quotes from the split arguments (e.g. [arg1, 'arg 2', arg3] => [arg1, arg 2, arg3])
  3. Iterates through the split arguments and attempts to parse them into the type specified by the enum constant ArgumentEnum.getArgumentType().
    • If the type is a string, then it will stay as is.
    • If the type is an integer, then it will attempt to parse it into an integer.
    • If the type is a double, then it will attempt to parse it into a double.
    • If the type is a boolean, then it will attempt to parse it into a boolean.
    • If the type is an enum, then it will attempt to parse it into the enum by matching the name of the enum constant (case-insensitive).
  4. If the argument is missing or fails to parse, then it will use the default value specified by the enum. If there is no default value and the argument fails to parse, then it will throw an exception.
  5. Extra arguments that are specified, greater than the length of the enum constant, will be ignored.

The constructor takes in an enum class that defines the arguments. To use the, the execute(Map) method must be implemented. This method has the parsed arguments passed in as a map, where the keys are the from the argument enum constants, and the values are the parsed arguments from the raw string passed into the run(String) method.

The run(String) method is the main entry point for the tool. This will automatically parse the raw string arguments and call the execute(Map) method with the parsed arguments.