Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
51 changes: 24 additions & 27 deletions src/ArgsParser/ArgsParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@
* <li>{@link ArgsParser#addCommand(String, String, String)}</li>
* </ul>
*
* <li>After all parameters are added, the {@link ArgsParser#parse()} method has to be called! (this is mandatory!)</li>
* <li>After all parameters are added, the {@link ArgsParser#parse(String[] args)} method has to be called! (this is mandatory!)</li>
* <li>Then the arguments can be accessed by using {@link Parameter#getArgument()} on the specific Parameter variable
* which will return the parsed argument of that parameter as the specified type or for commands by using the
* {@link Command#isProvided()} at the respective command instance to check rather the command was provided or not</li>
Expand All @@ -54,7 +54,6 @@
*/
public class ArgsParser {

private final String[] args;
private final Map<String, Parameter<?>> parameterMap = new HashMap<>();
private final Map<String, Command> commandMap = new HashMap<>();
private final Set<Parameter<?>> mandatoryParameters = new HashSet<>();
Expand All @@ -64,17 +63,12 @@ public class ArgsParser {
protected boolean parseArgsWasCalled = false;
private int longestFlagSize = 0;
private int longestShortFlag = 0;
private final int argsLength;

/**
* Constructor
* @param args arguments given to the program
* @throws IllegalArgumentException if args is null
*/
public ArgsParser(String[] args) throws IllegalArgumentException {
if (args == null) throw new IllegalArgumentException("Args cannot be null!");
this.args = args;
this.argsLength = args.length;
public ArgsParser() throws IllegalArgumentException {
}

/**
Expand Down Expand Up @@ -629,11 +623,12 @@ public Command addCommand(String fullCommandName, String shortCommandName, Strin
*
* If an ArgsException is thrown, its message is printed to the
* standard output and the application exits with a status code of 1.
* @throws IllegalArgumentException if args is null
*/
public void parse() {
public void parse(String[] args) throws IllegalArgumentException {

try {
parseUnchecked();
parseUnchecked(args);

} catch (CalledForHelpNotification help) {
System.out.println(help.getMessage());
Expand Down Expand Up @@ -662,20 +657,22 @@ public void parse() {
* @throws IllegalStateException if the .parse() method is called more than once.
* @throws FlagAlreadyProvidedArgsException if a flag is provided more than once.
* @throws HelpAtWrongPositionArgsException if the help argument is positioned incorrectly.
* @throws IllegalArgumentException if args is null
*/
public void parseUnchecked() throws NoArgumentsProvidedArgsException, UnknownFlagArgsException,
public void parseUnchecked(String[] args) throws NoArgumentsProvidedArgsException, UnknownFlagArgsException,
TooManyArgumentsArgsException, MissingArgArgsException, MandatoryArgNotProvidedArgsException,
CalledForHelpNotification, InvalidArgTypeArgsException, IllegalStateException,
FlagAlreadyProvidedArgsException, HelpAtWrongPositionArgsException {
FlagAlreadyProvidedArgsException, HelpAtWrongPositionArgsException, IllegalArgumentException {

if (args == null) throw new IllegalArgumentException("Args cannot be null!");
if(parseArgsWasCalled) throw new IllegalStateException(".parse() was already called!");

parseArgsWasCalled = true;

checkIfAnyArgumentsProvided();
if (argsLength > 0) {
checkForHelpCall();
Set<Parameter<?>> givenParameters = parseArguments();
checkIfAnyArgumentsProvided(args);
if (args.length > 0) {
checkForHelpCall(args);
Set<Parameter<?>> givenParameters = parseArguments(args);
checkMandatoryArguments(givenParameters);
}
}
Expand All @@ -686,8 +683,8 @@ public void parseUnchecked() throws NoArgumentsProvidedArgsException, UnknownFla
* to run without any arguments as long as no mandatory parameters were defined on this ArgsParser.
* @throws NoArgumentsProvidedArgsException if no arguments were provided in args
*/
private void checkIfAnyArgumentsProvided() throws NoArgumentsProvidedArgsException {
if (argsLength == 0 & !mandatoryParameters.isEmpty()) {
private void checkIfAnyArgumentsProvided(String[] args) throws NoArgumentsProvidedArgsException {
if (args.length == 0 & !mandatoryParameters.isEmpty()) {
throw new NoArgumentsProvidedArgsException();
}
}
Expand All @@ -698,9 +695,9 @@ private void checkIfAnyArgumentsProvided() throws NoArgumentsProvidedArgsExcepti
* @throws UnknownFlagArgsException if an unknown flag was provided in args
* @throws CalledForHelpNotification if --help or -h was called
*/
private void checkForHelpCall() throws UnknownFlagArgsException, CalledForHelpNotification {
boolean oneArgProvided = argsLength == 1;
boolean twoArgsProvided = argsLength == 2;
private void checkForHelpCall(String[] args) throws UnknownFlagArgsException, CalledForHelpNotification {
boolean oneArgProvided = args.length == 1;
boolean twoArgsProvided = args.length == 2;
boolean firstArgumentIsParameter = parameterMap.get(args[0]) != null;
boolean firstArgumentIsCommand = commandMap.get(args[0]) != null;

Expand Down Expand Up @@ -741,21 +738,21 @@ private void checkForHelpCall() throws UnknownFlagArgsException, CalledForHelpNo
* @throws FlagAlreadyProvidedArgsException If a flag is provided more than once.
* @throws HelpAtWrongPositionArgsException If the help flag is not in the correct position.
*/
private Set<Parameter<?>> parseArguments() throws UnknownFlagArgsException, TooManyArgumentsArgsException,
private Set<Parameter<?>> parseArguments(String[] args) throws UnknownFlagArgsException, TooManyArgumentsArgsException,
MissingArgArgsException, InvalidArgTypeArgsException, FlagAlreadyProvidedArgsException,
HelpAtWrongPositionArgsException {

Set<Parameter<?>> givenParameters = new HashSet<>();

//check if the first argument provided is actually a flag or command
if (argsLength > 0 && parameterMap.get(args[0]) == null && commandMap.get(args[0]) == null &&
if (args.length > 0 && parameterMap.get(args[0]) == null && commandMap.get(args[0]) == null &&
!(args[0].equals("--help") || args[0].equals("-h"))) {
throw new UnknownFlagArgsException(args[0], parameterMap.keySet(), commandMap.keySet(), true);
}

Parameter<?> currentParameter = null;
boolean longFlagUsed = false;
for (int i = 0; i < argsLength; i++) {
for (int i = 0; i < args.length; i++) {

boolean currentPositionIsFlag = args[i].startsWith("-");
if (currentPositionIsFlag) {
Expand All @@ -764,14 +761,14 @@ private Set<Parameter<?>> parseArguments() throws UnknownFlagArgsException, TooM
}
boolean currentPositionIsCommand = commandMap.get(args[i]) != null;
boolean flagExists = parameterMap.get(args[i]) != null;
boolean isLastEntry = i == argsLength - 1;
boolean isLastEntry = i == args.length - 1;
boolean currentParameterNotNull = currentParameter != null;
boolean argumentSet = currentParameterNotNull && currentParameter.hasArgument();
boolean lastPositionWasFlag = i >= 1 && args[i - 1].startsWith("-");
boolean flagAlreadyProvided = false;
if (flagExists) flagAlreadyProvided = givenParameters.contains(currentParameter);
boolean isHelpCall = ("--help".equals(args[i]) || "-h".equals(args[i]));
boolean helpCallInWrongPosition = isHelpCall && (i > 1 || (i == 0 && argsLength == 2));
boolean helpCallInWrongPosition = isHelpCall && (i > 1 || (i == 0 && args.length == 2));

if (helpCallInWrongPosition) {
throw new HelpAtWrongPositionArgsException();
Expand Down Expand Up @@ -803,7 +800,7 @@ private Set<Parameter<?>> parseArguments() throws UnknownFlagArgsException, TooM
if (isArrayParam) { // we "collect" all following arguments after an array parameter in a StringBuilder
StringBuilder arguments = new StringBuilder();
arguments.append(args[i]).append("==="); // every entry in the array gets seperated by ===
while(i + 1 < argsLength && !args[i + 1].startsWith("-") && !commandMap.containsKey(args[i + 1])) { // loop through all arguments
while(i + 1 < args.length && !args[i + 1].startsWith("-") && !commandMap.containsKey(args[i + 1])) { // loop through all arguments
arguments.append(args[++i]).append("===");
}
currentParameter.setArgument(arguments.toString());
Expand Down
2 changes: 1 addition & 1 deletion src/ArgsParser/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ protected void setCommand() {
* Checks if the command has been provided after the arguments were parsed.
*
* @return true if the command is provided, false otherwise.
* @throws IllegalArgumentException if the {@link ArgsParser#parse()} method was not called before checking the command.
* @throws IllegalArgumentException if the {@link ArgsParser#parse(String[] args)} method was not called before checking the command.
*/
public boolean isProvided() throws IllegalArgumentException {
if (!argsParser.parseArgsWasCalled()) throw new IllegalStateException("parse() was not called before trying to check the command!");
Expand Down
2 changes: 1 addition & 1 deletion src/ArgsParser/Parameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ protected boolean hasArgument() {
/**
* getter method for the argument attribute
* @return argument as String
* @throws IllegalStateException if {@link ArgsParser#parse()} was not called before trying to access this argument
* @throws IllegalStateException if {@link ArgsParser#parse(String[] args)} was not called before trying to access this argument
*/
public T getArgument() throws IllegalStateException {
if (!argsParser.parseArgsWasCalled()) throw new IllegalStateException("parse() was not called before trying to access the argument!");
Expand Down
Loading