Download the PHP package jdwx/args without Composer
On this page you can find all versions of the php package jdwx/args. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Package args
Short Description A simple PHP library for parsing command line arguments.
License MIT
Informations about the package args
jdwx/args
PHP library for handling command-line arguments
It's designed to provide methods for safely retrieving most common argument types, including strings, integers, floating point values, boolean flags, sets of keywords, filenames, hostnames, IP addresses, and email addresses.
It's useful for handling arguments presented to a PHP script on the shell command line, e.g.:
Or it could refer to the arguments on a command entered into a PHP-based REPL, such as a custom management tool.
Installation
You can require it directly with Composer:
Or download the source from GitHub: https://github.com/jdwx/args.git
Requirements
This library requires PHP 8.2 or later. It might work with earlier versions of PHP 8, but it has not been tested with them.
Usage
All "shift" methods throw an exception if an argument exists but is invalid:
The default form of such methods returns null if no more arguments are present, which is useful for iterating in a while loop:
The shift methods also provide a variant that throws an exception if no more arguments are present. This is useful for ensuring that a required argument is present:
It is also possible to "peek" at the next argument without necessarily consuming it. This is supported for strings that match a certain prefix:
or for strings in set of keywords:
`
The library also provides handling for optional Gnu-style arguments that begin with two hyphens (e.g., --example):
The handleOptions() method supports both boolean flags (e.g., --flag and --no-flag) and arguments that require a value (e.g., --key=value).
The handleOptions() method removes parsed arguments from the list, allowing remaining arguments to be handled as normal.
It does not support short options (e.g., -h) or options that require a value to be specified in the next argument (e.g., -k value).
Stability
This library is considered stable and is used in production code. Additional parsing methods may be added in the future, but existing methods should not be removed or changed in a way that breaks backwards compatibility. Exceptions to address security issues or bugs may occur, but are expected to be rare.
History
This library has been in use for many years. It was refactored out of a larger codebase in 2023 and was first released as a standalone library in 2024.