Skip to content

Commit

Permalink
Format the entire project.
Browse files Browse the repository at this point in the history
This likely breaks all other branches diffs. Those branches will
also need to re-format the code first.
  • Loading branch information
LadyCailin committed Mar 5, 2018
1 parent f45cee0 commit f243a14
Show file tree
Hide file tree
Showing 957 changed files with 68,263 additions and 67,098 deletions.
2,167 changes: 1,071 additions & 1,096 deletions src/main/java/com/laytonsmith/PureUtilities/ArgumentParser.java

Large diffs are not rendered by default.

148 changes: 77 additions & 71 deletions src/main/java/com/laytonsmith/PureUtilities/ArgumentSuite.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@
/**
* An ArgumentSuite is an ArgumentParser that supports "modes".
*
* A mode is a required parameter that causes a fully separate argument parser to
* be used to parse the remaining arguments. This allows for finer control over
* mutually exclusive parameters in both the documentation and the validation, as
* well as wider support for traditional use cases.
* A mode is a required parameter that causes a fully separate argument parser to be used to parse the remaining
* arguments. This allows for finer control over mutually exclusive parameters in both the documentation and the
* validation, as well as wider support for traditional use cases.
*
*/
public class ArgumentSuite {
Expand All @@ -26,65 +25,67 @@ public class ArgumentSuite {
private Map<String, String> aliases;
private String description;

public ArgumentSuite(){
public ArgumentSuite() {
suite = new LinkedHashMap<String, ArgumentParser>();
aliases = new LinkedHashMap<String, String>();
}

/**
* Adds a new mode. A mode name may contain dashes, which would look like normal
* argument flags, but would actually be a mode. This is useful especially for a
* --help command, which shows the ArgumentSuite's help.
* Adds a new mode. A mode name may contain dashes, which would look like normal argument flags, but would actually
* be a mode. This is useful especially for a --help command, which shows the ArgumentSuite's help.
*
* @param modeName The name of this mode. This may not contain spaces.
* @param mode The sub-ArgumentParser that will be used when in this mode.
* @throws IllegalArgumentException if the name of the mode contains spaces
*/
public ArgumentSuite addMode(String modeName, ArgumentParser mode){
public ArgumentSuite addMode(String modeName, ArgumentParser mode) {
validateModeName(modeName);
suite.put(modeName, mode);
return this;
}

/**
* Adds a mode alias. This is the recommended behavior instead of adding the
* same mode with a different name, because the built description is aware
* of the difference between an alias and the real mode. All the same rules
* apply to aliases that apply to mode names. The realModeName doesn't
* strictly need to exist yet.
* Adds a mode alias. This is the recommended behavior instead of adding the same mode with a different name,
* because the built description is aware of the difference between an alias and the real mode. All the same rules
* apply to aliases that apply to mode names. The realModeName doesn't strictly need to exist yet.
*
* @param alias
* @param realModeName
* @return
*/
public ArgumentSuite addModeAlias(String alias, String realModeName){
public ArgumentSuite addModeAlias(String alias, String realModeName) {
validateModeName(alias);
aliases.put(alias, realModeName);
return this;
}

private void validateModeName(String modeName){
if(modeName.contains(" ")){
private void validateModeName(String modeName) {
if (modeName.contains(" ")) {
throw new IllegalArgumentException("The mode name may not contain a space.");
}
}

/**
* Adds a description, which is used in {@see #getBuiltDescription}
* Adds a description, which is used in {
*
* @see #getBuiltDescription}
* @param description
* @return
*/
public ArgumentSuite addDescription(String description){
public ArgumentSuite addDescription(String description) {
this.description = description;
return this;
}

/**
* Returns a mode that was previously registered.
*
* @param name The name of the mode to get
* @return The mode registered under the provided name
* @throws IllegalArgumentException if the mode is not registered
*/
public ArgumentParser getMode(String name){
if(suite.containsKey(name)){
public ArgumentParser getMode(String name) {
if (suite.containsKey(name)) {
return suite.get(name);
} else {
throw new IllegalArgumentException("No mode by the name \"" + name + "\" has been registered.");
Expand All @@ -93,28 +94,28 @@ public ArgumentParser getMode(String name){

/**
* Selects the appropriate mode, and calls match on that ArgumentParser.
*
* @param args The pre-parsed arguments
* @param defaultMode The default mode, which will be used only if no arguments were passed in.
* @return
* @throws ResultUseException if the mode cannot be found, or if the sub-ArgumentParser
* throws an exception.
* @throws ResultUseException if the mode cannot be found, or if the sub-ArgumentParser throws an exception.
*/
public ArgumentSuiteResults match(String [] args, String defaultMode) throws ResultUseException, ValidationException {
String [] nonModeArgs = ArrayUtils.EMPTY_STRING_ARRAY;
public ArgumentSuiteResults match(String[] args, String defaultMode) throws ResultUseException, ValidationException {
String[] nonModeArgs = ArrayUtils.EMPTY_STRING_ARRAY;
String mode;
if(args.length > 1){
if (args.length > 1) {
mode = args[0];
nonModeArgs = ArrayUtils.cast(ArrayUtils.slice(args, 1, args.length - 1), String[].class);
} else if(args.length == 1){
} else if (args.length == 1) {
mode = args[0];
} else {
//0 argsm, use the defaultMode.
mode = defaultMode;
}
if(aliases.containsKey(mode)){
if (aliases.containsKey(mode)) {
mode = aliases.get(mode);
}
if(suite.containsKey(mode)){
if (suite.containsKey(mode)) {
return new ArgumentSuiteResults(mode, suite.get(mode), suite.get(mode).match(nonModeArgs));
} else {
throw new ResultUseException("Mode " + mode + " was not found.");
Expand All @@ -123,11 +124,11 @@ public ArgumentSuiteResults match(String [] args, String defaultMode) throws Res

/**
* Selects the appropriate mode, and calls match on that ArgumentParser.
*
* @param args The unparsed arguments
* @param defaultMode The default mode, which will be used only if no arguments were passed in.
* @return
* @throws ResultUseException if the mode cannot be found, or if the sub-ArgumentParser
* throws an exception.
* @throws ResultUseException if the mode cannot be found, or if the sub-ArgumentParser throws an exception.
*/
public ArgumentSuiteResults match(String args, String defaultMode) throws ResultUseException, ValidationException {
//We're going to use ArgumentParser's parse method to get a string list, then
Expand All @@ -136,27 +137,28 @@ public ArgumentSuiteResults match(String args, String defaultMode) throws Result
}

/**
* Returns a built description of this ArgumentSuite, which would be appropriate
* to display if no arguments are passed in (or the mode name is help, -help, --help, etc)
* Returns a built description of this ArgumentSuite, which would be appropriate to display if no arguments are
* passed in (or the mode name is help, -help, --help, etc)
*
* @return
*/
public String getBuiltDescription(){
public String getBuiltDescription() {
StringBuilder b = new StringBuilder();
if(description != null){
if (description != null) {
b.append(description).append("\n\n");
}
b.append("Modes: (a mode must be the first argument) \n");
for(String mode : suite.keySet()){
for (String mode : suite.keySet()) {
b.append("\t").append(TermColors.BOLD).append(mode);
if(aliases.containsValue(mode)){
if (aliases.containsValue(mode)) {
List<String> keys = new ArrayList<>();
for(String alias : aliases.keySet()){
if(aliases.get(alias).equals(mode)){
for (String alias : aliases.keySet()) {
if (aliases.get(alias).equals(mode)) {
keys.add(alias);
}
}
b.append(TermColors.RESET).append(" (Alias");
if(keys.size() != 1){
if (keys.size() != 1) {
b.append("es");
}
b.append(": ").append(StringUtils.Join(keys, ", ")).append(")");
Expand All @@ -167,84 +169,88 @@ public String getBuiltDescription(){
}

/**
* A convenience method to get the real mode name registered for this
* alias, or null if no such alias exists. If the alias is actually a mode,
* it is simply returned. Useful for perhaps a help mode, to resolve the actual
* mode named. If {@code alias} is null, null is returned.
* A convenience method to get the real mode name registered for this alias, or null if no such alias exists. If the
* alias is actually a mode, it is simply returned. Useful for perhaps a help mode, to resolve the actual mode
* named. If {@code alias} is null, null is returned.
*
* @param alias
* @return
*/
public String getModeFromAlias(String alias){
if(alias == null){
public String getModeFromAlias(String alias) {
if (alias == null) {
return null;
}
if(suite.containsKey(alias)){
if (suite.containsKey(alias)) {
return alias;
} else if(aliases.containsKey(alias)){
} else if (aliases.containsKey(alias)) {
return aliases.get(alias);
} else {
return null;
}
}

/**
* A convenience method to get the underlying ArgumentParser
* based on the mode name given. Aliases will not suffice, but you
* may call getModeFromAlias to resolve the mode name first. Null
* is returned if no mode exists with that name. Useful for perhaps
* a help mode, to generically display a mode's help. If {@see mode}
* is null, null is returned.
* A convenience method to get the underlying ArgumentParser based on the mode name given. Aliases will not suffice,
* but you may call getModeFromAlias to resolve the mode name first. Null is returned if no mode exists with that
* name. Useful for perhaps a help mode, to generically display a mode's help. If {
*
* @see mode} is null, null is returned.
* @param mode
* @return
*/
public ArgumentParser getModeFromName(String mode){
if(mode == null){
public ArgumentParser getModeFromName(String mode) {
if (mode == null) {
return null;
}
if(suite.containsKey(mode)){
if (suite.containsKey(mode)) {
return suite.get(mode);
} else {
return null;
}
}

public static class ArgumentSuiteResults{
public static class ArgumentSuiteResults {

private ArgumentParser mode;
private ArgumentParserResults results;
private String modeName;
private ArgumentSuiteResults(String modeName, ArgumentParser mode, ArgumentParserResults results){

private ArgumentSuiteResults(String modeName, ArgumentParser mode, ArgumentParserResults results) {
this.modeName = modeName;
this.mode = mode;
this.results = results;
}

/**
* Returns the name of the mode that was selected. (Not the alias)
*
* @return
*/
public String getModeName(){
public String getModeName() {
return modeName;
}

/**
* The ArgumentParser for the given mode. This will be a reference
* to the mode passed in, so you can do == on it.
* The ArgumentParser for the given mode. This will be a reference to the mode passed in, so you can do == on
* it.
*
* @return
*/
public ArgumentParser getMode(){
public ArgumentParser getMode() {
return mode;
}

/**
* The ArgumentParserResults for the ArgumentParser mode
*
* @return
*/
public ArgumentParserResults getResults(){
public ArgumentParserResults getResults() {
return results;
}
}

public static void main(String [] args){
public static void main(String[] args) {
ArgumentSuite suite = new ArgumentSuite();
ArgumentParser mode1 = ArgumentParser.GetParser();
ArgumentParser mode2 = ArgumentParser.GetParser();
Expand All @@ -267,21 +273,21 @@ public static void main(String [] args){
ArgumentSuiteResults results = suite.match("wat", "help");
ArgumentParser mode = results.getMode();
ArgumentParserResults modeResults = results.getResults();
if(mode == help){
if (mode == help) {
String modeHelp = modeResults.getStringArgument();
ArgumentParser selectedMode = suite.getModeFromName(suite.getModeFromAlias(modeHelp));
if(selectedMode != null){
if (selectedMode != null) {
StreamUtils.GetSystemOut().println(selectedMode.getBuiltDescription());
System.exit(0);
} else {
showHelp(suite);
}
} else if(mode == mode1 || mode == mode2){
} else if (mode == mode1 || mode == mode2) {
String argument;
if((argument = modeResults.getStringArgument("arg1")) != null){
if ((argument = modeResults.getStringArgument("arg1")) != null) {
StreamUtils.GetSystemOut().println("You selected " + argument + " for arg1");
}
if((argument = modeResults.getStringArgument("arg2")) != null){
if ((argument = modeResults.getStringArgument("arg2")) != null) {
StreamUtils.GetSystemOut().println("You selected " + argument + " for arg2");
}
System.exit(0);
Expand All @@ -293,7 +299,7 @@ public static void main(String [] args){
}
}

private static void showHelp(ArgumentSuite suite){
private static void showHelp(ArgumentSuite suite) {
StreamUtils.GetSystemOut().println(suite.getBuiltDescription());
System.exit(1);
}
Expand Down
Loading

0 comments on commit f243a14

Please sign in to comment.