Argh!

Argument Helper for PHP CLI
Effortlessly parse arguments to your PHP command line interface scripts.
Argh! What's it good for?
- Argh makes it easy to handle command line arguments in PHP command line scripts
- Works will all types of command line arguments (e.g. commands, flags, variables, lists)
- Forget about
$argv[]
, Argh interprets command line input and automatically sets the values of any parameters you define. - Access the run-time values of command line arguments with convenient object sytax
$argh->somevar
- Special requirements? Define your own argument syntax and let Argh do the rest.
- Create usage guide for your PHP scripts automatically.
Argh Understands All These Types of Arguments
Argh comes preconfigured to understand lots of different types of command line arguments.
Custom rules can be defined by their syntax and semantical meaning to fit your specific requirements.
-v |
Hyphenated Boolean Flag |
-xvf |
Hyphenated Multiple Boolean Flag |
-o /path/to/file.txt |
Hyphenated Flag with Value |
-m 'Hello World!' |
Hyphenated Flag with Quoted Value |
-f [one, two, three] |
Hyphenated Flag with List |
-verbose |
Hyphenated Boolean Parameter Name |
-out /path/to/file.txt |
Hyphenated Parameter with Value |
--verbose |
Double Hyphenated Boolean Parameter Name |
--out=/path/to/file.txt |
Double Hyphenated Parameter Name with Value |
--message='Hello World!' |
Double Hyphenated Parameter Name with Quoted Value |
--colors=[blue, green, 'blue green'] |
Double Hyphenated Parameter Name with List |
v |
Unmarked Boolean Flag |
xvf |
Unmarked Multiple Boolean Flags |
somefile.txt '/path with spaces/file.txt' |
Unmarked Variables |
help |
Commands |
Compound Arguments
Any number of arguments can be used together in any order
$ php myprogram.php -xvf --colors=[blue, green, 'blue green'] /path/to/file.txt
Basic Usage Example
Command Line Arguments Passed to Your PHP Script
- Your PHP CLI script is invoked with command line arguments
$ php myprogram.php --message='Hello World!'
Argh Interprets Command Line Arguments
- Create a new Argh instance.
- Define any parameters you want your script to accept as command line arguments.
- Pass PHP's $argv[] array as an argument.
- // Create a new Argh instance
- $argh = new Argh(
- [
- StringParameter::createWithAttributes( [ 'name' => 'message' ] )
- ]
- );
- // Argh interprets PHP's $argv[] array using the Parameters you defined
- $argh->parseArguments($argv);
Access Run-Time Values of Command Line Arguments
- // Access command line arguments as properties of Argh
- echo $argh->message; // Hello World!
More Examples
See the Argh wiki for more Argh usage examples.
Installing Argh
Composer
$ composer require netfocusinc/argh $ php vendor/netfocusinc/argh/bin/argh.php about Argh! by Benjamin Hough, Net Focus Inc.
Phar (PHP Archive)
- Download argh.phar from the Argh Github releases page.
- Copy the argh.phar (PHP archive) into your
include_path
- Import the phar in your PHP CLI script with
require('argh.phar')
Detailed Install Instructions
See the Argh wiki for Argh install instructions.
Documentation
- README from the Argh Github project page.
- Detailed documentation in the Argh Github Wiki
- PHPDocs Coming Soon...