PHPackages                             aidantwoods/betteroptions - 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. aidantwoods/betteroptions

ActiveLibrary

aidantwoods/betteroptions
=========================

Better than `getopt()`

v0.0.0(9y ago)24.1kMITPHPPHP &gt;=7.0

Since Feb 20Pushed 8y ago1 watchersCompare

[ Source](https://github.com/aidantwoods/BetterOptions)[ Packagist](https://packagist.org/packages/aidantwoods/betteroptions)[ RSS](/packages/aidantwoods-betteroptions/feed)WikiDiscussions master Synced 2mo ago

READMEChangelog (1)Dependencies (1)Versions (2)Used By (0)

BetterOptions
=============

[](#betteroptions)

*than [`getopt()`](http://php.net/manual/en/function.getopt.php)*

Synopsis
--------

[](#synopsis)

`getopt()` sucks. Flags (options with no value) are set as false **when they *are* set**, options that are unset by the user will cause a key error if you attempt to look them up. If multiple options are specified you are given an array containing them all, regardless of whether you wanted to recieve an array.

There is no way to specify which type you are expecting. If compulsory options are left unspecified by the user, this will prevent other compulsory options from being parsed, (but will not enforce that these options are present). "long" options are specified in an array, while "short" options are specified by concatenating a string...

All this means that in order to accept command line options, lots (and lots) of ugly validation logic must be manually written. And making use of "long" and "short" options creates unnessesary code to special case the configuration of things that aren't functionally different.

---

Instead, create a data structure file specifying logical (AND, OR, XOR) requirement groups of options, and/or just specify options on their own with no requirements.

I say "data structure file" here because, in theory you can use whatever you like (provided you create an adapter that implements the right interface and register the file extension). The default supported file types are JSON using PHPs native parser, and YAML (if you also install Symfony's YAML parser from require dev in composer.json).

The expected type of an option can be specified, and this is the type you will recieve (or `null` if not set). Optionally specify a default value (you can still query whether the user set the variable or the default was loaded without having to hard code in a comparison to the default).

If you're not expecting multiple options you won't get an array. If you want to accept multiple options, but can cope with one just fine you can recieve an array with one item in (types like `string[]`, `bool[]`, `integer[]` are arrays containing a certain type).

You can document your options in the data structure file too, as well as specifying whether you want a 'long' or 'short' (two or one dash) options without changing the structure of specification.

Aliases can also be defined (e.g. `--file`, `-f`). You can make as many as you like (though two is almost always sufficient).

You can recieve responses from groups if their logical conditions fail so you don't have to write error messages yourself, and you can even use an auto-generated help screen populated with the description of each option from the data structure file.

Example
-------

[](#example)

Using the command line options configured in the example `options.json`/`options.yaml`, and the code in `example.php`, the following can be achieved:

```
$ php example.php --foo=asda --foo=sdsg --bar=dsgs
```

```
foo:
array(2) {
  [0]=>
  string(4) "asda"
  [1]=>
  string(4) "sdsg"
}

bar:
string(4) "dsgs"

baz:
NULL

boo:
NULL

help:
NULL
```

If we change the type of --foo from `string[]` to `string` in `options.json`/`options.yaml`, we can instead get

```
foo:
string(4) "asda"
```

(the first value configured is returned).

If we just call

```
$ php example.php
```

then the auto-generated response for incompleteness (based on logical groups setup in the data file) is as follows

```
At least one of {--foo or --bar or {--baz xor --boo}} must be set

```

Using the auto-generated help screen (we have bound this to `--help` in the example code), we can use:

```
$ php example.php --help
```

to get the following

```
--foo, -fooalias        Foo.
--bar
--baz                   Lorem ipsum dolor sit amet, consectetur adipiscing
                        elit. Ut fermentum tristique enim quis consequat.
                        Sed dignissim mi in erat gravida, sed interdum
                        mauris iaculis. Quisque nulla dolor, sollicitudin
                        quis pretium non, venenatis eu tortor. Mauris eget
                        enim leo. Etiam pretium semper leo, eu elementum
                        lacus luctus in. Cras elit ex, rhoncus ac gravida
                        ac, pulvinar porttitor sapien. Ut vel purus
                        sollicitudin, finibus est ac, imperdiet elit. Ut
                        sollicitudin at nunc et dapibus.

--boo
--help                  Print this help dialogue

```

###  Health Score

26

—

LowBetter than 43% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity20

Limited adoption so far

Community7

Small or concentrated contributor base

Maturity48

Maturing project, gaining track record

 Bus Factor1

Top contributor holds 100% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Unknown

Total

1

Last Release

3365d ago

### Community

Maintainers

![](https://www.gravatar.com/avatar/dbb49e6e5041af70c7a518aed76ce2f38091eeae6f14febc2e54f71cfa8f7738?d=identicon)[aidantwoods](/maintainers/aidantwoods)

---

Top Contributors

[![aidantwoods](https://avatars.githubusercontent.com/u/3288888?v=4)](https://github.com/aidantwoods "aidantwoods (23 commits)")

---

Tags

phpoptionsgetoptbetterbetteroptions

### Embed Badge

![Health badge](/badges/aidantwoods-betteroptions/health.svg)

```
[![Health](https://phpackages.com/badges/aidantwoods-betteroptions/health.svg)](https://phpackages.com/packages/aidantwoods-betteroptions)
```

###  Alternatives

[aura/cli

Provides the equivalent of request (Context) and response (Stdio) classes for a command line environment, including Getopt support.

1051.6M29](/packages/aura-cli)[sandrokeil/interop-config

Provides interfaces and a concrete implementation to create instances depending on configuration via factory classes and ensures a valid config structure. It can also be used to auto discover factories and to create configuration files.

58446.7k34](/packages/sandrokeil-interop-config)[pestphp/pest-plugin-stressless

Stressless plugin for Pest

67792.6k16](/packages/pestphp-pest-plugin-stressless)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
