CLI script
php-script is a template for a single-file command-line interface (CLI) script. It runs on PHP alone and loads no Composer packages. Use it for a tool that does 1 task. Follow this page to adapt the script, test it and ship it. For a tool with several commands, see CLI command.
To select this mode, pass --php-script to install.sh. Or, at the install.sh prompts, answer n to Use CLI command app and y to Use simple script. install.sh replaces php-script with your project name, or with the value of --php-script-name, in filenames and file contents. This page uses the template name.
What the script handles
php-script already handles the following:
- Shebang: the first line is
#!/usr/bin/env php, so./php-scriptruns with thephpon yourPATH. - Help: when any argument is
help,--help,-hor-?,main()callsprint_help()and returns before the argument check. - Argument check: after the help check,
main()throws an exception unless the script gets exactly 1 argument. - Error handling: a handler set with
set_error_handler()turns PHP errors into exceptions. The entry point, the code at the end of the file, catches an exception frommain(). It prints the message and exits with the exception's code, or1if that code is0. For a deprecation or a user notice, the entry point discards the exception, so the script exits with code0and no message. - Output buffer:
verbose()prints each message and records it in an internal buffer. It returns the buffer, so tests can assert on the output. - CLI only: outside the command line, such as under a web server, the script prints an error and stops.
The script reads 2 environment variables:
| Variable | Effect when set to 1 |
|---|---|
SCRIPT_QUIET | verbose() records messages in its buffer without printing them. |
SCRIPT_RUN_SKIP | The entry point skips main(), so you can require the file and call main() yourself. |
Run the script with 1 argument from the project root:
./php-script value/of/argument
The script prints:
Would execute script business logic with argument value/of/argument.
Script tests
The example tests check the script at 2 levels, each with a base class. Both base classes include AssertArrayTrait from alexskrypnyk/phpunit-helpers, whose assertArrayContainsString() checks output lines.
Unit tests
Unit tests call main() inside the test process. ScriptUnitTestCase sets SCRIPT_RUN_SKIP=1 and SCRIPT_QUIET=1, then loads the script. Its runMain() method calls main() with your arguments and returns the verbose() buffer.
ExampleScriptUnitTest.php runs main() with different arguments and checks the output or the exception.
Functional tests
Functional tests run the script in a separate process. ScriptFunctionalTestCase extends the unit base class and sets both variables back to 0. Its runScript() method runs php php-script with your arguments and returns the exit code and output lines.
ExampleScriptFunctionalTest.php runs the same cases as the unit test and checks each exit code against EXIT_SUCCESS (0) or EXIT_ERROR (1).
For the PHPUnit configuration and coverage reports, see Testing.
Adapt the script
To turn the template into your script:
-
In the header comment, replace
PHP CLI script templatewith your script's name. -
In
main(), set both numbers in theif ($argc < 2 || $argc > 2)check to your argument count plus 1, because$argcalso counts the script name. For 2 arguments, change both2values to3. -
Replace the
// Add your logic here.comment and theverbose()call after it with your code. -
Rewrite the help text in
print_help()to describe your script. -
Delete the
To adopt script template:block from the header comment. -
In
ExampleScriptUnitTest.phpandExampleScriptFunctionalTest.php, update the cases indataProviderMain()to match your script's arguments and output. -
Run the tests:
composer testConfirm that every test passes.
Ship the script
composer.json lists the script under bin, so Composer links it into vendor/bin in projects that require your package. When you push a tag, the Release PHP workflow attaches the script to the GitHub release for the tag.