Download the PHP package fostam/getopts without Composer

On this page you can find all versions of the php package fostam/getopts. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.

FAQ

After the download, you have to make one include require_once('vendor/autoload.php');. After that you have to import the classes with use statements.

Example:
If you use only one package a project is not needed. But if you use more then one package, without a project it is not possible to import the classes with use statements.

In general, it is recommended to use always a project to download your libraries. In an application normally there is more than one library needed.
Some PHP packages are not free to download and because of that hosted in private repositories. In this case some credentials are needed to access such packages. Please use the auth.json textarea to insert credentials, if a package is coming from a private repository. You can look here for more information.

  • Some hosting areas are not accessible by a terminal or SSH. Then it is not possible to use Composer.
  • To use Composer is sometimes complicated. Especially for beginners.
  • Composer needs much resources. Sometimes they are not available on a simple webspace.
  • If you are using private repositories you don't need to share your credentials. You can set up everything on our site and then you provide a simple download link to your team member.
  • Simplify your Composer build process. Use our own command line tool to download the vendor folder as binary. This makes your build process faster and you don't need to expose your credentials for private repositories.
Please rate this library. Is it a good library?

Informations about the package getopts

fostam/getopts

Flexible PHP command line argument parser with automated help message generation and validation support.

Features

Install

The easiest way to install GetOpts is by using composer:

Usage

Depending on the given options and arguments, the output could look like this ($> is the command line prompt).

Option and argument values:

Help text:

Missing arguments:

Option Configuration

To add a new option to the GetOpts handler, the addOption() method is called. It takes the internal name for that option as argument. This internal name is later used to reference the option value in the parse result and must be unique among all option and argument names.

addOption() returns a new Option object that can be configured further by calling methods. Each configuration method again returns the option object itself, so that the calls can be chained.

If mismatching option configurations are called or naming constraints are not met, an OptionConfigException exception is thrown.

Short Name

This sets the short name for that option. Only a single alphanumeric character is allowed for the short name. In this example, the flag -f would match this option.

Note that different short options can be passed both individually and collapsed, e.g. -a -b -c or -abc, or any combination in between.

Long Name

The long name for an option can consist of one or more alphanumeric characters, including _and -. In this example, passing --filename would match the option.

For each option, either the long name, the short name or both need to be configured.

Description

For the automatic help text generation, an optional description can be given:

This help text would be generated for that option:

Option Argument

Specifies that this option requires an additional argument, e.g. -f myfile.txt.

The name that is passed to argument() is used for the automatic Usage string/help text generation. It is used in it's uppercase version.

Parsing will fail if the argument is missing.

If the option has a long name, the argument can be passed both with and without =, i.e. either --file file1.txt or --file=file1.txt.

Multiple Occurrences

Options that require an argument can be configured so that they can be given multiple times. In that case the option will always return an array as value, even if given only once.

A call passing -f file1.txt -f file2.txt would result in this result array:

Incrementable

If the value of an option should be incremented each time it is provided, it can be set to incrementable:

-v -v -v or -vvv would thus result in the value 3 for the verboseLevel option.

The default increment of 1 can be changed by passing an integer to incrementable():

Incrementable options cannot have option arguments (i.e. incrementable() and argument()/multiple() are mutual exclusive).

Required

This will make the option mandatory. Parsing will fail if it is not provided.

Default Value

By default, an option that is not provided will be null in the result array. This behaviour can be changed by configuring an optional default value:

Negatable

Flag options (i.e. options without argument and not incrementable) can be made negatable when they have a long name. To negate the option, the long name is prefixed with no.

For --test, the result value will be true as usual, but for --notest, the result value will be false. This is particularly useful in combination with a default value of true.

Validation

An optional validator function can be specified for an option:

validator() takes any callable as option, e.g. a function, a closure, a class/object method etc. The callable is passed the option value as argument and must return either true or false, depending on the validation result.

If any option validator returns false, option parsing fails.

Argument Configuration

Argument configuration works similarly to option configuration. To add an argument, the handler's addArgument() method is called, returning an Argument object:

Like Option, the Argument configuration methods are chainable. The argument's name must be unique among all option and argument names

If mismatching argument configurations are called or naming constraints are not met, an ArgumentConfigException exception is thrown.

Name

This name is used for creating the automated Usage string. If not provided, "ARGUMENT" is used as default. If you are relying on the automated Usage string generation, you should always configure name, so that the created text is user-friendly.

Multiple Occurrences

An argument can be configured to be passable multiple times. To avoid ambiguous configurations, only the last argument can be configured that way.

In the result array, this argument will always return an array.

Default Value

Non-mandatory arguments that are omitted will be null in the result object. When configuring a default value, this value is returned instead:

Required

This will make the argument mandatory. To avoid ambiguity, a required argument cannot be preceded by non-mandatory arguments.

Validation

Argument validation works in the same way as option validation:

The given callable is passed the argument value as input and must return true or false.

Parse Results

Generic Getter

or

With the generic get() method, any result - option or argument - can be retrieved. Without argument, an associative array containing both option and argument results is returned. When passing a name, the corresponding option or argument value is returned. If the name has not been configured, a LogicException is thrown.

See Options and Arguments for details about the result values.

Options

Each option that has been configured will be present as key in the result array, regardless whether it was given or not. The value of each option depends on the configuration:

Arguments

Each argument that has been configured will be present as key in the result array, regardless whether it was given or not. The value of each argument is null if not given at all, or the optional default value if configured. Otherwise, the value is a string.

Script Name

The script name is the name of the PHP script that has been executed:

Parse Errors

If the input argument parsing fails, e.g. because requirement constraints have not been met or argument validators have returned false, argument processing is stopped immediately and the following steps are executed:

If required, the automated error handling can be turned off. See Advanced Features for details.

Help Text

When the default help option (short -h or long --help) is given, argument processing is stopped immediately and the following steps are executed:

The short and long help option names can be customized or deactivated completely. See Advanced Features for details.

Advanced Features

Input Arguments

By default, input arguments are taken from $_SERVER['argv']. If you don't want that - e.g. because you are using a framework that provides script arguments by other means - you can override the default behaviour and pass an optional custom input argument array to the handler's parse() method.

The array must consist of strings, where each string represents exactly one argument to the script. The arguments are taken from the array by it's internal order; the array's keys are ignored.

IMPORTANT: the first element of the array must be the script name of the executed PHP script. This is the value returned by $handler->getScriptName(). If you don't have or need the script name, put an empty string as first element.

Error Handling

If you don't want the automated error handling to take over, e.g. because you want to keep control about when your script is exiting, or you want to provide error messages in a different language, you can disable the default behaviour:

When disabled, the $handler->parse() method throws one of the following UsageException exceptions in case of a parse error:

The exception's message is the error text that is printed when using the automated error handling. Some of the exceptions have additional methods to retrieve data that can be used to create more detailed custom error messages.

You can still get the automated Usage string:

Help Options

An additional custom help text can be set that is printed below the automated help text. The custom text should be terminated with a line break (PHP_EOL).

When a lot of options have been configured, the Usage string can become quite long. If you rather want a short Usage string without option details, the terse Usage string can be enabled. Options will then be represented by "[OPTION] ..." in the string.

With the following handler methods, the options of the in-built help function can be changed:

By setting both values to the empty string or false, the help text feature can be disabled. In that case, the help hint on parse errors will no longer be shown, either.

You can still access the generated help text:

License

fostam/GetOpts is published under the MIT license. See License File for more information.


All versions of getopts with dependencies

PHP Build Version
Package Version
Requires php Version >=5.6
Composer command for our command line client (download client) This client runs in each environment. You don't need a specific PHP version etc. The first 20 API calls are free. Standard composer command

The package fostam/getopts contains the following files

Loading the files please wait ....