Login   Register  
PHP Classes
elePHPant
Icontem

File: argv.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Dennis T Kaplan  >  CLI args  >  argv.php  >  Download  
File: argv.php
Role: Class source
Content type: text/plain
Description: Capture command line arguments in to your php script
Class: CLI args
Parse and process command line arguments
Author: By
Last change: Windows note
Date: 4 years ago
Size: 3,252 bytes
 

Contents

Class file image Download
<?php

/**
 * @name      phpargv
 * @package   PHP TaMeR
 * @copyright (C) 2009-2010 Dennis T Kaplan
 * @license   GPL v3 {@link http://www.gnu.org/licenses/gpl.html}
 * @author    Dennis T Kaplan
 * @todo
 */

/**
 * Class    : phpargv
 * Date     : June 17, 2009
 * Purpose  : Capture command line arguments in to your php script
 */


class argv {
    public function 
run($argv) {
        echo 
exec('clear'); // You might have to remove this for windows
        
$this->argv $argv;
        
$this->remove_self();
        
$this->check_help();
        
$this->args_array();
        
$this->check_args();
        return 
$this->argv;
    }

    public function 
set_arg($arg) {
        
$this->arg $arg;
        
$this->args[$this->arg]['required'] = FALSE;
    }

    public function 
set_required($i=FALSE) {
        
$this->args[$this->arg]['required'] = $i;
    }

    public function 
set_value($v) {
        
$this->args[$this->arg]['value'][] = $v;
    }

    public function 
set_text($text) {
        if(!isset(
$this->text)) {
            
$this->text $text;
        }else{
            
$this->text .= PHP_EOL.$text ;
        }
    }

    public function 
args_view() {
        
$r "\n$this->text\n\nOptions:\n";
        
$h '';
        foreach(
$this->args as $arg=>$v) {
            
$s'';
            
$i='Optional';
            if(
$v['required'] === TRUE) {
                
$i "Required";
            }
            if(isset(
$v['value'])) {
                foreach(
$v['value'] as $value) {
                    
$s .= "$value | ";
                }
                
$s=trim($s,'| ');
                
$r.="\t-$arg=[ $s ] <$i>\n";
                
$h .= $arg=$s";
            }
        }
        
$self __FILE__;
        
$r .= "\n\nUsage:\n\t php $self $h\n\n";
        return 
$this->help $r;
    }

    private function 
check_args() {
        if(isset(
$this->argv) && is_array($this->argv)) {
            foreach(
$this->args as $key=>$args){
                foreach(
$args as $k=>$v){
                    if (
$k == 'required' && $v == 1) {
                        if(!isset(
$this->argv[$key][$k])){
                            
$this->create_help();
                            exit;
                        }
                    }
                }
            }
        }else {
            
$this->create_help();
            exit;
        }
    }

    private function 
check_help() {
        if (isset(
$this->argv[1]) && in_array($this->argv[1], array('--help''-help''-h''-?'))) {
            
$this->create_help();
            exit;
        }
    }

    private function 
create_help() {
        
$this->args_view();
        
$h="\n   This is a command line PHP script.\n";
        
$h.= $this->help;
        
$h.="\n\n";
        echo 
$h;
    }

    private function 
remove_self() {
        if(
$this->argv[0] == $_SERVER['PHP_SELF']) {
            unset(
$this->argv[0]);
        }
    }

    function 
args_array() {
        
$t $this->argv;
        unset(
$this->argv);
        foreach(
$t as $v) {
            
$this->argv[trim(strstr($v,'=',true),'- ')] = trim(strstr($v,'='),'= ');
        }
        unset(
$t);
    }
}

?>