Debugging parameters for CLI apps using Eclipse PDT

May 3rd, 2009 by Ivo

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:


    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:


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.

3 Responses to “Debugging parameters for CLI apps using Eclipse PDT”

  1. May 03, 2009 at 11:10 am, Twitted by phpbr said:

    [...] This post was Twitted by phpbr – [...]

  2. May 04, 2009 at 6:13 pm, Ivo Jansch’s Blog: Debugging parameters for CLI apps using Eclipse PDT | Cole Design Studios said:

    [...] Jansch has a new blog post today looking at a method he’s come up with to debug command-line PHP applications in Eclipse [...]

  3. May 18, 2009 at 8:14 pm, patrick said:

    This is tested and works in Zend Studio 6.1.1 and WAMP.

    Just need to be sure to install zend studio debugger in the correct php.ini in wamp folder, and also select the wamp php.exe as the executable (or whichever executable you are using from the command line). This is usually the on found in your path variable