Permalink
Browse files

Allow regex search in api tool and interpreter

  • Loading branch information...
LadyCailin committed Jan 30, 2019
1 parent 60a5879 commit 22b465ce1ebee771840df67d2be056a72f27902f
@@ -1160,7 +1160,16 @@ public String getBuiltDescription() {

//Now, if the default switch exists, put it here too
if(getArgument() != null) {
parts.add("<" + getArgument().usageName + ", ...>");
String s = "<";
if(getArgument().isNumeric()) {
s += "#";
}
s += getArgument().usageName;
if(getArgument().isArray()) {
s += ", ...";
}
s += ">";
parts.add(s);
}

{
@@ -183,11 +183,16 @@
.addDescription("Starts the automatic wiki uploader GUI.");
suite.addMode("docgen", DOCGEN_MODE);
API_MODE = ArgumentParser.GetParser()
.addDescription("Prints documentation for the function specified, then exits.")
.addDescription("Prints documentation for the function specified, then exits. The argument is actually"
+ " a regex, with ^ and $ added to it, so if you would like to search the function list,"
+ " you can instead provide the rest of the regex. If multiple matches are found, the full"
+ " list of matches is printed out. For instance \"array.*\" will return all the functions"
+ " that start with the word \"array\".")
.addArgument(new ArgumentBuilder()
.setDescription("The name of the function to print the information for")
.setUsageName("function")
.setRequiredAndDefault())
.setUsageName("functionRegex")
.setRequiredAndDefault()
.setArgType(ArgumentBuilder.BuilderTypeNonFlag.STRING))
.addArgument(new ArgumentBuilder()
.setDescription("Instead of displaying the results in the console, launches the website with"
+ " this function highlighted. The local documentation is guaranteed to be consistent"
@@ -624,26 +629,35 @@ public static void main(String[] args) throws Exception {
String function = parsedArgs.getStringArgument();
boolean examples = parsedArgs.isFlagSet('e');
if("".equals(function)) {
StreamUtils.GetSystemErr().println("Usage: java -jar CommandHelper.jar --api <function name>");
StreamUtils.GetSystemErr().println("Usage: java -jar CommandHelper.jar api <function name>");
System.exit(1);
}
FunctionBase f;
try {
f = FunctionList.getFunction(function, Target.UNKNOWN);
} catch (ConfigCompileException e) {
List<FunctionBase> fl = new ArrayList<>();
for(FunctionBase fb : FunctionList.getFunctionList(api.Platforms.INTERPRETER_JAVA)) {
if(fb.getName().matches("^" + function + "$")) {
fl.add(fb);
}
}
if(fl.isEmpty()) {
StreamUtils.GetSystemErr().println("The function '" + function + "' was not found.");
System.exit(1);
throw new Error();
}
if(parsedArgs.isFlagSet("online")) {
String url = String.format("https://methodscript.com/docs/%s/API/functions/%s",
MSVersion.LATEST.toString(), f.getName());
System.out.println("Launching browser to " + url);
if(!UIUtils.openWebpage(new URL(url))) {
System.err.println("Could not launch browser");
} else if(fl.size() == 1) {
FunctionBase f = fl.get(0);
if(parsedArgs.isFlagSet("online")) {
String url = String.format("https://methodscript.com/docs/%s/API/functions/%s",
MSVersion.LATEST.toString(), f.getName());
System.out.println("Launching browser to " + url);
if(!UIUtils.openWebpage(new URL(url))) {
System.err.println("Could not launch browser");
}
} else {
StreamUtils.GetSystemOut().println(Interpreter.formatDocsForCmdline(f.getName(), examples));
}
} else {
StreamUtils.GetSystemOut().println(Interpreter.formatDocsForCmdline(f.getName(), examples));
StreamUtils.GetSystemOut().println("Multiple function matches found:");
for(FunctionBase fb : fl) {
StreamUtils.GetSystemOut().println(fb.getName());
}
}
System.exit(0);
} else if(mode == SYNTAX_MODE) {
@@ -201,7 +201,8 @@ private String getHelpMsg() {
+ "If $$ is on a line by itself, it puts the shell in shell_adv mode, and each line is taken as if it started\n"
+ "with $$. Use - on a line by itself to exit this mode as well.\n\n"
+ "For more information about a specific function, type \"help function\"\n"
+ "and for documentation plus examples, type \"examples function\".";
+ "and for documentation plus examples, type \"examples function\". See the api tool\n"
+ "for more information about this feature.";
try {
msg += "\nYour current working directory is: " + env.getEnv(GlobalEnv.class).GetRootFolder().getCanonicalPath();
} catch (IOException ex) {
@@ -583,32 +584,30 @@ private boolean textLine(String line) throws IOException {
break;
default:
{
Pattern p = Pattern.compile("help (.*)");
Pattern p = Pattern.compile("(help|examples) (.*)");
Matcher m;
if((m = p.matcher(line)).find()) {
String helpCommand = m.group(1);
String helpCommand = m.group(2);
try {
StreamUtils.GetSystemOut().println(formatDocsForCmdline(helpCommand, false));
} catch (ConfigCompileException e) {
StreamUtils.GetSystemErr().println("Could not find function of name " + helpCommand);
} catch (IOException | DataSourceException | URISyntaxException
| DocGenTemplates.Generator.GenerateException e) {
e.printStackTrace(StreamUtils.GetSystemErr());
}
break;
}
}
{
Pattern p = Pattern.compile("examples (.*)");
Matcher m;
if((m = p.matcher(line)).find()) {
String helpCommand = m.group(1);
try {
StreamUtils.GetSystemOut().println(formatDocsForCmdline(helpCommand, true));
} catch (ConfigCompileException e) {
StreamUtils.GetSystemErr().println("Could not find function of name " + helpCommand);
List<FunctionBase> fl = new ArrayList<>();
for(FunctionBase fb : FunctionList.getFunctionList(api.Platforms.INTERPRETER_JAVA)) {
if(fb.getName().matches("^" + helpCommand + "$")) {
fl.add(fb);
}
}
if(fl.isEmpty()) {
StreamUtils.GetSystemErr().println("Could not find function of name " + helpCommand);
} else if(fl.size() == 1) {
StreamUtils.GetSystemOut().println(formatDocsForCmdline(helpCommand,
m.group(1).equals("examples")));
} else {
StreamUtils.GetSystemOut().println("Multiple function matches found:");
for(FunctionBase fb : fl) {
StreamUtils.GetSystemOut().println(fb.getName());
}
}
} catch (IOException | DataSourceException | URISyntaxException
| DocGenTemplates.Generator.GenerateException e) {
| DocGenTemplates.Generator.GenerateException | ConfigCompileException e) {
e.printStackTrace(StreamUtils.GetSystemErr());
}
break;

0 comments on commit 22b465c

Please sign in to comment.