oGetopt
[ class tree: oGetopt ] [ index: oGetopt ] [ all elements ]

Source for file oGetopt.php

Documentation is available at oGetopt.php

  1. <?php
  2. /**
  3.  * Project:    oGetopt :: OOPS C library (olibc)의 o_getopt wrapping api<br>
  4.  * File:    oGetopt.php<br>
  5.  * Dependency: {@link ePrint} over 1.0.1
  6.  *
  7.  * OOPS C library의 o_getopt wrapping Class
  8.  * 이 패키지는 getopt function의 대안을 제공한다.
  9.  *
  10.  * 이 패키지는 {@link ePrint} 1.0.1 이상 버전이 필요하다.
  11.  * 
  12.  * @category    Core
  13.  * @package        oGetopt
  14.  * @author        JoungKyun.Kim <http://oops.org>
  15.  * @copyright    (c) 2018, OOPS.org
  16.  * @link        http://pear.oops.org/package/oGetopt
  17.  * @since        File available since relase 1.0.0
  18.  * @example     oGetopt/test.php 샘플 예제 코드
  19.  * @filesource
  20.  */
  21.  
  22. /**
  23.  * import ePrint class
  24.  * @see ePrint
  25.  */
  26. require_once 'ePrint.php';
  27.  
  28. if extension_loaded ('gettext') ) {
  29.     function _($sreturn $s}
  30. }
  31.  
  32. /**
  33.  * Base classes for oops getopt
  34.  * @package        oGetopt
  35.  */
  36. class oGetopt extends ePrint {
  37.     // {{{ properties
  38.     /**#@+
  39.      * @access public
  40.      */
  41.     /**
  42.      * 현재 옵션 값
  43.      * @var string 
  44.      */
  45.     static public $optarg;
  46.     /**
  47.      * 옵션이 아닌 명령행 인자가 할당되는 배열
  48.      * @var array 
  49.      */
  50.     static public $optcmd;
  51.     /**
  52.      * long 옵션 매핑 테이블
  53.      * @var stdClass 
  54.      */
  55.     static public $longopt;
  56.     /**
  57.      * 옵션이 아닌 명령행 인자의 수
  58.      * @var integer 
  59.      */
  60.     static public $optcno;
  61.     /**#@-*/
  62.  
  63.     /**#@+
  64.      * @access private
  65.      */
  66.     /**#@+
  67.      * @var integer
  68.      */
  69.     /**
  70.      * Set 1, option command argument parsing is end
  71.      */
  72.     static private $optend;
  73.     /**
  74.      * command argument order
  75.      */
  76.     static private $gno;
  77.     /**#@-*/
  78.     /**#@-*/
  79.     // }}}
  80.  
  81.     // {{{ constructor
  82.     /**
  83.      * @access public
  84.      * @return void 
  85.      */
  86.     function __construct ({
  87.         self::init ();
  88.  
  89.         $this->gno     &self::$gno;
  90.         $this->optcno  = &self::$optcno;
  91.         $this->optend  &self::$optend;
  92.         $this->optarg  = &self::$optarg;
  93.         $this->optcmd  = &self::$optcmd;
  94.         $this->longopt = &self::$longopt;
  95.     }
  96.     // }}}
  97.  
  98.     // {{{ public function init ()
  99.     /**
  100.      * 인자의 순서를 초기화
  101.      *
  102.      * @access public
  103.      * @return void 
  104.      */
  105.     static public function init ({
  106.         self::$gno     = -1;
  107.         self::$optcno  = -1;
  108.         self::$optend  = -1;
  109.  
  110.         self::$optarg  '';
  111.         self::$optcmd  array ();
  112.         self::$longopt new stdClass;
  113.     }
  114.     // }}}
  115.  
  116.     // {{{ public function exec ($argc, $argv, $optstrs)
  117.     /**
  118.      * getopt 실행
  119.      *
  120.      * @access public
  121.      * @return string short option을 반환.
  122.      *
  123.      *                 false를 반환할 경우, getopt 수행이 완료 되었음을 의미.
  124.      *                 잘못된 옵션이 있을 경우, 에러 메시지 출력 후 null 반환.
  125.      *
  126.      * @param  integer 명령행 인자 수
  127.      * @param  array   명령행 인자 배열
  128.      * @param  string  옵션 형식.
  129.      *                  '{@link http://man.kldp.net/wiki/ManPage/getopt.3 man 3 getopt}'
  130.      *                  참조
  131.      */
  132.     static public function exec ($argc$argv$optstrs{
  133.         if self::$gno self::$gno 1;
  134.         if self::$optcno self::$optcno 0;
  135.         if isset (self::$optcno|| self::$optend )
  136.             self::$optend 0;
  137.         self::$optarg '';
  138.  
  139.         $errMark parent::asPrintf ('white'_('ERROR'));
  140.  
  141.         while true {
  142.             if self::$gno == $argc )
  143.                 return false;
  144.  
  145.             // {{{ case by long option
  146.             if preg_match ('/^--[a-z]/i'$argv[self::$gno&& self::$optend {
  147.                 $longops explode ('='$argv[self::$gno]);
  148.                 $longname trim (substr ($longops[0]2));
  149.                 self::$optarg trim ($longops[1]);
  150.  
  151.                 $errArg array ($errMark$longname);
  152.                 if ($opt self::$longopt->$longname) ) {
  153.                     parent::ePrintf (_("%s: option --%s don't support")$errArg);
  154.                     return null;
  155.                 }
  156.  
  157.                 if preg_match ("/{$opt}:/"$optstrs) ) {
  158.                     self::$optarg self::$optarg self::$optarg $argv[self::$gno 1];
  159.                     if trim (self::$optarg) ) {
  160.                         parent::ePrintf (_('%s: option --%s must need values')$errArg);
  161.                         return null;
  162.                     }
  163.  
  164.                     if preg_match ('/=/'$argv[self::$gno]) ) self::$gno++;
  165.                 }
  166.                 break;
  167.             }
  168.             // }}}
  169.             // {{{ case by short option
  170.             else if preg_match ('/^-[a-z]/i'$argv[self::$gno&& self::$optend {
  171.                 $opt $argv[self::$gno][1];
  172.                 $optvalue_c $argv[self::$gno][2];
  173.                 $errArg array ($errMark$opt);
  174.  
  175.                 if preg_match ("/{$opt}:/"$optstrs) ) {
  176.                     if $optvalue_c )
  177.                         self::$optarg substr ($argv[self::$gno]2);
  178.                     else {
  179.                         $nextArg $argv[self::$gno 1];
  180.  
  181.                         if preg_match ('/^-[a-z-]/i'$nextArg) ) {
  182.                             parent::ePrintf (_('%s: option -%s must need option value')$errArg);
  183.                             return null;
  184.                         }
  185.  
  186.                         self::$optarg $nextArg;
  187.                         self::$gno++;
  188.                     }
  189.  
  190.                     if trim (self::$optarg) ) {
  191.                         parent::ePrintf (_("%s: option -%s must need option value")$errArg);
  192.                         return null;
  193.                     }
  194.                 else {
  195.                     if $optvalue_c {
  196.                         parent::ePrintf (_("%s: option -%s must have not any value")$errArg);
  197.                         return null;
  198.                     }
  199.  
  200.                     $buf preg_replace ('/[a-z]:/i'''$optstrs);
  201.                     $blen strlen ($buf);
  202.  
  203.                     $_optok 0;
  204.                     for $i=0$i<$blen$i++ {
  205.                         if $buf[$i== $opt {
  206.                             $_optok++;
  207.                             break;
  208.                         }
  209.                     }
  210.  
  211.                     if $_optok {
  212.                         parent::ePrintf (_("%s: option -%s don't support")$errArg);
  213.                         return null;
  214.                     }
  215.                 }
  216.                 break;
  217.             }
  218.             // }}}
  219.             // {{{ Case by command arg
  220.             else {
  221.                 /*
  222.                  * After '--' command argument, next is not options.
  223.                  * Set self::$optend to 1.
  224.                  */
  225.                 if $argv[self::$gno== '--' {
  226.                     self::$optend 1;
  227.                     continue;
  228.                 }
  229.  
  230.                 self::$optcmd[self::$optcno$argv[self::$gno];
  231.                 self::$optcno++;
  232.                 self::$gno++;
  233.                 continue;
  234.             }
  235.             // }}}
  236.         }
  237.  
  238.         self::$gno++;
  239.         return $opt;
  240.     }
  241.     // }}}
  242. }
  243. ?>

Documentation generated on Tue, 14 May 2019 02:00:42 +0900 by phpDocumentor 1.4.4