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 Olivier Bourdoux  >  CLI Parser extended  >  example.php  >  Download  
File: example.php
Role: Example script
Content type: text/plain
Description: Example script
Class: CLI Parser extended
Get values from command line arguments
Author: By
Last change:
Date: 3 years ago
Size: 4,846 bytes
 

Contents

Class file image Download
<?php
/**
 *+-----------------------------------------------------------------------+
 *| CliParser v0.2 - 19 Oct 2011                                          |
 *+-----------------------------------------------------------------------+
 *|  Olivier Bourdoux                                                     |
 *|  olivier.bourdoux@gmail.com                                           |
 *|  www.xurei-design.be                                                  |
 *|                                                                       |
 *|  Original code from :                                                 |
 *|  Diego do Nascimento Feitosa                                          |
 *|  diego@dnfeitosa.com                                                  |
 *|  www.dnfeitosa.com                                                    |
 *|  São Paulo/SP - Brasil                                               | 
 *+-----------------------------------------------------------------------+
 *| 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                                                       |
 *+-----------------------------------------------------------------------+
**/

require_once('CliParser.inc');

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

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

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

$clibool = new CliTokenBoolean("-b");
$clibool->setDescription("Boolean token");

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

$clifile = new CliTokenDirectory("-f");
$clifile->setDescription("This token requires a file path as argument. If the argument isn't a file path, an error message will appear.");

$cliint = new CliTokenInteger("-i");
$cliint->setDescription("This token requires an integer path as argument. If the argument isn't an integer, an error message will appear.");

$clienum = new CliTokenEnum("-enum", array('the''different''values''accepted'));
$clienum->setDescription("This token requires its argument to be one of the values specified");

class 
MyCliParser extends CliParser 
{
  public function 
getHelpMessage() 
    {
      global 
$argv;
        echo 
sprintf("Usage: %s [options] <file(s)>\nOptions :\n"$argv[0]);
        
$this->getDescriptions();
    echo 
"\n";
        exit;
    }
}

//Building the parser and parsing the arguments
  
$cli = new MyCliParser($_SERVER["argv"]);
  
$cli->register($clihelpfalse); // false because it not require an argument
  
$cli->register($clistring);
  
$cli->register($clisingletonfalse); // false because it not require an argument
  
$cli->register($clibool);
  
$cli->register($clidir);
  
$cli->register($clifile);
  
$cli->register($cliint);
  
$cli->register($clienum);
  
$cli->parse();

//Showing the help message if asked
if ($clihelp->getValue())
{
    
$cli->getHelpMessage();
    exit;
}

//Showing the options
  
var_dump($clistring->getValue());
  
var_dump($clisingleton->getValue());
  
var_dump($clibool->getValue());
  
var_dump($clidir->getValue());
  
var_dump($clifile->getValue());
  
var_dump($cliint->getValue());

//Showing the non options
  
var_dump($cli->getNonOptions());
  
//Some commands : 
//  php example.php --help      => will show the help message
//  php example.php -c foo      => the $clistring token will be set
//  php example.php -e -c foo   => the $clisingleton token will be set
//  php example.php nonoption1 -i 42 => one non-option argument : nonoption1
//  php example.php -i 42 nonoption1 => same thing, different order
//  php example.php -i 42.5          => shows an error : 42.5 is not an integer


?>