[SOLVED] Command Line Arguments Width and Height

It took me a while to figure out how to run a processing sketch with a size defined by the command line arguments so I thought I’d share the solution I came up with. If someone has suggestions to make this more streamlined I’d love to hear them.

sketch.pde

void settings() {
  int argWidth = 0;
  int argHeight = 0;
  String usage = "Usage width(int) height(int)";
  if (args == null || args.length != 2) {
    println(usage);
    exit();
  }
  try {
    argWidth = Integer.parseInt(args[0]);
    argHeight = Integer.parseInt(args[1]);
  } catch (NumberFormatException e) {
    println(usage);
    exit();
  }
  size(argWidth, argHeight);
}

void setup() {
  
}

void draw() {
  
}

tasks.json for use with VS Code, not necessary but makes it easier to run the sketch

// Visual Studio Code tasks.json for the sketch
// Uses Processing VS code extension (https://github.com/TobiahZ/processing-vscode)
// Width and Height are hard coded in this file for rapid testing.
// Ctrl + Shift + B to Run
// Only tested on Linux

{
  "version": "2.0.0",
  "tasks": [
    {
      "label": "All",
      "type": "shell",
      "group": {
        "kind": "build",
        "isDefault": true
      },
      "command": "echo",
      "presentation": {
        "reveal": "always",
        "focus": false,
        "panel": "shared",
      },
      "args": [
        "Running Clean, Build, and Run Tasks",
      ],
      "dependsOrder": "sequence",
      "dependsOn": [
        "Clean",
        "Build",
        "Run"
      ]
    },{
      "label": "Clean",
      "type": "shell",
      "group": "build",
      "command": "rm",
      "presentation": {
        "echo": true,
        "reveal": "always",
        "focus": false,
        "panel": "shared",
      },
      "args": [
        "-rf",
        "${workspaceRoot}/out",
      ],
    },{
      "label": "Build",
      "type": "shell",
      "group": "build",
      "command": "${config:processing.path}",
      "presentation": {
        "echo": true,
        "reveal": "always",
        "focus": false,
        "panel": "shared",
      },
      "args": [
	    	"--force",
	    	"--sketch=${workspaceRoot}",
	    	"--output=${workspaceRoot}/out",
        "--export"
	    ],
    },{
      "label": "Run",
      "type": "shell",
      "group": "build",
      "command": "${workspaceRoot}/out/${workspaceFolderBasename}",
      "presentation": {
        "echo": true,
        "reveal": "always",
        "focus": false,
        "panel": "shared",
      },
      "args": [
        "512",
        "512",
      ]
    }
  ]
}
1 Like

Thanks for sharing this!

Some previous related discussions for earlier versions from 2017 and 2016:

1 Like