Login   Register  
PHP Classes
elePHPant
Icontem

File: example.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Diego Feitosa  >  CLI Parser  >  example.php  >  Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Example
Class: CLI Parser
Parse and extract arguments from the command line
Author: By
Last change: Added note
Date: 2006-07-19 19:07
Size: 3,306 bytes
 

Contents

Class file image Download
<?php
/**
 *+-----------------------------------------------------------------------+
 *| CliParser v0.1 - 19 Jul 2006                                          |
 *+-----------------------------------------------------------------------+
 *|  Diego do Nascimento Feitosa                                          |
 *|  diego@dnfeitosa.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($token2false); // false because it not require an argument
$obj->register($token3false); // 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());


?>