note 71587 deleted from function.array-search by felipe
| From: | felipe@php.net | Date: | Sun, 10 Feb 2008 13:08:47 +0000 |
| Subject: | note 71587 deleted from function.array-search by felipe | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-135947@lists.php.net to get a copy of this message | ||
Note Submitter: joltmans at bolosystems dot com
----
You can use array_search to simply parse a CLI program's input parameters such as:
[user@server ~]$ ./someCLIprog.php --username user1 --password 123password --option1 whatever
You can easily parse this information using the following code snippets:
<?php
// Get the username
$userkey = array_search("--username", $argv);
// Make sure both that the token was found and followed by another valid token, AKA not one that
begins with a -
if ($userkey && !empty($argv[$userkey+1]) && strcmp($argv[$userkey+1]{0},
'-')) {
$username = $argv[$userkey+1];
}
// If you want the token to be required add the following else statement
else {
exit("No username provided\n");
}
// repeat for each optional parameter,
?>