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
- Download
phpDocumentor.pharfrom phpdoc.org toC:\bin - Add
C:\binto your path- Press Windows + Pause|Break
- Advanced system settings > Environment Variables
- Edit
Pathand append “C:\bin;”
- Create
phpdoc.batand open it in Notepad - Type
php C:\path\to\phpDocumentor.phar %* - 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!