Permalink
Browse files

Work on making clamp work as a composite function

This is a good example of a function for this, as it has no external
dependencies, is purely a math based function, has a simple
implementation, but takes full advantage of the optimization system.

There are also improvements to the ArgumentParser, along with a few bug
fixes in that area.
  • Loading branch information...
LadyCailin committed Jan 17, 2019
1 parent 0da70ce commit 135470203b8520abc75c605b2617bab385ca2a8d

Large diffs are not rendered by default.

Oops, something went wrong.
@@ -4,7 +4,6 @@
import com.laytonsmith.PureUtilities.ArgumentParser.ResultUseException;
import com.laytonsmith.PureUtilities.ArgumentParser.ValidationException;
import com.laytonsmith.PureUtilities.Common.ArrayUtils;
import com.laytonsmith.PureUtilities.Common.StreamUtils;
import com.laytonsmith.PureUtilities.Common.StringUtils;
import java.util.ArrayList;
import java.util.LinkedHashMap;
@@ -26,8 +25,8 @@
private String description;

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

/**
@@ -36,6 +35,7 @@ public ArgumentSuite() {
*
* @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.
* @return
* @throws IllegalArgumentException if the name of the mode contains spaces
*/
public ArgumentSuite addMode(String modeName, ArgumentParser mode) {
@@ -99,6 +99,7 @@ public ArgumentParser getMode(String name) {
* @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 com.laytonsmith.PureUtilities.ArgumentParser.ValidationException
*/
public ArgumentSuiteResults match(String[] args, String defaultMode) throws ResultUseException, ValidationException {
String[] nonModeArgs = ArrayUtils.EMPTY_STRING_ARRAY;
@@ -129,6 +130,7 @@ public ArgumentSuiteResults match(String[] args, String defaultMode) throws Resu
* @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 com.laytonsmith.PureUtilities.ArgumentParser.ValidationException
*/
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
@@ -250,56 +252,4 @@ public ArgumentParserResults getResults() {
}
}

public static void main(String[] args) {
ArgumentSuite suite = new ArgumentSuite();
ArgumentParser mode1 = ArgumentParser.GetParser();
ArgumentParser mode2 = ArgumentParser.GetParser();
mode1.addArgument("arg1", ArgumentParser.Type.STRING, "Argument 1", "arg1", false);
mode1.addArgument("arg2", ArgumentParser.Type.STRING, "Argument 2", "arg2", false);
mode1.addDescription("A description of Mode 1");
mode2.addArgument("arg1", ArgumentParser.Type.STRING, "Argument 1", "arg1", false);
mode2.addArgument("arg2", ArgumentParser.Type.STRING, "Argument 2", "arg2", false);
mode2.addDescription("A description of Mode 2");
suite.addModeAlias("mode-2", "mode2");
suite.addModeAlias("mode--2", "mode2");
ArgumentParser help = ArgumentParser.GetParser();
help.addDescription("Displays the help for a mode, then exits.")
.addArgument("The mode name", "mode name", true);
suite.addDescription("A description of the suite")
.addMode("help", help)
.addMode("mode1", mode1)
.addMode("mode2", mode2);
try {
ArgumentSuiteResults results = suite.match("wat", "help");
ArgumentParser mode = results.getMode();
ArgumentParserResults modeResults = results.getResults();
if(mode == help) {
String modeHelp = modeResults.getStringArgument();
ArgumentParser selectedMode = suite.getModeFromName(suite.getModeFromAlias(modeHelp));
if(selectedMode != null) {
StreamUtils.GetSystemOut().println(selectedMode.getBuiltDescription());
System.exit(0);
} else {
showHelp(suite);
}
} else if(mode == mode1 || mode == mode2) {
String argument;
if((argument = modeResults.getStringArgument("arg1")) != null) {
StreamUtils.GetSystemOut().println("You selected " + argument + " for arg1");
}
if((argument = modeResults.getStringArgument("arg2")) != null) {
StreamUtils.GetSystemOut().println("You selected " + argument + " for arg2");
}
System.exit(0);
}
} catch (ResultUseException | ValidationException ex) {
showHelp(suite);
}
}

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

}
@@ -63,7 +63,7 @@ public static String GetString(InputStream in, String encoding) throws Unsupport
encoding = "UTF-8";
}
if(in == null) {
throw new NullPointerException();
throw new NullPointerException("InputStream is null");
}
InputStreamReader input;
input = new InputStreamReader(new BufferedInputStream(in), encoding);
@@ -1,6 +1,7 @@
package com.laytonsmith.core;

import com.laytonsmith.PureUtilities.ArgumentParser;
import com.laytonsmith.PureUtilities.ArgumentParser.ArgumentBuilder;
import com.laytonsmith.PureUtilities.Common.StreamUtils;
import com.laytonsmith.PureUtilities.Common.StringUtils;
import com.laytonsmith.PureUtilities.TermColors;
@@ -439,17 +440,32 @@ public final void reload(MCPlayer player, String[] settings, boolean firstLoad)
boolean extensions;

private final ArgumentParser options = ArgumentParser.GetParser()
.addFlag("whitelist", "Sets the list of arguments to be a whitelist, that is,"
.addArgument(new ArgumentBuilder().setDescription("Sets the list of arguments to be a whitelist, that is,"
+ " only the specified modules get reloaded, the rest will be skipped. Without this option,"
+ " the specified modules don't get reloaded.")
.addFlag('g', "globals", "Specifies that globals memory (values stored with export/import) should be preserved.")
.addFlag('t', "tasks", "Specifies that tasks registered with set_interval/set_timeout should be preserved.")
.addFlag('e', "execution-queue", "Specifies that tasks registered in execution queues should be preserved.")
.addFlag('r', "persistence-config", "Specifies that the persistence config file should not be reloaded.")
.addFlag('f', "profiler", "Specifies that the profiler config should not be reloaded.")
.addFlag('s', "scripts", "Specifies that scripts should not be reloaded.")
.addFlag('x', "extensions", "Specifies that extensions should not be reloaded.")
.addFlag('h', "help", "Prints this list and returns. Nothing is reloaded if this option is set.");
.asFlag().setName("whitelist"))
.addArgument(new ArgumentBuilder().setDescription("Specifies that globals memory (values stored with"
+ " export/import) should be preserved.")
.asFlag().setName('g', "globals"))
.addArgument(new ArgumentBuilder().setDescription("Specifies that tasks registered with"
+ " set_interval/set_timeout should be preserved.")
.asFlag().setName('t', "tasks"))
.addArgument(new ArgumentBuilder().setDescription("Specifies that tasks registered in execution queues"
+ " should be preserved.")
.asFlag().setName('e', "execution-queue"))
.addArgument(new ArgumentBuilder().setDescription("Specifies that the persistence config file should"
+ " not be reloaded.")
.asFlag().setName('r', "persistence-config"))
.addArgument(new ArgumentBuilder().setDescription("Specifies that the profiler config should not be"
+ " reloaded.")
.asFlag().setName('f', "profiler"))
.addArgument(new ArgumentBuilder().setDescription("Specifies that scripts should not be reloaded.")
.asFlag().setName('s', "scripts"))
.addArgument(new ArgumentBuilder().setDescription("Specifies that extensions should not be reloaded.")
.asFlag().setName('x', "extensions"))
.addArgument(new ArgumentBuilder().setDescription("Prints this list and returns. Nothing is reloaded"
+ " if this option is set.")
.asFlag().setName('h', "help"));

public ReloadOptions(String[] settings) throws ArgumentParser.ValidationException {
globals = true;
Oops, something went wrong.

0 comments on commit 1354702

Please sign in to comment.