[PHP] optarg parser für PHP CLI

Dieses Thema im Forum "Webentwicklung" wurde erstellt von Murdoc, 18. Januar 2011 .

Schlagworte:
  1. 18. Januar 2011
    optarg parser für PHP CLI

    falls jemand sowas in der art sucht, hier mein code (php4/php5)

    php4
    Spoiler
    PHP:
    <? php

    define
    ( 'OPTARG_STRING' 1 );
    define ( 'OPTARG_INT' ,     2 );
    define ( 'OPTARG_FLOAT' ,   3 );
    define ( 'OPTARG_FLAG' ,    4 );


    function  optarg_init ( $argc $argv )
    {
        global 
    $____optarg_table ;
        
        
    $____optarg_table  = array();
        
    $____optarg_table [ 'argc' ] =  $argc ;
        
    $____optarg_table [ 'argv' ] = & $argv ;
        
    $____optarg_table [ 'defs' ] = array();
    }


    function  optarg_add ( $parse $type $def , & $var $help  'no help defined' )
    {
        global 
    $____optarg_table ;
        
        if((
    $p  strpos ( $parse '[' )) !==  false ) {
            
    $left  substr ( $parse 0 $p );
            
    $right  $left  substr ( $parse $p  1 , - 1 );
            
            
    $left   str_repeat ( '-' strlen ( $left ) >  1 ) .  $left ;
            
    $right  str_repeat ( '-' strlen ( $right ) >  1 ) .  $right ;
            
            
    $____optarg_table [ 'defs' ][ $left ] = array( $type $def , & $var $help );
            
    $____optarg_table [ 'defs' ][ $right ] = array( $type $def , & $var $help $left );
        } else {
            
    $parse  str_repeat ( '-' strlen ( $parse ) >  1 ) .  $parse ;
            
    $____optarg_table [ 'defs' ][ $parse ] = array( $type $def , & $var $help );
        }
    }


    function  optarg_avail ()
    {
        global 
    $____optarg_table ;
        
        foreach(
    $____optarg_table [ 'defs' ] as  $parse  =>  $opts ) {
            switch(
    $opts [ 0 ]) {
                case 
    OPTARG_STRING :
                    
    $type  'string' ;
                    break;
                    
                case 
    OPTARG_FLAG :
                    
    $type  'flag' ;
                    break;
                    
                case 
    OPTARG_FLOAT :
                    
    $type  'float' ;
                    break;
                    
                default:
                    
    $type  'int' ;
            }
            
            
    $alias  = isset( $opts [ 4 ]) ?  " (alias for:  { $opts [ 4 ]} )"  '' ;
            print 
    " $parse  [ $type ] (default:  { $opts [ 1 ]} ) $alias   { $opts [ 3 ]} \n<br />" ;
        }
    }


    function  optarg_fetch ()
    {
        global 
    $____optarg_table ;
        
        
    $fetched  = array();
        
        foreach(
    $____optarg_table [ 'defs' ] as  $parse  =>  $opts ) {
            if(isset(
    $opts [ 4 ]) &&  in_array ( $opts [ 4 ],  $fetched ))
                continue;
                
            if(
    optarg_arg ( $parse $opts [ 0 ],  $opts [ 1 ],  $opts [ 2 ]) ===  true )
                
    $fetched [] =  $parse ;
        }
    }


    function  optarg_arg ( $parse $type $def , & $var
    {
        global 
    $____optarg_table ;
        
        
    $argc   $____optarg_table [ 'argc' ];
        
    $argv   $____optarg_table [ 'argv' ];
        
    $value  null ;
        
    $found  true ;
        
        for(
    $i  0 $i  $argc ; ++ $i ) {
            if(
    $argv [ $i ] ==  $parse ) {
                if(
    $type  ===  OPTARG_FLAG ) {
                    
    $value  true ;
                    break;
                }
                
                
    $value  = ( $i  $argc ) ?  $argv [++ $i ] :  null ;
                break;
            }
        }
        
        if(
    $value  ===  null ) {
            
    $found  false ;
            
    $value  $def ;
        }
        
        switch(
    $type ) {
            case 
    OPTARG_STRING :
                
    $value  = (string)  $value ;
                break;
                
            case 
    OPTARG_FLAG :
                
    $value  = ( $value  ===  true ) ?  true  false ;
                break;
                
            case 
    OPTARG_INT :
                
    $value  intval ( $value );
                break;
                
            case 
    OPTARG_FLOAT :
                
    $value  floatval ( $value );
                break;
        }
        
        
    $var  $value ;
        
        return 
    $found ;
    }

    php5
    Spoiler
    PHP:
    class  Optarg
    {
        const 
    T_STRING  1 ,
              
    T_INT     2 ,
              
    T_FLOAT   3 ,
              
    T_FLAG    4 ;
        
        protected 
    $argc  0
                  
    $argv  = array(),
                  
    $defs  = array();
        
        

        
    public function  __construct ()
        {
            
    $args  func_get_args ();
            
            if(
    count ( $args ) ==  && ( is_int ( $args [ 0 ]) &&  is_array ( $args [ 1 ])))
                
    $this -> init ( $args [ 0 ],  $args [ 1 ]);
        }   
        
        

        
    public function  init ( $argc , array  $argv )
        {
            
    $this -> argc  $argc ;
            
    $this -> argv  $argv ;
            
    $this -> defs  = array();
        }
        
        

        
    public function  add ( $parse $type $def , & $var $help  'no help defined' )
        {
            if((
    $p  strpos ( $parse '[' )) !==  false ) {
                
    $left  substr ( $parse 0 $p );
                
    $right  $left  substr ( $parse $p  1 , - 1 );
                
                
    $left   str_repeat ( '-' strlen ( $left ) >  1 ) .  $left ;
                
    $right  str_repeat ( '-' strlen ( $right ) >  1 ) .  $right ;
                
                
    $this -> defs [ $left ] = array( $type $def , & $var $help );
                
    $this -> defs [ $right ] = array( $type $def , & $var $help $left );
            } else {
                
    $parse  str_repeat ( '-' strlen ( $parse ) >  1 ) .  $parse ;
                
    $this -> defs [ $parse ] = array( $type $def , & $var $help );
            }
        }
        
        

        
    public function  avail ()
        {
            foreach(
    $this -> defs  as  $parse  =>  $opts ) {
                switch(
    $opts [ 0 ]) {
                    case 
    self :: T_STRING :
                        
    $type  'string' ;
                        break;
                        
                    case 
    self :: T_FLAG :
                        
    $type  'flag' ;
                        break;
                        
                    case 
    self :: T_FLOAT :
                        
    $type  'float' ;
                        break;
                        
                    default:
                        
    $type  'int' ;
                }
                
                
    $alias  = isset( $opts [ 4 ]) ?  " (alias for:  { $opts [ 4 ]} )"  '' ;
                print 
    " $parse  [ $type ] (default:  { $opts [ 1 ]} ) $alias   { $opts [ 3 ]} \n<br />" ;
            }
        }
        
        

        
    public function  fetch ()
        {        
            
    $fetched  = array();
            
            foreach(
    $this -> defs  as  $parse  =>  $opts ) {
                if(isset(
    $opts [ 4 ]) &&  in_array ( $opts [ 4 ],  $fetched ))
                    continue;
                    
                if(
    $this -> arg ( $parse $opts [ 0 ],  $opts [ 1 ],  $opts [ 2 ]) ===  true )
                    
    $fetched [] =  $parse ;
            }
        }
        
        

        
    public function  arg ( $parse $type $def , & $var
        {
            
    $value  null ;
            
    $found  true ;
            
            for(
    $i  0 $i  $this -> argc ; ++ $i ) {
                if(
    $this -> argv [ $i ] ==  $parse ) {
                    if(
    $type  ===  self :: T_FLAG ) {
                        
    $value  true ;
                        break;
                    }
                    
                    
    $value  = ( $i  $this -> argc ) ?  $this -> argv [++ $i ] :  null ;
                    break;
                }
            }
            
            if(
    $value  ===  null ) {
                
    $found  false ;
                
    $value  $def ;
            }
            
            switch(
    $type ) {
                case 
    self :: T_STRING :
                    
    $value  = (string)  $value ;
                    break;
                    
                case 
    self :: T_FLAG :
                    
    $value  = ( $value  ===  true ) ?  true  false ;
                    break;
                    
                case 
    self :: T_INT :
                    
    $value  intval ( $value );
                    break;
                    
                case 
    self :: T_FLOAT :
                    
    $value  floatval ( $value );
                    break;
            }
            
            
    $var  $value ;
            
            return 
    $found ;
        }
    }

    verwenden kann man das ganze so:

    1. direkt
    PHP:
    optarg_init ( $_SERVER [ 'argc' ],  $_SERVER [ 'argv' ]);
    optarg_arg ( '-m' OPTARG_STRING '' $m );

    print 
    $m // gibt das argument "-m" aus
    2. erweitert
    PHP:
    // selbes ergebnis wie in der direkten version
    optarg_init ( $_SERVER [ 'argc' ],  $_SERVER [ 'argv' ]);
    optarg_add ( 'm' OPTARG_STRING '' $m );
    optarg_fetch ();

    print 
    $m ;

    // mit angabe von "alias"
    optarg_init ( $_SERVER [ 'argc' ],  $_SERVER [ 'argv' ]);
    optarg_add ( 'm[essage]' OPTARG_STRING '' $m );
    optarg_fetch ();

    print 
    $m // gibt das argument -m oder --message aus
    beispiel:
    Code:
    php -f "mein_script.php" -- -m "hello world"
    verschiedene typen:
    STRING = lässt den parameter so wie er angegeben wurde
    INT = konvertiert den parameter zu einer ganzzahl
    FLOAT = konvertiert den parameter zu eine gleitkommazahl
    FLAG = setzt die angebene variable auf TRUE wenn der parameter existiert, ansonsten FALSE

    jeder ders braucht sollte sich schnell zurecht finden.
     
  2. Video Script

    Videos zum Themenbereich

    * gefundene Videos auf YouTube, anhand der Überschrift.