ulrichsg/getopt-php
Lightweight PHP library for parsing command-line options and arguments (getopt). Define short/long flags, required/optional values, and get a clean result for building CLI tools. No framework dependencies, simple API, supports help/usage output.
Start by installing via Composer: composer require ulrichsg/getopt-php. Create a simple Getopt instance and define options using Option objects—short (-v), long (--verbose), with or without values (no_value, required_value, optional_value). Parse arguments with Getopt::parse() and access results via Getopt::getOption(). First use case: building a CLI tool that accepts flags like --config=file.yml and -v for verbosity.
OptionSet (if available in version) or by logical sections in code.Getopt::getNonOptionArgs() for positional arguments (e.g., filenames).--env is not used with --no-env).logger()->debug('CLI args', $argv);.try { ... } catch (OptionException $e) { exit($e->getMessage()); } for user-friendly error handling.Getopt::parse() modifies internal state—don’t call it multiple times with different inputs without re-instantiating.-vfile.yml, not -v file.yml; the space-separated style only works reliably with long options or when explicitly configured.Getopt stops parsing at the first non-option argument (e.g., mytool.php -v script.php parses only -v). Use Getopt::setRestHandled() or Getopt::handleOptions() to change this behavior.-- (end-of-options) correctly, call Getopt::setRequireEquals(false) if you allow -f=value vs -f value, and set Getopt::setBundling(true) if you want -vxf to bundle short flags.$argv to Getopt constructor or mocking the argument source.--help handler that prints usage using option definitions.How can I help you explore Laravel packages today?