Posts Tagged ‘debug’

Debugging parameters for CLI apps using Eclipse PDT

Sunday, May 3rd, 2009

I write quite a lot of command line utilities in PHP and luckily PDT makes it easy to debug command line scripts.

The thing with command line scripts is that often they require parameters passed on the command line, and PDT offers an easy way to pass them when you debug a script.

Assuming you already have debugging in Eclipse working (using either XDebug or Zend Debugger, both will work with this feature), here are the steps to debug a command line script using command line parameters:

  1. Right-click the script you want to debug and select 'Debug As...' and from the context menu that appears, select 'Debug Configurations...', like this:

    PDT context menu

  2. In the debug configurations screen that appears, select the 'PHP Script Arguements' tab. In the text box that appears, you can add fixed parameters (e.g. --key=value), but it's much more useful to use Eclipse's dynamic parameters, like this:

    picture-29

    In this case I entered --visitor=${string_prompt:Visitor} which means: pass --visitor= to the script when debugging, but ask me to enter a value named 'Visitor'.

  3. You can use the 'Variables' button to enter variables like this using a more visual wizard. There are many different prompts, string_prompt is just one example; you can prompt for folders and passwords as well, and you can inject certain system variables as a parameter using this wizard)
  4. After you've saved the Debug Configurations, again right-click the script you want to debug, and select 'Debug As...' and then 'PHP Script'.
  5. Eclipse now pops up the prompt, like this:

    picture-30

Voila, you can now debug your command line script by passing in dynamic parameters whenever you start the debug session.

P.S. This not only works in Eclipse PDT but also in Zend Studio for Eclipse and Aptana (in theory, haven't tried either yet).

P.P.S. No, I didn't create a script that allows me to track individual visitors, it's an archive processor for Flackr that uses the Visitor design pattern to make it easier to write multiple processors. I love Design Patterns.