implement passing of command line options (#2552)

This commit is contained in:
Ben Fry
2015-08-14 09:19:53 -04:00
parent a637c37805
commit c76db01b12
6 changed files with 111 additions and 90 deletions

View File

@@ -112,10 +112,10 @@ public class Commander implements RunnerListener {
System.exit(1);
}
// File preferencesFile = Base.getSettingsFile("preferences.txt");
// System.out.println("Preferences file at " + preferencesFile.getAbsolutePath());
int argOffset = 0;
for (String arg : args) {
argOffset++;
if (arg.length() == 0) {
// ignore it, just the crappy shell script
@@ -127,18 +127,19 @@ public class Commander implements RunnerListener {
} else if (arg.equals(buildArg)) {
task = BUILD;
break;
} else if (arg.equals(runArg)) {
task = RUN;
break;
} else if (arg.equals(presentArg)) {
task = PRESENT;
// } else if (arg.equals(exportAppletArg)) {
// task = EXPORT_APPLET;
break;
} else if (arg.equals(exportApplicationArg)) {
task = EXPORT;
break;
} else if (arg.equals(noJavaArg)) {
embedJava = false;
@@ -189,6 +190,7 @@ public class Commander implements RunnerListener {
complainAndQuit("I don't know anything about " + arg + ".", true);
}
}
String[] sketchArgs = PApplet.subset(args, argOffset);
// if ((outputPath == null) &&
// (task == PREPROCESS || task == BUILD ||
@@ -268,7 +270,11 @@ public class Commander implements RunnerListener {
success = true;
if (task == RUN || task == PRESENT) {
Runner runner = new Runner(build, this);
runner.launch(task == PRESENT);
if (task == PRESENT) {
runner.present(sketchArgs);
} else {
runner.launch(sketchArgs);
}
}
} else {
success = false;
@@ -369,7 +375,7 @@ public class Commander implements RunnerListener {
out.println();
out.println("--build Preprocess and compile a sketch into .class files.");
out.println("--run Preprocess, compile, and run a sketch.");
out.println("--present Preprocess, compile, and run a sketch full screen.");
out.println("--present Preprocess, compile, and run a sketch in presentation mode.");
out.println();
out.println("--export Export an application.");
out.println("--no-java Do not embed Java. Use at your own risk!");
@@ -378,6 +384,13 @@ public class Commander implements RunnerListener {
// out.println("--bits Must be specified if libraries are used that are");
// out.println(" 32- or 64-bit specific such as the OpenGL library.");
// out.println(" Otherwise specify 0 or leave it out.");
out.println("The --build, --run, --present, or --export must be the final parameter.");
out.println("Additional arguments will be passed to the sketch itself and available");
out.println("in the sketch's 'args' field. To pass options understood by PApplet.main(),");
out.println("write a custom main() method so that the preprocessor does not add one.");
out.println("https://github.com/processing/processing/wiki/Command-Line");
out.println();
}

View File

@@ -216,7 +216,7 @@ public class Debugger implements VMEventListener {
log(Level.INFO, "launching debuggee runtime");
runtime = new Runner(build, editor);
VirtualMachine vm = runtime.launchDebug(); // non-blocking
VirtualMachine vm = runtime.debug(null); // non-blocking
if (vm == null) {
log(Level.SEVERE, "error 37: launch failed");
}

View File

@@ -149,7 +149,12 @@ public class JavaMode extends Mode {
final Runner runtime = new Runner(build, listener);
new Thread(new Runnable() {
public void run() {
runtime.launch(present); // this blocks until finished
// these block until finished
if (present) {
runtime.present(null);
} else {
runtime.launch(null);
}
}
}).start();
return runtime;
@@ -197,15 +202,20 @@ public class JavaMode extends Mode {
if (appletClassName != null) {
final Runner runtime = new Runner(build, listener);
new Thread(new Runnable() {
public void run() {
runtime.launch(present); // this blocks until finished
// next lines are executed when the sketch quits
if (launchInteractive) {
editor.initEditorCode(parser.allHandles, false);
editor.stopTweakMode(parser.allHandles);
}
public void run() {
// these block until finished
if (present) {
runtime.present(null);
} else {
runtime.launch(null);
}
}).start();
// next lines are executed when the sketch quits
if (launchInteractive) {
editor.initEditorCode(parser.allHandles, false);
editor.stopTweakMode(parser.allHandles);
}
}
}).start();
if (launchInteractive) {
// replace editor code with baseCode

View File

@@ -26,6 +26,7 @@ import processing.app.*;
import processing.app.exec.StreamRedirectThread;
import processing.app.ui.Editor;
import processing.core.*;
import processing.data.StringList;
import processing.mode.java.JavaBuild;
import java.awt.GraphicsDevice;
@@ -113,10 +114,19 @@ public class Runner implements MessageConsumer {
}
public void launch(boolean presenting) {
if (launchVirtualMachine(presenting)) {
public VirtualMachine launch(String[] args) {
if (launchVirtualMachine(false, args)) {
generateTrace();
}
return vm;
}
public VirtualMachine present(String[] args) {
if (launchVirtualMachine(true, args)) {
generateTrace();
}
return vm;
}
@@ -124,8 +134,8 @@ public class Runner implements MessageConsumer {
* Simple non-blocking launch of the virtual machine. VM starts suspended.
* @return debuggee VM or null on failure
*/
public VirtualMachine launchDebug() {
if (launchVirtualMachine(false)) { // will return null on failure
public VirtualMachine debug(String[] args) {
if (launchVirtualMachine(false, args)) { // will return null on failure
redirectStreams(vm);
}
return vm;
@@ -153,9 +163,9 @@ public class Runner implements MessageConsumer {
}
public boolean launchVirtualMachine(boolean presenting) {
String[] vmParams = getMachineParams();
String[] sketchParams = getSketchParams(presenting);
public boolean launchVirtualMachine(boolean present, String[] args) {
StringList vmParams = getMachineParams();
StringList sketchParams = getSketchParams(present, args);
// PApplet.printArray(sketchParams);
int port = 8000 + (int) (Math.random() * 1000);
String portStr = String.valueOf(port);
@@ -167,13 +177,13 @@ public class Runner implements MessageConsumer {
String jdwpArg = "-agentlib:jdwp=transport=dt_socket,address=" + portStr + ",server=y,suspend=y";
// Everyone works the same under Java 7 (also on OS X)
String[] commandArgs = new String[] { Platform.getJavaPath(), jdwpArg };
StringList commandArgs = new StringList();
commandArgs.append(Platform.getJavaPath());
commandArgs.append(jdwpArg);
commandArgs = PApplet.concat(commandArgs, vmParams);
commandArgs = PApplet.concat(commandArgs, sketchParams);
// PApplet.println(commandArgs);
// commandArg.setValue(commandArgs);
launchJava(commandArgs);
commandArgs.append(vmParams);
commandArgs.append(sketchParams);
launchJava(commandArgs.array());
AttachingConnector connector = (AttachingConnector)
findConnector("com.sun.jdi.SocketAttach");
@@ -228,8 +238,8 @@ public class Runner implements MessageConsumer {
}
protected String[] getMachineParams() {
ArrayList<String> params = new ArrayList<String>();
protected StringList getMachineParams() {
StringList params = new StringList();
//params.add("-Xint"); // interpreted mode
//params.add("-Xprof"); // profiler
@@ -244,7 +254,7 @@ public class Runner implements MessageConsumer {
for (int i = 0; i < pieces.length; i++) {
String p = pieces[i].trim();
if (p.length() > 0) {
params.add(p);
params.append(p);
}
}
}
@@ -252,65 +262,45 @@ public class Runner implements MessageConsumer {
// params.add("-Djava.ext.dirs=nuffing");
if (Preferences.getBoolean("run.options.memory")) {
params.add("-Xms" + Preferences.get("run.options.memory.initial") + "m");
params.add("-Xmx" + Preferences.get("run.options.memory.maximum") + "m");
params.append("-Xms" + Preferences.get("run.options.memory.initial") + "m");
params.append("-Xmx" + Preferences.get("run.options.memory.maximum") + "m");
}
if (Platform.isMacOS()) {
params.add("-Xdock:name=" + build.getSketchClassName());
params.append("-Xdock:name=" + build.getSketchClassName());
// params.add("-Dcom.apple.mrj.application.apple.menu.about.name=" +
// sketch.getMainClassName());
}
// sketch.libraryPath might be ""
// librariesClassPath will always have sep char prepended
params.add("-Djava.library.path=" +
build.getJavaLibraryPath() +
File.pathSeparator +
System.getProperty("java.library.path"));
params.append("-Djava.library.path=" +
build.getJavaLibraryPath() +
File.pathSeparator +
System.getProperty("java.library.path"));
params.add("-cp");
params.add(build.getClassPath());
// params.add(sketch.getClassPath() +
// File.pathSeparator +
// Base.librariesClassPath);
params.append("-cp");
params.append(build.getClassPath());
// enable assertions
// http://dev.processing.org/bugs/show_bug.cgi?id=1188
params.add("-ea");
params.append("-ea");
//PApplet.println(PApplet.split(sketch.classPath, ':'));
String outgoing[] = new String[params.size()];
params.toArray(outgoing);
// PApplet.println(outgoing);
// PApplet.println(PApplet.split(outgoing[0], ":"));
// PApplet.println();
// PApplet.println("class path");
// PApplet.println(PApplet.split(outgoing[2], ":"));
return outgoing;
//return (String[]) params.toArray();
// System.out.println("sketch class path");
// PApplet.println(PApplet.split(sketch.classPath, ';'));
// System.out.println();
// System.out.println("libraries class path");
// PApplet.println(PApplet.split(Base.librariesClassPath, ';'));
// System.out.println();
return params;
}
protected String[] getSketchParams(boolean presenting) {
ArrayList<String> params = new ArrayList<String>();
protected StringList getSketchParams(boolean present, String[] args) {
StringList params = new StringList();
// It's dangerous to add your own main() to your code,
// but if you've done it, we'll respect your right to hang yourself.
// http://processing.org/bugs/bugzilla/1446.html
if (build.getFoundMain()) {
params.add(build.getSketchClassName());
params.append(build.getSketchClassName());
} else {
params.add("processing.core.PApplet");
params.append("processing.core.PApplet");
// get the stored device index (starts at 1)
int runDisplay = Preferences.getInteger("run.display");
@@ -366,8 +356,8 @@ public class Runner implements MessageConsumer {
// If sketches are to be shown on the same display as the editor,
// provide the editor location so the sketch's main() can place it.
Point editorLocation = editor.getLocation();
params.add(PApplet.ARGS_EDITOR_LOCATION + "=" +
editorLocation.x + "," + editorLocation.y);
params.append(PApplet.ARGS_EDITOR_LOCATION + "=" +
editorLocation.x + "," + editorLocation.y);
} else {
// The sketch's main() will set a location centered on the new
// display. It has to happen in main() because the width/height
@@ -379,37 +369,38 @@ public class Runner implements MessageConsumer {
// params.add(PApplet.ARGS_LOCATION + "=" + runX + "," + runY);
}
} else {
params.add(PApplet.ARGS_LOCATION + "=" +
windowLocation.x + "," + windowLocation.y);
params.append(PApplet.ARGS_LOCATION + "=" +
windowLocation.x + "," + windowLocation.y);
}
params.add(PApplet.ARGS_EXTERNAL);
params.append(PApplet.ARGS_EXTERNAL);
}
params.add(PApplet.ARGS_DISPLAY + "=" + runDisplay);
params.append(PApplet.ARGS_DISPLAY + "=" + runDisplay);
if (presenting) {
params.add(PApplet.ARGS_PRESENT);
if (present) {
params.append(PApplet.ARGS_PRESENT);
// if (Preferences.getBoolean("run.present.exclusive")) {
// params.add(PApplet.ARGS_EXCLUSIVE);
// }
params.add(PApplet.ARGS_STOP_COLOR + "=" +
Preferences.get("run.present.stop.color"));
params.add(PApplet.ARGS_WINDOW_COLOR + "=" +
Preferences.get("run.present.bgcolor"));
params.append(PApplet.ARGS_STOP_COLOR + "=" +
Preferences.get("run.present.stop.color"));
params.append(PApplet.ARGS_WINDOW_COLOR + "=" +
Preferences.get("run.present.bgcolor"));
}
// There was a PDE X hack that put this after the class name, but it was
// removed for 3.0a6 because it would break the args passed to sketches.
params.add(PApplet.ARGS_SKETCH_FOLDER + "=" + build.getSketchPath());
params.append(PApplet.ARGS_SKETCH_FOLDER + "=" + build.getSketchPath());
params.add(build.getSketchClassName());
params.append(build.getSketchClassName());
}
// String outgoing[] = new String[params.size()];
// params.toArray(outgoing);
// return outgoing;
return params.toArray(new String[0]);
// Add command-line arguments to be given to the sketch itself
if (args != null) {
params.append(args);
}
// Pass back the whole list
return params;
}