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
General Argument Handling
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:
`
Parsing Arguments From Strings
While the command line is the most common place where arguments are encountered, the library also provides functionality for processing arbitrary strings into lists of arguments, including robust handling of quoting and escaped characters:
The ParsedString class provides a lower-level interface for interacting with parsed strings, including $variable substitution and implementing `backtick` replacement through callbacks. See the parser.php file in the examples directory for a simple example.
Handling Options
The library also provides handling for optional Gnu-style arguments that begin with two hyphens. It supports both boolean flags (e.g., --flag and --no-flag) and options that require a value (e.g., --key=value). It does not support short options (e.g, "-h") or options that require a value to be specified in the next argument (e.g., "--key value").
If you only have one flag, you can use the Option class directly.
If you prefer to do it in one line of code, there are static methods:
If you don't want to bother predefining options, you can also extract everything that looks like one from the arguments:
Handling options through any of these interfaces removes them from the argument list, allowing remaining arguments to be processed without further interference.
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.