Custom global commands in Command Prompt (cmd.exe) with parameters

Here’s a quick-and-dirty way to create custom global commands in Command Prompt (cmd.exe) with parameters.

I recently had the (dis)pleasure of trying to generate documentation with PHPDoc on a Windows machine.

Making a custom command for PHPDoc made the process far less painful!

Steps

  1. Download phpDocumentor.phar from phpdoc.org to C:\bin
  2. Add C:\bin to your path
    1. Press Windows + Pause|Break
    2. Advanced system settings > Environment Variables
    3. Edit Path and append “C:\bin;”
  3. Create phpdoc.bat and open it in Notepad
  4. Type php C:\path\to\phpDocumentor.phar %*
  5. Save and Close

The most basic usage would be phpdoc -d . -t ./docs which generates documentation in a folder called “docs” for files starting in the current directory.

How does it work?

Anything in your path can be run from anywhere and you can use %1, %2, %3, etc. to get the values of parameters passed when the batch file is called (each parameter is separated by a space—step four would require 4 parameters for -d, ., -t and ./docs).

%* gets all parameters as passed in one string, which is perfect for making a custom command because you don’t necessarily know the number of parameters beforehand and you’re essentially passing the global command the very parameters you want passed to the non-global command.

Put simply, the phpdoc command passes its parameters to phpdoc.bat, which calls php C:\bin\phpDocumentor.phar followed by said parameters, thus calling php C:\bin\phpDocumentor.phar -d . -t ./docs. Simple!

Other Uses

Composer can be used by following exactly the same method as above with composer.phar. I also have a batch file called serve.bat with short-hand for PHP’s in-built server: php -S localhost:8000 %*. You could either serve a specific file with serve test.php or simply serve for the directory itself!

Leave a Reply

Your email address will not be published.