note 93949 deleted from function.getopt by danbrown

From: Date: Thu, 08 Oct 2009 13:24:06 +0000
Subject: note 93949 deleted from function.getopt by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-161511@lists.php.net to get a copy of this message
Note Submitter: Gabolander ---- 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

« previous php.notes (#161511) next »