note 49507 added to function.getopt
| From: | dante at wiw dot org | Date: | Sun, 30 Jan 2005 16:44:48 +0000 |
| Subject: | note 49507 added to function.getopt | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-84243@lists.php.net to get a copy of this message | ||
i didn't like the way getopt worked, exactly, so i wrote a new variant, that other people would
possibly like to see. (works more like perl's function)
it reads an array of options like:
$opAr = array ("-a|--append","-l|--list","-i|--input:");
$op = bgetop($opAr);
and parses the command line, returning an array like
$op['cmdline'] = $param; ...
it has a small bug that can easily be avoided ... haven't yet determined how to work around the
particular case where the bug exists, but otherwise is very robust.
it also accepts wildwards as the last option, or the output from another program like
'find' :
./getop.php
find /etc -name *.conf
populates the $op array with the filenames returned from find. it's pretty nifty.
the source is at : http://higginsforpresident.net/projects/source/getopt-0.1.phps
i didn't want to post it here until i fixed that one condition, but the function works nicely
(as expected) if you don't use duplicate beginnings for different option array strings:
$opAr = array("-f:","--foo");
bgetopt() sees --foo needs no 'next input', but -f exists in '--foo', so
./getop.php -f foobar set's f to 'true', which is the
expected result of array("-f","--foo") or array("-f|--foo"), but not
("-f:","--foo");
enjoy. (my first submission here ... be kind.)
----
Manual Page -- http://www.php.net/manual/en/function.getopt.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+49507
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+49507&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+49507&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+49507&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+49507&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+49507&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+49507&report=yes
Search -- http://master.php.net/manage/user-notes.php