note 65427 added to function.getopt
| From: | joey at alegria dot co dot jp | Date: | Sun, 30 Apr 2006 16:57:45 +0000 |
| Subject: | note 65427 added to function.getopt | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-110196@lists.php.net to get a copy of this message | ||
There are 2 simpler (and much faster) methods for getting good getopt() operation without creating
your own handler.
1. Use the Console_Getopt PEAR class (should be standard in most PHP installations) which lets you
specify both short and long form options as well as whether or not arguments supplied to an option
are themselves 'optional'. Very simple to use and requires very little code to operate
compaired to writing own handler.
2. If you cannot load external PEAR objects, use your shell's getopt() functions (which in
BASHs case work very well) to process options and have your shell script then call your PHP script
with a rigid argument structure that is very easy for PHP to digest such as:
% myfile.php -a TRUE -b FALSE -c ARGUMENT ...
If the initial arguments are invalid you can have the shell script return an error without calling
the PHP script. Sounds convoluted but is a very simple solution and in fact PHP's own % pear
command uses this method. /usr/bin/pear is a shell script that does some simle checking before
calling pearcmd.php and repassing the arguments on to it.
The second method is by far the best for portability because it allows a single shell script to
check a few things like your PHP version and respond acordingly e.g. does it call your PHP4 or PHP5
compatible script? Also, because getopt() is not available on Windows, The second solution allows
you to do Windows specific testing as a BAT file (as oposed to BASH, ZSH or Korn on UNIX).
----
Server IP: 61.195.146.164
Probable Submitter: 218.219.202.171
----
X-Spam-Status: No, hits=3.1 required=5.0 tests=DATE_MISSING,FROM_NO_LOWER
autolearn=no version=2.64
----
Manual Page -- http://www.php.net/manual/en/function.getopt.php
Edit -- http://master.php.net/note/edit/65427
Del: integrated -- http://master.php.net/note/delete/65427/integrated
Del: useless -- http://master.php.net/note/delete/65427/useless
Del: bad code -- http://master.php.net/note/delete/65427/bad+code
Del: spam -- http://master.php.net/note/delete/65427/spam
Del: non-english -- http://master.php.net/note/delete/65427/non-english
Del: in docs -- http://master.php.net/note/delete/65427/in+docs
Del: other reasons-- http://master.php.net/note/delete/65427
Reject -- http://master.php.net/note/reject/65427
Search -- http://master.php.net/manage/user-notes.php