Location: PHPKode > scripts > CLI Parser class > cli-parser-class/example.php
<?php
/**
 *+-----------------------------------------------------------------------+
 *| CliParser v0.1 - 19 Jul 2006                                          |
 *+-----------------------------------------------------------------------+
 *|  Diego do Nascimento Feitosa                                          |
 *|  hide@address.com                                                  |
 *|  www.dnfeitosa.com                                                    |
 *|  São Paulo/SP - Brasil                                               | 
 *|  The latest version can be found at www.dnfeitosa.com                 | 
 *+-----------------------------------------------------------------------+
 *| CliParser is free software; you can redistribute it and/or modify     |
 *| it under the terms of the GNU General Public License as published by  |
 *| the Free Software Foundation; either version 2 of the License, or     |
 *| (at your option) any later version.                                   |
 *|                                                                       |
 *| CliParser is distributed in the hope that it will be useful, but      |
 *| WITHOUT ANY WARRANTY; without even the implied warranty of            |
 *| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     |
 *| General Public License for more details.                              |
 *|                                                                       |
 *| You should have received a copy of the GNU General Public License     |
 *| along with CliParser; if not, write to the Free Software              |
 *| Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA              |
 *| 02111-1307  USA                                                       |
 *+-----------------------------------------------------------------------+
**/


// An example of how to extend the CliToken class can be found in the CliTokenExample.inc file
function __autoload($class) {
	require_once("$class.inc");
}

$token1 = new CliTokenString("-c");
$token1->setDescription("It requires a string");

$token2 = new CliTokenBoolean("-h");
$token2->setDescription("Token that shows a help message");

$token3 = new CliTokenBoolean("-e");
$token3->setDescription("It don't require any value. The existence of this argument is enough");

$token4 = new CliTokenDirectory("-d");
$token4->setDescription("This token require a directory path as argument. If the argument isn't a path, an error message will appear.");



class AppCliParser extends CliParser {
	public function undefinedTokenError($token) {
		echo sprintf("Unknown option %s\n", $token);
	}

	public function getHelpMessage() {
		echo sprintf("Usage: application [options]...\n");
		$this->getDescriptions();
		exit;
	}
}

//$args = array("filename.php", "-c", "teste", "-e", "-d");
//$obj = new AppCliParser($args);
$obj = new AppCliParser($_SERVER["argv"]);
$obj->register($token1);
$obj->register($token2, false); // false because it not require an argument
$obj->register($token3, false); // false because it not require an argument
$obj->register($token4);
$obj->parse();

if ($token2->getValue()) {
	$obj->getHelpMessage();
}

var_dump($token1->getValue());
var_dump($token2->getValue());
var_dump($token3->getValue());
var_dump($token4->getValue());


?>
Return current item: CLI Parser class