Re: php on the Command line
| From: | Bjorn Sodergren | Date: | Sun, 25 Jun 2000 22:29:00 +0000 |
| Subject: | Re: php on the Command line | ||
| References: | 1 2 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-3040@lists.php.net to get a copy of this message | ||
Chris Adams wrote:
>
> > i understand how argv and argc work, but if someone leaves off a
> > required field, or something such as a password , is it possible to stop
> > and prompt them ? i know no perl or c, and im fairly efficent in php, so
> > i'd like to stick with this.
>
> You could just do something as simple as:
> if ($argc != 3) {
> die("usage: scriptname username password database");
> } else {
> list($Username, $Password, $Database) = $argv;
> }
im using something similar to that, it accepts -n -i etc, etc
$c = $argc;
while(list($key, $pair) = each($argv) ){
if($pair == "-i"){
$n = 1 + $key;
$ip = $argv["$n"];
$n = 0;
}
if($pair == "-u"){
$n = 1 + $key;
$user = $argv["$n"];
$n = 0;
}
......
}
# required fields..
if(!$ip || !$domain || !$user || !$passwd){
echo "you forgot something\n";
echo "usage is:$argv[0] -i [ip] -d [domain] -u [user] -p [password]
[OPTIONS]\n";
.. then say things...
exit;
}
Works quite well too. going to switch it to switch() case: statements
later on, just
testing and debuging the script first..
If anyone else has an idea on how to make that even cleaner, id like to
hear it. :)
> If you want the user to be able to enter a value, try using fgets on stdin (See the fopen()
> docs for
> instructions on how to open stdin, stdout, or stderr). It should be somewhat like this:
>
> $stdout = fopen("php://stdout");
> $stdin = fopen("php://stdin");
>
> fputs($stdout, "Username: ");
> fgets($stdin, $Username);
>
I really really need to sit down sometime and read EVERY page of that
manual :)
thanks
bjorn
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, e-mail: php-general-unsubscribe@lists.php.net
> For additional commands, e-mail: php-general-help@lists.php.net
> To contact the list administrators, e-mail: php-list-admin@lists.php.net