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