handling command line arguments in a sketch

edited September 2017 in Share Your Work

processing sketches can handle command line arguments. here is how to do so:

void setup() {
  if (args != null) {
    println(args.length);
    for (int i = 0; i < args.length; i++) {
      println(args[i]);
    }
  } else {
    println("args == null");
  }
}

here is how to invoke a sketch while passing command line arguments and the output:

$ processing-java --sketch=sketchname --run argu "arg o"
2
argu
arg o

here is output when not passing arguments:

$ processing-java --sketch=sketchname --run
args == null

as far as i know it is not possible to pass command line arguments when runnig the sketch from the processing editor. args will always be null in the editor. it is prudent to code some sensible default.


i posted this because i wanted handle command line arguments and searched the forum and only got vague hints how to do so. i managed to do it and now want to share the knowledge. i hope this will help future hackers wanting to handle command line arguments.

here are some of the threads with the vague hints:

Tagged:

Comments

  • edited August 2017

    Thanks for sharing this -- very interesting!

    This might also appeal to people interested in REPL mode and the new p5.py ....

Sign In or Register to comment.