note 85140 modified in function.getopt by danbrown

From: Date: Sat, 27 Dec 2008 17:47:24 +0000
Subject: note 85140 modified in function.getopt by danbrown
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-148583@lists.php.net to get a copy of this message
I really need long options but it's not supported in 5.2.6 on my Solaris box so I wrote an _getopt() to simulate getopt(). An example ======== a.php ======== #!/usr/local/bin/php <?php include "getopt.inc"; $shortopts = "f:v::abc:hp"; $longopts = array( "required:", // Required value "optional::", // Optional value "option", // No value "opt", // No value ); $options = _getopt( $shortopts, $longopts); var_dump($options); ?> ====== end of a.php ====== then run the file a.php, # ./a.php -f "test 2" -ab --required "test 1" --optional="test 3" --option -v "test 4" -k array(7) { ["f"]=> string(6) "test 2" ["a"]=> bool(false) ["b"]=> bool(false) ["required"]=> string(6) "test 1" ["optional"]=> string(6) "test 3" ["option"]=> bool(false) ["v"]=> string(6) "test 4" } Source code is a bit longer and not allowed by this site. Please go to my page http://www.ntu.beautifulworldco.com/weblog/?p=526 for it. --was-- I really need long options but it's not supported in 5.2.6 on my Solaris box so I wrote an _getopt() to simulate getopt(). An example ======== a.php ======== #!/usr/local/bin/php <? include "getopt.inc"; $shortopts = "f:v::abc:hp"; $longopts = array( "required:", // Required value "optional::", // Optional value "option", // No value "opt", // No value ); $options = _getopt( $shortopts, $longopts); var_dump($options); ?> ====== end of a.php ====== then run the file a.php, # ./a.php -f "test 2" -ab --required "test 1" --optional="test 3" --option -v "test 4" -k array(7) { ["f"]=> string(6) "test 2" ["a"]=> bool(false) ["b"]=> bool(false) ["required"]=> string(6) "test 1" ["optional"]=> string(6) "test 3" ["option"]=> bool(false) ["v"]=> string(6) "test 4" } Source code is a bit longer and not allowed by this site. Please go to my page http://www.ntu.beautifulworldco.com/weblog/?p=526 for it. http://php.net/manual/en/function.getopt.php

« previous php.notes (#148583) next »