Source for file oGetopt.php
Documentation is available at oGetopt.php
* Project: oGetopt :: OOPS C library (olibc)의 o_getopt wrapping api<br>
* Dependency: {@link ePrint} over 1.0.1
* OOPS C library의 o_getopt wrapping Class
* 이 패키지는 getopt function의 대안을 제공한다.
* 이 패키지는 {@link ePrint} 1.0.1 이상 버전이 필요하다.
* @author JoungKyun.Kim <http://oops.org>
* @copyright (c) 2018, OOPS.org
* @link http://pear.oops.org/package/oGetopt
* @since File available since relase 1.0.0
* @example oGetopt/test.php 샘플 예제 코드
require_once 'ePrint.php';
function _($s) { return $s; }
* Base classes for oops getopt
* Set 1, option command argument parsing is end
$this->gno = &self::$gno;
$this->optcno = &self::$optcno;
$this->optend = &self::$optend;
$this->optarg = &self::$optarg;
$this->optcmd = &self::$optcmd;
// {{{ public function init ()
static public function init () {
self::$optcmd = array ();
self::$longopt = new stdClass;
// {{{ public function exec ($argc, $argv, $optstrs)
* @return string short option을 반환.
* false를 반환할 경우, getopt 수행이 완료 되었음을 의미.
* 잘못된 옵션이 있을 경우, 에러 메시지 출력 후 null 반환.
* @param integer 명령행 인자 수
* '{@link http://man.kldp.net/wiki/ManPage/getopt.3 man 3 getopt}'
static public function exec ($argc, $argv, $optstrs) {
if ( self::$gno < 0 ) self::$gno = 1;
if ( self::$optcno < 0 ) self::$optcno = 0;
if ( ! isset (self::$optcno) || self::$optend < 0 )
$errMark = parent::asPrintf ('white', _('ERROR'));
if ( self::$gno == $argc )
// {{{ case by long option
if ( preg_match ('/^--[a-z]/i', $argv[self::$gno] ) && ! self::$optend ) {
$longops = explode ('=', $argv[self::$gno]);
$longname = trim (substr ($longops[0], 2));
self::$optarg = trim ($longops[1]);
$errArg = array ($errMark, $longname);
if ( ! ($opt = self::$longopt->$longname) ) {
parent::ePrintf (_("%s: option --%s don't support"), $errArg);
self::$optarg = self::$optarg ? self::$optarg : $argv[self::$gno + 1];
if ( ! trim (self::$optarg) ) {
parent::ePrintf (_('%s: option --%s must need values'), $errArg);
if ( ! preg_match ('/=/', $argv[self::$gno]) ) self::$gno++ ;
// {{{ case by short option
else if ( preg_match ('/^-[a-z]/i', $argv[self::$gno] ) && ! self::$optend ) {
$opt = $argv[self::$gno][1];
$optvalue_c = $argv[self::$gno][2];
$errArg = array ($errMark, $opt);
if ( preg_match ("/{$opt}:/", $optstrs) ) {
self::$optarg = substr ($argv[self::$gno], 2);
$nextArg = $argv[self::$gno + 1];
if ( preg_match ('/^-[a-z-]/i', $nextArg) ) {
parent::ePrintf (_('%s: option -%s must need option value'), $errArg);
self::$optarg = $nextArg;
if ( ! trim (self::$optarg) ) {
parent::ePrintf (_("%s: option -%s must need option value"), $errArg);
parent::ePrintf (_("%s: option -%s must have not any value"), $errArg);
for ( $i= 0; $i< $blen; $i++ ) {
if ( $buf[$i] == $opt ) {
parent::ePrintf (_("%s: option -%s don't support"), $errArg);
// {{{ Case by command arg
* After '--' command argument, next is not options.
* Set self::$optend to 1.
if ( $argv[self::$gno] == '--' ) {
self::$optcmd[self::$optcno] = $argv[self::$gno];
|