Getopt.php meets your needs.
In addition, its true object-oriented interface helps you write elegant PHP code.
This is well documented and it has no external dependencies.
Functions
- Supports both short (e.g.
-v ) and long (e.g. --version ) options - Aliasing option, i.e. option can have both long and short version
- Collapse brief parameters (e.g.
-abc instead of -a -b -c ) - Aggregate options (e.g.
-vvv ) - Parameters may take optional or required arguments
- Two alternative notations for long options with arguments:
--option value and --option=value - Collapse brief parameters with a required argument at the end (e.g.
-ab 1 instead of -a -b 1 )
Code example
<?php use Ulrichsg\Getopt\Getopt; use Ulrichsg\Getopt\Option; $getopt = new Getopt(array( new Option('m', 'mode', Getopt::REQUIRED_ARGUMENT), new Option('p', 'parents'), new Option('v', 'verbose'), new Option('Z', 'context', Getopt::REQUIRED_ARGUMENT), new Option(null, 'help'), new Option(null, 'version') )); try { $getopt->parse(); if ($getopt['version']) { echo "Getopt example v0.0.1\n"; exit(0); } // Error handling and --help functionality omitted for brevity $createParents = ($getopt['parents'] > 0); // Note that these are null if the respective options are not given $mode = $getopt['mode']; $context = $getopt['context']; $dirNames = $getopt->getOperands(); makeDirectories($dirNames, $createParents, $mode, $context); } catch (UnexpectedValueException $e) { echo "Error: ".$e->getMessage()."\n"; echo $getopt->getHelpText(); exit(1); }
Demis Palma ツ
source share