-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Using Argparse4j for Command Line Arguments Parsing
- Loading branch information
Showing
6 changed files
with
123 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
7 changes: 7 additions & 0 deletions
7
backend/src/main/java/ai/dragon/enumeration/CommandLineExecutionResultType.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
package ai.dragon.enumeration; | ||
|
||
public enum CommandLineExecutionResultType { | ||
EXECUTED, | ||
BYPASS, | ||
ERROR | ||
} |
92 changes: 92 additions & 0 deletions
92
backend/src/main/java/ai/dragon/util/CommandLineRunnerWithArgumentsParser.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package ai.dragon.util; | ||
|
||
import java.util.Arrays; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
|
||
import org.springframework.boot.CommandLineRunner; | ||
|
||
import ai.dragon.enumeration.CommandLineExecutionResultType; | ||
import net.sourceforge.argparse4j.ArgumentParsers; | ||
import net.sourceforge.argparse4j.helper.HelpScreenException; | ||
import net.sourceforge.argparse4j.inf.ArgumentParser; | ||
import net.sourceforge.argparse4j.inf.ArgumentParserException; | ||
|
||
public abstract class CommandLineRunnerWithArgumentsParser implements CommandLineRunner { | ||
public abstract CommandLineExecutionResultType runCommand(String... args) throws Exception; | ||
|
||
private String commandName; | ||
private ArgumentParser parser; | ||
|
||
public void run(String... args) throws Exception { | ||
CommandLineExecutionResultType executionResult = null; | ||
|
||
try { | ||
executionResult = this.runCommand(args); | ||
} catch (ArgumentParserException e) { | ||
if (parser == null) { | ||
throw e; | ||
} | ||
|
||
executionResult = this.handleError(parser, e, args); | ||
} | ||
|
||
switch (executionResult) { | ||
case BYPASS: | ||
return; | ||
case ERROR: | ||
System.exit(1); | ||
case EXECUTED: | ||
System.exit(0); | ||
} | ||
} | ||
|
||
public CommandLineExecutionResultType handleError(ArgumentParser parser, ArgumentParserException parentException, | ||
String... args) { | ||
if (parentException instanceof HelpScreenException) { | ||
return CommandLineExecutionResultType.EXECUTED; | ||
} | ||
|
||
String command = getCommandFromArgs(args); | ||
if (commandName == null || !commandName.equals(command)) { | ||
return CommandLineExecutionResultType.BYPASS; | ||
} | ||
|
||
parser.handleError(parentException); | ||
return CommandLineExecutionResultType.ERROR; | ||
} | ||
|
||
public ArgumentParser parserFor(String command, String description) { | ||
this.commandName = command; | ||
|
||
parser = ArgumentParsers.newFor(command).build() | ||
.defaultHelp(true) | ||
.description("Dump the database to a file."); | ||
parser.addArgument("--command") | ||
.type(String.class) | ||
.required(true) | ||
.choices(command) | ||
.help("Specify the command to launch"); | ||
|
||
return parser; | ||
} | ||
|
||
private String getCommandFromArgs(String... args) { | ||
List<String> argsList = Arrays.asList(args); | ||
Iterator<String> it = argsList.iterator(); | ||
|
||
while (it.hasNext()) { | ||
String arg = it.next(); | ||
if (arg.startsWith("--command")) { | ||
String[] argAndValue = arg.split("="); | ||
if (argAndValue.length > 1) { | ||
return argAndValue[1]; | ||
} else if (it.hasNext()) { | ||
return it.next(); | ||
} | ||
} | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters