Weave 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

Getopt Php Laravel Package

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.

View on GitHub
Deep Wiki
Context7

Getting Started

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.

Implementation Patterns

  • Use named constants for option names to avoid typos and enable refactoring.
  • Group related options with OptionSet (if available in version) or by logical sections in code.
  • Leverage Getopt::getNonOptionArgs() for positional arguments (e.g., filenames).
  • Validate option combinations post-parse (e.g., ensure --env is not used with --no-env).
  • Integrate with PSR-3 logging: log unparsed args for debugging: logger()->debug('CLI args', $argv);.
  • Wrap parsing in a try { ... } catch (OptionException $e) { exit($e->getMessage()); } for user-friendly error handling.

Gotchas and Tips

  • Getopt::parse() modifies internal state—don’t call it multiple times with different inputs without re-instantiating.
  • Short options that accept values must be specified as -vfile.yml, not -v file.yml; the space-separated style only works reliably with long options or when explicitly configured.
  • By default, 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.
  • To support -- (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.
  • Unit-test CLI logic by passing custom $argv to Getopt constructor or mocking the argument source.
  • This library doesn’t auto-generate help text—build your own --help handler that prints usage using option definitions.
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.
hsluv/hsluv
seampact/codegen
seampact/hooks
seampact/ir
mehedimi/laravel-amp-mail
happones/laravel-vue-i18n-generator
jwerd/inertia-table
siberfx/flysystem-wasabi
hollyit/token-replace
ippanelcom/laravel-sdk
adachsoft/changelog-linter
adachsoft/collection
payerurl/telegrambot-crypto-payment-instant-payout
dskripchenko/laravel-hybrid-search
riorizkyrainey/lara-ced
mehedimi/static-vel
zharp/zharp
nonsapiens/bigquery-model-sync
lgu-bayambang-ict-office/lgu-laravel-development-standards
andydefer/laravel-support