pear/console_getopt
PEAR Console_Getopt is a small PHP library for parsing command-line options and arguments. It supports GNU-style short and long flags, handles required/optional values, and provides help-friendly parsing for CLI scripts and tools.
Install via Composer: composer require pear/console_getopt. This package provides a minimal, procedural CLI option parser inspired by PHP’s getopt() but with support for long options, option parameters, and clear error handling. Start by reading the class-level docblock in Console/Getopt.php—it contains concise usage examples. A minimal first use case is parsing options like --verbose -v -o=file.txt:
require 'vendor/autoload.php';
$opts = Console_Getopt::getopt($argv, 'vo:', [
'verbose',
'output='
]);
if (PEAR::isError($opts)) {
die("Error: " . $opts->getMessage() . "\n");
}
list($options, $arguments) = $opts;
: for required params), third for long opts (with = for required params).PEAR::isError($opts) since invalid syntax or missing required args return PEAR_Error.$arguments, useful for positional args like filenames or commands.Console_Color2 or Console_Shell for colorful output, especially in CLI tools that need to be POSIX-compliant (e.g., tools mimicking wget, curl).[ [opt, param], ... ]. You must normalize manually (e.g., using Console_Getopt::getShortOption() or a foreach loop) to build an associative array.PEAR_Core or a PEAR installation unless Composer’s autoloader handles it (it does via PEAR/Exception). Beware of PEAR::isError() checks failing if PEAR isn’t loaded (Composer handles this).getOptSafe($argv) that auto-deduces short/long names).symfony/console for full-featured CLIs, but retain Console_Getopt for ultra-lightweight tools (e.g., simple deploy scripts or CI helpers).How can I help you explore Laravel packages today?