Download the PHP package aidantwoods/betteroptions without Composer
On this page you can find all versions of the php package aidantwoods/betteroptions. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download aidantwoods/betteroptions
More information about aidantwoods/betteroptions
Files in aidantwoods/betteroptions
Informations about the package betteroptions
BetterOptions
than getopt()
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
Using the command line options configured in the example options.json
/options.yaml
,
and the code in example.php
, the following can be achieved:
If we change the type of --foo from string[]
to string
in options.json
/options.yaml
,
we can instead get
(the first value configured is returned).
If we just call
then the auto-generated response for incompleteness (based on logical groups setup in the data file) is as follows
Using the auto-generated help screen (we have bound this to --help
in the
example code), we can use:
to get the following