Re: php on the Command line
| From: | Chris Adams | Date: | Sun, 25 Jun 2000 22:55:19 +0000 |
| Subject: | Re: php on the Command line | ||
| References: | 1 2 3 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-3044@lists.php.net to get a copy of this message | ||
> > > 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 ==
"){
> $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. :)
Maybe something like
for ($i = 0; $i < $argc; $i++) {
switch ($argv[$i]) {
case "-i":
$IP = $argv[++$i];
break;
case "-n":
$Username = $argv[++$i];
break;
}
}
Of course, this doesn't really work if the user does something like "-u -n -i
127.0.0.1". Perhaps it'd be better to use a style such as --username=bob and parse
everything from -- to the equal sign as the key and everything afterword as the value.
> > 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 :)
One thing I've been wishing is that there was some exposed versioning system in the manual
code.
It'd be really cool to be able to see what's new in recent versions in a form a little
friendlier
than diff php3_manual_file.html php4_manual_file.html.