note 71680 added to function.array-search
| From: | joltmans at bolosystems dot com | Date: | Mon, 11 Dec 2006 18:57:41 +0000 |
| Subject: | note 71680 added to function.array-search | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-120508@lists.php.net to get a copy of this message | ||
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,
?>
----
Server IP: 216.194.113.175
Probable Submitter: 205.158.200.226
----
Manual Page -- http://www.php.net/manual/en/function.array-search.php
Edit -- https://master.php.net/note/edit/71680
Del: integrated -- https://master.php.net/note/delete/71680/integrated
Del: useless -- https://master.php.net/note/delete/71680/useless
Del: bad code -- https://master.php.net/note/delete/71680/bad+code
Del: spam -- https://master.php.net/note/delete/71680/spam
Del: non-english -- https://master.php.net/note/delete/71680/non-english
Del: in docs -- https://master.php.net/note/delete/71680/in+docs
Del: other reasons-- https://master.php.net/note/delete/71680
Reject -- https://master.php.net/note/reject/71680
Search -- https://master.php.net/manage/user-notes.php