PHPackages                             douglasgreen/opt-parser - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [CLI &amp; Console](/categories/cli)
4. /
5. douglasgreen/opt-parser

ActiveProject[CLI &amp; Console](/categories/cli)

douglasgreen/opt-parser
=======================

Command-line option parser for PHP

v0.7.4(4mo ago)0161↓90%MITPHPPHP &gt;=8.3

Since Jul 23Pushed 4mo ago1 watchersCompare

[ Source](https://github.com/douglasgreen/opt-parser)[ Packagist](https://packagist.org/packages/douglasgreen/opt-parser)[ Docs](https://github.com/douglasgreen/opt-parser)[ RSS](/packages/douglasgreen-opt-parser/feed)WikiDiscussions main Synced 2d ago

READMEChangelog (8)Dependencies (4)Versions (9)Used By (0)

OptParser
=========

[](#optparser)

A POSIX.1-2017 compliant command-line option parser for PHP with GNU extensions.

Overview
--------

[](#overview)

OptParser implements the [IEEE Std 1003.1-2017 (POSIX.1-2017)](https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap12.html)utility conventions, supporting:

- **Standard option syntax**: Short options (`-a`), clustering (`-abc`), and arguments (`-a value`or `-a=value`)
- **GNU extensions**: Long options (`--option`, `--option=value`) for enhanced readability
- **Strict separation**: stdout for data, stderr for diagnostics
- **Standard exit codes**: `0` (success), `2` (usage error), `130` (SIGINT), etc.
- **Type safety**: Built-in validation for 20+ data types (paths, dates, networks, etc.)

Unlike PHP's built-in `getopt()`, OptParser supports positional arguments (terms), subcommands, automatic help generation, and strict validation with descriptive error messages.

Installation
------------

[](#installation)

```
composer require douglasgreen/opt-parser
```

POSIX Compliance
----------------

[](#posix-compliance)

This library adheres to POSIX.1-2017 Section 12.2 (Utility Syntax Guidelines) and XCU Section 12.2 (Utility Conventions):

### Option Syntax

[](#option-syntax)

SyntaxDescriptionExample`-a`Short option`-v` (verbose)`-abc`Clustered options (equivalent to `-a -b -c`)`-vrf` (verbose, recursive, force)`-a value`Option with argument (space-separated)`-o output.txt``-a=value`Option with argument (equals-separated)`-o=output.txt``--long`Long option (GNU extension)`--verbose``--long=value`Long option with argument`--output=file.txt``--`Option terminator (remaining args are operands)`-- -filename-starting-with-dash`**Important**: When using clustered options, if the last option in a cluster requires an argument, the remaining characters are interpreted as the argument. For example, if `-o` requires an argument, `-abcovalue` is equivalent to `-a -b -c -o value`.

### Exit Codes

[](#exit-codes)

OptParser uses standard exit codes per `sysexits.h` and POSIX conventions:

CodeMeaningUsage`0``EXIT_SUCCESS`Successful execution`1``EXIT_FAILURE`General error (catchall)`2``EX_USAGE`CLI usage error (invalid arguments, unknown options)`126`-Command invoked cannot execute (permission denied)`127`-Command not found`130`-Fatal error signal (`128 + SIGINT(2)`)### Stream Handling

[](#stream-handling)

- **stdout**: Primary data output (results, matched options, machine-readable formats)
- **stderr**: Diagnostic messages (errors, warnings, progress, help text when explicitly requested via error)

### Signal Handling

[](#signal-handling)

Long-running operations should handle `SIGINT` (Ctrl+C) gracefully, performing cleanup and exiting with status `130`.

Usage Guide
-----------

[](#usage-guide)

### Basic Program Structure

[](#basic-program-structure)

```
#!/usr/bin/env php
