Ignore:
Timestamp:
11/07/08 06:48:32 (5 years ago)
Author:
bsittler
Message:

hacked around the lack of a reasonable php getopt_long by writing a
new version in PHP. it seems to work, and is more flexible than the
getopt() found in PHP 5.3+.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • libcaca/trunk/caca-php/examples/img2txt.php

    r3302 r3304  
    2020} 
    2121 
     22class MygetoptException extends Exception 
     23{ 
     24} 
     25 
     26$myoptind = 0; 
     27 
     28function mygetopt($shortopts, $longopts) 
     29{ 
     30        global $argc, $argv, $myoptind; 
     31        if($myoptind < 0) 
     32        { 
     33                $myoptind = 0; 
     34        } 
     35        if(($myoptind + 1) >= $argc) 
     36        { 
     37                return NULL; 
     38        } 
     39        $myoptind ++; 
     40        $nextarg = $argv[$myoptind]; 
     41        $ret = NULL; 
     42        if((substr($nextarg, 0, 1) != '-') 
     43        || 
     44        ($nextarg == '-')) 
     45        { 
     46                $myoptind = $argc - 1; 
     47        } 
     48        else 
     49        { 
     50                $skipopt = $myoptind; 
     51                $skipopts = 1; 
     52                if($nextarg == '--') 
     53                { 
     54                        $myoptind = $argc - 1; 
     55                } 
     56                else 
     57                { 
     58                        $opt = NULL; 
     59                        $arg = NULL; 
     60                        foreach($longopts as $longopt) 
     61                        { 
     62                                $optional = false; 
     63                                $hasarg = false; 
     64                                if(($longopt != '::') && substr($longopt, -2) == '::') 
     65                                { 
     66                                        $optional = true; 
     67                                        $hasarg = true; 
     68                                        $longopt = substr($longopt, 0, -2); 
     69                                } 
     70                                else if(($longopt != ':') && substr($longopt, -1) == ':') 
     71                                { 
     72                                        $optional = false; 
     73                                        $hasarg = true; 
     74                                        $longopt = substr($longopt, 0, -1); 
     75                                } 
     76                                if($nextarg == ('--' . $longopt)) 
     77                                { 
     78                                        $opt = '--' . $longopt; 
     79                                        if($hasarg && ! $optional) 
     80                                        { 
     81                                                if(($myoptind + 1) < $argc) 
     82                                                { 
     83                                                        $myoptind ++; 
     84                                                        $skipopts ++; 
     85                                                        $arg = $argv[$myoptind]; 
     86                                                } 
     87                                                else 
     88                                                { 
     89                                                        throw new MygetoptException("option \"$opt\" requires an argument"); 
     90                                                } 
     91                                        } 
     92                                        break; 
     93                                } 
     94                                else if(substr($nextarg, 0, strlen('--' . $longopt . '=')) 
     95                                                == 
     96                                                ('--' . $longopt . '=')) 
     97                                { 
     98                                        $opt = '--' . $longopt; 
     99                                        $arg = substr($nextarg, strlen($opt . '=')); 
     100                                        if(! $hasarg) 
     101                                        { 
     102                                                throw new MygetoptException("option \"$opt\" does not allow an argument"); 
     103                                        } 
     104                                        break; 
     105                                } 
     106                        } 
     107                        if($opt === NULL) 
     108                        { 
     109                                for($i = 0; $i < strlen($shortopts); $i ++) 
     110                                { 
     111                                        $optional = false; 
     112                                        $hasarg = false; 
     113                                        $shortopt = substr($shortopts, $i, 1); 
     114                                        if(substr($shortopts, $i + 1, 2) == '::') 
     115                                        { 
     116                                                $optional = true; 
     117                                                $hasarg = true; 
     118                                                $i += 2; 
     119                                        } 
     120                                        else if(substr($shortopts, $i + 1, 1) == ':') 
     121                                        { 
     122                                                $hasarg = true; 
     123                                        } 
     124                                        if($nextarg 
     125                                        == 
     126                                        ('-' . $shortopt)) 
     127                                        { 
     128                                                $opt = '-' . $shortopt; 
     129                                                if($hasarg && ! $optional) 
     130                                                { 
     131                                                        if(($myoptind + 1) < $argc) 
     132                                                        { 
     133                                                                $myoptind ++; 
     134                                                                $skipopts ++; 
     135                                                                $arg = $argv[$myoptind]; 
     136                                                        } 
     137                                                        else 
     138                                                        { 
     139                                                                throw new MygetoptException("option \"$opt\" requires an argument"); 
     140                                                        } 
     141                                                } 
     142                                                break; 
     143                                        } 
     144                                        else if(substr($nextarg, 0, strlen('-' . $shortopt)) 
     145                                                        == 
     146                                                        ('-' . $shortopt)) 
     147                                        { 
     148                                                $opt = '-' . $shortopt; 
     149                                                if($hasarg) 
     150                                                { 
     151                                                        $arg = substr($nextarg, strlen($opt)); 
     152                                                } 
     153                                                else 
     154                                                { 
     155                                                        $argv[$myoptind] = '-' . substr($nextarg, strlen($opt)); 
     156                                                        $myoptind --; 
     157                                                        $skipopts = 0; 
     158                                                } 
     159                                        } 
     160                                } 
     161                        } 
     162                        if($opt === NULL) 
     163                        { 
     164                                if(substr($nextarg, 0, strlen('--')) == '--') 
     165                                { 
     166                                        $longopt = substr($nextarg, strlen('--')); 
     167                                        if(strpos($longopt, '=') !== false) 
     168                                        { 
     169                                                $longopt = substr($longopt, 0, strpos($longopt, '=')); 
     170                                        } 
     171                                        throw new MygetoptException("option \"--$longopt\" is not recognized"); 
     172                                } 
     173                                $shortopt = substr($nextarg, strlen('-'), 1); 
     174                                throw new MygetoptException("option \"-$shortopt\" is not recognized"); 
     175                        } 
     176                        $ret = array($opt, $arg); 
     177                } 
     178                if ($skipopts > 0) 
     179                { 
     180                        for($i = 0; $i < $argc; $i ++) 
     181                        { 
     182                                if(($i < $skipopt) || ($i >= ($skipopt + $skipopts))) 
     183                                { 
     184                                        $new_argv[] = $argv[$i]; 
     185                                } 
     186                        } 
     187                        if($myoptind >= $skipopt) 
     188                        { 
     189                                $myoptind -= $skipopts; 
     190                        } 
     191                        $argv = $new_argv; 
     192                        $argc = count($argv); 
     193                } 
     194        } 
     195        return $ret; 
     196} 
     197 
    22198function usage($argc, $argv) 
    23199{ 
     
    74250        $gamma = $brightness = $contrast = -1.0; 
    75251 
    76         if($argc < 2) 
    77         { 
    78                 fprintf(STDERR, "%s: wrong argument count\n", $argv[0]); 
    79                 usage($argc, $argv); 
    80                 return 1; 
    81         } 
    82  
    83252        $long_options = array( 
    84                 "width:"       => 'W', 
    85                 "height:"      => 'H', 
    86                 "font-width:"  => 'x', 
    87                 "font-height:" => 'y', 
    88                 "format:"      => 'f', 
    89                 "dither:"      => 'd', 
    90                 "gamma:"       => 'g', 
    91                 "brightness:"  => 'b', 
    92                 "contrast:"    => 'c', 
    93                 "help"         => 'h', 
    94                 "version"      => 'v' 
     253                "width:"        => 'W', 
     254                "height:"       => 'H', 
     255                "font-width:"   => 'x', 
     256                "font-height:"  => 'y', 
     257                "format:"       => 'f', 
     258                "dither:"       => 'd', 
     259                "gamma:"        => 'g', 
     260                "brightness:"   => 'b', 
     261                "contrast:"     => 'c', 
     262                "help"          => 'h', 
     263                "version"       => 'v' 
    95264                ); 
    96         $options = getopt("W:H:f:d:g:b:c:hvx:y:", array_keys($long_options)); 
    97         if (! $options) 
    98         { 
    99                 /* PHP before 5.3 or so does not have long option support in 
    100                  * most cases */ 
    101                 $options = getopt("W:H:f:d:g:b:c:hvx:y:"); 
    102         } 
    103  
    104         foreach($options as $opt => $arg) 
    105         { 
    106                 if (array_key_exists($opt + (isset($arg) ? ':' : ''), $long_options)) 
    107                 { 
    108                         $opt = $long_options[$opt + (isset($arg) ? ':' : '')]; 
     265 
     266        while($opt_and_arg = mygetopt("W:H:f:d:g:b:c:hvx:y:", array_keys($long_options))) 
     267        { 
     268                $opt = $opt_and_arg[0]; 
     269                $arg = $opt_and_arg[1]; 
     270                if((substr($opt, 0, 2) == '--') 
     271                && 
     272                array_key_exists(substr($opt, strlen('--')) . (($arg !== NULL) ? ':' : ''), $long_options)) 
     273                { 
     274                        $opt = '-' . $long_options[substr($opt, strlen('--')) . (($arg !== NULL) ? ':' : '')]; 
    109275                } 
    110276                switch($opt) 
    111277                { 
    112                 case 'W': /* --width */ 
     278                case '-W': /* --width */ 
    113279                        $cols = intval($arg); 
    114280                        break; 
    115                 case 'H': /* --height */ 
     281                case '-H': /* --height */ 
    116282                        $lines = intval($arg); 
    117283                        break; 
    118                 case 'x': /* --width */ 
     284                case '-x': /* --width */ 
    119285                        $font_width = intval($arg); 
    120286                        break; 
    121                 case 'y': /* --height */ 
     287                case '-y': /* --height */ 
    122288                        $font_height = intval($arg); 
    123289                        break; 
    124                 case 'f': /* --format */ 
     290                case '-f': /* --format */ 
    125291                        $format = $arg; 
    126292                        break; 
    127                 case 'd': /* --dither */ 
     293                case '-d': /* --dither */ 
    128294                        $dither = $arg; 
    129295                        break; 
    130                 case 'g': /* --gamma */ 
     296                case '-g': /* --gamma */ 
    131297                        $gamma = floatval($arg); 
    132298                        break; 
    133                 case 'b': /* --brightness */ 
     299                case '-b': /* --brightness */ 
    134300                        $brightness = floatval($arg); 
    135301                        break; 
    136                 case 'c': /* --contrast */ 
     302                case '-c': /* --contrast */ 
    137303                        $contrast = floatval($arg); 
    138304                        break; 
    139                 case 'h': /* --help */ 
     305                case '-h': /* --help */ 
    140306                        usage($argc, $argv); 
    141307                        return 0; 
    142                 case 'v': /* --version */ 
     308                case '-v': /* --version */ 
    143309                        version(); 
    144310                        return 0; 
     
    148314        } 
    149315 
     316        if($argc != 2) 
     317        { 
     318                fprintf(STDERR, "%s: wrong argument count\n", $argv[0]); 
     319                usage($argc, $argv); 
     320                return 1; 
     321        } 
    150322 
    151323        $cv = caca_create_canvas(0, 0); 
Note: See TracChangeset for help on using the changeset viewer.