note 53410 added to function.getopt
| From: | daevid at daevid dot com | Date: | Tue, 31 May 2005 20:11:16 +0000 |
| Subject: | note 53410 added to function.getopt | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-89704@lists.php.net to get a copy of this message | ||
This is how I parse command line options:
<?php
$OPTION['debug'] = false;
$OPTION['test'] = false;
$OPTION['force'] = "";
//loop through our arguments and see what the user selected
for ($i = 1; $i < $_SERVER["argc"]; $i++)
{
switch($_SERVER["argv"][$i])
{
case "-v":
case "--version":
echo $_SERVER['argv'][0]." v07.19.04 06:10 PM\n";
exit;
break;
case "--debug":
$OPTION['debug'] = true;
break;
case "--force":
$OPTION['force'] = " --force";
break;
case "--db":
case "--database":
case "--id":
if ( is_numeric($_SERVER['argv'][$i+1]) )
$OPTION['CompanyID'] = intval($_SERVER['argv'][++$i]);
else //it must be a database name
$OPTION['CompanyDB'] = $_SERVER['argv'][++$i];
break;
case "--base":
case "--basedir":
case "--dir":
$OPTION['basedir'] = $_SERVER["argv"][++$i];
break;
case "--test":
$OPTION['test'] = true;
break;
case "-?":
case "-h":
case "--help":
?>
This will print any .txt files, process any .sql files and
execute any .php files found starting from the base directory
'<?=$OPTION['basedir']?>' in alphabetical order.
Usage: <?php echo $_SERVER['argv'][0]; ?> <option>
--help, -help, -h, or -? to get this help.
--version to return the version of this file.
--debug to turn on output debugging.
--test to fake the SQL and PHP commands.
--force to ignore SQL errors and keep on going.
--db [CompanyDB] to apply to only this [CompanyDB].
--id [CompanyID] to apply to only this [CompanyID].
--basedir [directory] to change base directory from <?=$OPTION['basedir']?>.
Omitting the CompanyID or CompanyDB will cause ALL Company DB's to be updated.
There are global files and per-company files. Global files are applied to global
databases (see list below). Their filename contains a tag that reflects the database
to apply them against. Per-company files are applied against databases. Their
filename contains a tag that reads 'COMPANY'. Files should be in
<?=$OPTION['basedir']?>;
Global files that are understood:
<?=$OPTION['basedir']?>
<?php foreach ($GLOBAL_SQL_FILES as $sqlFile) echo $sqlFile." "; ?>
<?php
exit;
break;
}
} //parse arguments
// print_r($OPTION);
?>
----
Manual Page -- http://www.php.net/manual/en/function.getopt.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+53410
Delete: added to the manual -- http://master.php.net/manage/user-notes.php?action=delete+53410&report=yes&reason=added+to+the+manual
Delete: bad code -- http://master.php.net/manage/user-notes.php?action=delete+53410&report=yes&reason=bad+code
Delete: spam -- http://master.php.net/manage/user-notes.php?action=delete+53410&report=yes&reason=spam
Delete: useless -- http://master.php.net/manage/user-notes.php?action=delete+53410&report=yes&reason=useless
Delete: other reasons -- http://master.php.net/manage/user-notes.php?action=delete+53410&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+53410&report=yes
Search -- http://master.php.net/manage/user-notes.php