note 71680 deleted from function.array-search by bobby
| From: | bobby@php.net | Date: | Mon, 11 Dec 2006 19:58:29 +0000 |
| Subject: | note 71680 deleted from function.array-search by bobby | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-120514@lists.php.net to get a copy of this message | ||
Note Submitter: joltmans at bolosystems dot com
----
I had a syntax error in the previous post, please delete it.
---------------------------------------------------------------------
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,
?>