Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Console Getopt Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

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;

Implementation Patterns

  • Mixed short/long options: Use the second argument for short opts (with : for required params), third for long opts (with = for required params).
  • Error handling: Always check for PEAR::isError($opts) since invalid syntax or missing required args return PEAR_Error.
  • Argv passthrough: Leftover non-option arguments appear in $arguments, useful for positional args like filenames or commands.
  • Integration with CLI apps: Pair with Console_Color2 or Console_Shell for colorful output, especially in CLI tools that need to be POSIX-compliant (e.g., tools mimicking wget, curl).
  • Legacy compatibility: Despite being archived, it remains safe for production due to its minimal surface area—ideal for Composer-based tools where dependency bloat is undesirable.

Gotchas and Tips

  • No named return keys: Options are returned as a numerically indexed array of [ [opt, param], ... ]. You must normalize manually (e.g., using Console_Getopt::getShortOption() or a foreach loop) to build an associative array.
  • Default values: Not built-in—remember to pre-fill defaults in your option struct before parsing.
  • PEAR dependency: Requires 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).
  • Extending functionality: Its simplicity means you’ll likely wrap it in a helper (e.g., getOptSafe($argv) that auto-deduces short/long names).
  • Modern alternatives: Consider symfony/console for full-featured CLIs, but retain Console_Getopt for ultra-lightweight tools (e.g., simple deploy scripts or CI helpers).
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
davejamesmiller/laravel-breadcrumbs
artisanry/parsedown
christhompsontldr/phpsdk
enqueue/dsn
bunny/bunny
enqueue/test
enqueue/null
enqueue/amqp-tools
milesj/emojibase
bower-asset/punycode
bower-asset/inputmask
bower-asset/jquery
bower-asset/yii2-pjax
laravel/nova
spatie/laravel-mailcoach
spatie/laravel-superseeder
laravel/liferaft
nst/json-test-suite
danielmiessler/sec-lists
jackalope/jackalope-transport