note 93949 added to function.getopt

From: Date: Thu, 08 Oct 2009 08:59:19 +0000
Subject: note 93949 added to function.getopt
Groups: php.notes 
Request: Send a blank email to php-notes+get-161498@lists.php.net to get a copy of this message
Nice function, Francois Hill, but IMHO you should also decrease the argc value accordingly with shift(S) ... Thus, the "shift" function, with this correction should be: <?php function shift($options_array) { foreach( $options_array as $o => $a ) { // Look for all occurrences of option in argv and remove if found : // ---------------------------------------------------------------- // Look for occurrences of -o (simple option with no value) or -o<val> (no space in between): while($k=array_search("-".$o.$a,$GLOBALS['argv'])) { // If found remove from argv: if($k) unset($GLOBALS['argv'][$k]); // decreasing argc value for one option $GLOBALS['argc']-=1; } // Look for remaining occurrences of -o <val> (space in between): while($k=array_search("-".$o,$GLOBALS['argv'])) { // If found remove both option and value from argv: if($k) { unset($GLOBALS['argv'][$k]); unset($GLOBALS['argv'][$k+1]); // decreasing argc value for one option $GLOBALS['argc']-=2; } } } // Reindex : $GLOBALS['argv']=array_merge($GLOBALS['argv']); } $options = getopt("b:"); shift($options); var_dump($options); echo "Argc = $argc"; echo "Argv = "; var_dump($argv); ?> .. so if I run my program as : $ php myprog.php -bABCDE file.txt OR $ php myprog.php -b ABCDE file.txt I would obtain : array(1) { ["b"]=> string(5) "ABCDE" } Argv = array(2) { [0]=> string(12) "chaccpwd.php" [1]=> string(8) "file.txt" } Argc = 2 In fact, excluding options and options' arguments, there are only two left parameters. Thank you for your post. Gabo ---- Server IP: 69.147.83.197 Probable Submitter: 195.62.176.5 ---- Manual Page -- http://www.php.net/manual/en/function.getopt.php Edit -- https://master.php.net/note/edit/93949 Del: integrated -- https://master.php.net/note/delete/93949/integrated Del: useless -- https://master.php.net/note/delete/93949/useless Del: bad code -- https://master.php.net/note/delete/93949/bad+code Del: spam -- https://master.php.net/note/delete/93949/spam Del: non-english -- https://master.php.net/note/delete/93949/non-english Del: in docs -- https://master.php.net/note/delete/93949/in+docs Del: other reasons-- https://master.php.net/note/delete/93949 Reject -- https://master.php.net/note/reject/93949 Search -- https://master.php.net/manage/user-notes.php

« previous php.notes (#161498) next »