Download the PHP package devsrealm/tonics-console without Composer
On this page you can find all versions of the php package devsrealm/tonics-console. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download devsrealm/tonics-console
More information about devsrealm/tonics-console
Files in devsrealm/tonics-console
Package tonics-console
Short Description Tonics Console is a PHP-based command-line argument processing library. It allows you to easily parse and handle command-line arguments in your PHP applications.
License MIT
Informations about the package tonics-console
Tonics Console
Tonics Console is a PHP-based command-line argument processing library. It allows you to easily parse and handle command-line arguments in your PHP applications.
Installation
To install Tonics Console, you can use Composer:
Usage
Basic Example
Here's a basic example of how to use the ProcessCommandLineArgs class to parse command-line arguments:
Output
The above code will output:
Here is an additional example demonstrating how to use the ProcessCommandLineArgs class to handle different types of command-line arguments:
Advanced Example
This example shows how to handle both required and optional arguments, as well as how to check if the arguments were passed correctly.
Output
The above code will output:
This example demonstrates how to parse and access individual command-line arguments, including handling optional flags like -v.
Advanced Features
- Repeated keys (both required
--keyand optional-k) are supported. A single occurrence remains a string (backward compatible); multiple occurrences become an array. - Wildcard matching for required patterns: commands can declare
--env*to match any--env:subcommandkey. - ArgsHelper utility provides convenient methods for checking and retrieving arguments.
Repeated Keys Example
Parsed structure snippet:
Wildcard Required Patterns in Commands
A command can express its requirements with wildcards:
ArgsHelper Quick Reference
Optional Description Interface
For commands that want to expose metadata without affecting behavior, implement:
name(): stringdescription(): stringusage(): string
Command Specificity and Matching
When multiple commands have overlapping requirements, the console automatically matches the most specific command first (the one with the most required arguments). This ensures correct behavior when you have commands with subset/superset requirements.
Example:
When you run:
The system will match MigrateAllFresh (2 required args) instead of MigrateAll (1 required arg), ensuring the correct command executes regardless of registration order.
When you run:
The system will match MigrateAll since MigrateAllFresh is missing the --fresh requirement.
Class Details
ProcessCommandLineArgs
This class is responsible for processing command-line arguments.
Constructor
- $args: An array of command-line arguments.
Methods
-
processArgs($args): array
Filters and processes the command-line arguments.
-
passes(): bool
Checks if there are any valid arguments passed.
-
getProcessArgs(): array
Returns the processed arguments.
Testing with Kahlan
Specs are located in the spec/ directory. Run the test suite with:
Kahlan coverage/reporters are not required for this project; the default output is sufficient.
License
This project is licensed under the MIT License. See the LICENSE file for details.
All versions of tonics-console with dependencies
ext-intl Version *
ext-fileinfo Version *
ext-mbstring Version *