We are about to switch to a new forum software. Until then we have removed the registration on this forum.
I have a sketch that I want to
In order to do this I need to detect whether it was run from the command line!
Currently I can assume a command line invocation if there are args present.
However, I would like to detect command line invocation even if there are no args. Is that possible?
boolean exitWhenDone;
void setup() {
// detect command line invocation if args present
if (args != null) {
// default command line behavior - run and quit
exitWhenDone = true;
// override default with command line argument
for (String arg : args) {
if ("noexit".equals(arg)) {
exitWhenDone=false;
}
}
}
}
void draw() {
if (exitWhenDone) {
exit();
}
}
The sketch detects CL invocation with any throwaway placeholder arg, e.g.
processing-java --sketch=`pwd`/CLTest --run myarg
It doesn't work without, because args is null:
processing-java --sketch=`pwd`/CLTest --run
Answers
What operating system is this? 8)
Hah!
macOS 10.12.6 (Sierra)
this works in linux ("null" when run with ctrl-r in pde, prints the arg if run with
./tmpArgs whatever
)oh, that's what you currently have...
Great! I'm trying to have the sketch distinguish between:
./tmpNoArgs
-- no argsIn other words "Was I run from the command line with no args, or from PDE?"
that last one looks promising
ah, ok, i was running an exported app vs the pde. the processing-java version does a different sun.java.command again (ie can distinguish the three cases)
(as an aside, the linux export creates an additional shell script containing the following:
}
where
tmpArgs
is the sketch name)Excellent --
System.getProperty("sun.java.command")
looks like exactly what I needed. Many thanks, @koogs.the name suggests that it's an oracle thing. but it's in both the pde and command line version (the former is using openjdk, the latter oracle)
So, it turns out that if a sketch was called from PDE it will have been passed the flag "--external" -- if from the command line, not. Documented here:
Parsing
System.getProperty("sun.java.command")
for that flag will detect PDE.This is a reliable way to distinguish between a sketch being run in PDE or the command line even without using command line arguments.
I'm not yet sure how this affects exported applications, however.
Here is a demo sketch that combines reliable no-argument detection with argument parsing: