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

  1. Your PHP CLI script is invoked with command line arguments
$ php myprogram.php --message='Hello World!'

Argh Interprets Command Line Arguments

  1. Create a new Argh instance.
  2. Define any parameters you want your script to accept as command line arguments.
  3. Pass PHP's $argv[] array as an argument.
  1. // Create a new Argh instance
  2. $argh = new Argh(
  3. [
  4. StringParameter::createWithAttributes( [ 'name' => 'message' ] )
  5. ]
  6. );
  7.  
  8. // Argh interprets PHP's $argv[] array using the Parameters you defined
  9. $argh->parseArguments($argv);

Access Run-Time Values of Command Line Arguments

  1. // Access command line arguments as properties of Argh
  2. 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)

  1. Download argh.phar from the Argh Github releases page.
  2. Copy the argh.phar (PHP archive) into your include_path
  3. Import the phar in your PHP CLI script with require('argh.phar')

Detailed Install Instructions

See the Argh wiki for Argh install instructions.

Documentation

  1. README from the Argh Github project page.
  2. Detailed documentation in the Argh Github Wiki
  3. PHPDocs Coming Soon...