Skip to content

Commit

Permalink
Merge pull request #2 from hlafaille/feat/1-add-command-support-to-ba…
Browse files Browse the repository at this point in the history
…se-level

#1 Add support for base level Command(s)
  • Loading branch information
hlafaille authored Nov 22, 2023
2 parents 4eb690b + 818cf7c commit 767e7d3
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 11 deletions.
25 changes: 24 additions & 1 deletion src/main/java/xyz/hlafaille/eap/EspressoArgumentParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class EspressoArgumentParser {
@Getter
private final String applicationDescription;
private final List<ExceptionHandler> exceptionHandlerList = new ArrayList<>();

private final List<Command> commandList = new ArrayList<>();
private final List<CommandContainer> commandContainerList = new ArrayList<>();

/**
Expand Down Expand Up @@ -74,6 +74,20 @@ public void addCommandContainer(CommandContainer commandContainer) throws EapDup
commandContainerList.add(commandContainer);
}

/**
* Add a Command
*
* @param command Command instance
*/
public void addCommand(Command command) throws EapDuplicateCommandException {
for (Command iterCommand : commandList) {
if (iterCommand.getName().equals(command.getName())) {
throw new EapDuplicateCommandException();
}
}
commandList.add(command);
}

/**
* Intended to be called recursively, parseInCommandContainer will work its way down the args until it discovers a
* valid Command under a CommandContainer. If a Command was found, it will call .execute() on the Command.
Expand Down Expand Up @@ -129,6 +143,14 @@ public void parse(String[] arguments) throws EapMissingSubcommandException, EapC
// establish the name of a command container to parse through (should be first argument in the chain)
String baseCommandContainerName = arguments[0];

// iterate over the top level commands, check if we have any that match
for (Command command : commandList) {
if (command.getName().equals(baseCommandContainerName)) {
command.preExecute(Arrays.copyOfRange(arguments, 1, arguments.length));
return;
}
}

// iterate over the command containers
Boolean commandContainerFound = false;
for (CommandContainer commandContainer : commandContainerList) {
Expand All @@ -142,6 +164,7 @@ public void parse(String[] arguments) throws EapMissingSubcommandException, EapC
if (!commandContainerFound) {
throw new EapSubcommandNotFoundException(baseCommandContainerName);
}

} catch (Exception e) {
// iterate over exception handlers, find a match
for (ExceptionHandler exceptionHandler : exceptionHandlerList) {
Expand Down
12 changes: 2 additions & 10 deletions src/main/java/xyz/hlafaille/eap/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
import java.util.List;

public class Main {
public static void main(String[] args) throws EapCommandNotFoundException, EapMissingSubcommandException, EapDuplicateCommandContainerException, EapMalformedCommandModifierException, EapSubcommandNotFoundException, EapCommandModifierNotFoundException, EapCommandNotSpecifiedException {
public static void main(String[] args) throws EapCommandNotFoundException, EapMissingSubcommandException, EapDuplicateCommandContainerException, EapMalformedCommandModifierException, EapSubcommandNotFoundException, EapCommandModifierNotFoundException, EapCommandNotSpecifiedException, EapDuplicateCommandException {
EspressoArgumentParser espressoArgumentParser = new EspressoArgumentParser("EAP Application", "Test");

// build our command
Expand All @@ -16,15 +16,7 @@ public void execute(List<CommandModifier> commandModifiers) {
System.out.println(commandModifiers);
}
};

// add a command modifier
CommandModifier commandModifier = new CommandModifier("test", "test");
command.addCommandModifier(commandModifier);

// build our command container
CommandContainer commandContainer = new CommandContainer("project", "Create, read, update and delete project information");
commandContainer.addCommand(command);
espressoArgumentParser.addCommandContainer(commandContainer);
espressoArgumentParser.addCommand(command);

// entrypoint
espressoArgumentParser.parse(args);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package xyz.hlafaille.eap.buitlin.exceptionhandler;

import xyz.hlafaille.eap.ExceptionHandler;
import xyz.hlafaille.eap.exception.EapDuplicateCommandContainerException;
import xyz.hlafaille.eap.exception.EapDuplicateCommandException;
import xyz.hlafaille.eap.util.ColorLogger;

/**
* Built in exception handler for: xyz.hlafaille.eap.exception.EapDuplicateCommandException
*/
public class EapDuplicateCommandExceptionHandler extends ExceptionHandler<EapDuplicateCommandException> {
public EapDuplicateCommandExceptionHandler() {
super(EapDuplicateCommandException.class);
}

@Override
public void execute(Exception exception) {
ColorLogger.severe(exception.getMessage());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package xyz.hlafaille.eap.exception;

public class EapDuplicateCommandException extends Exception{
public EapDuplicateCommandException() {
super("Duplicate commands are not allowed");
}
}

0 comments on commit 767e7d3

Please sign in to comment.