tags:

views:

88

answers:

7

I want to write a php script that I can use from the command line. I want it to prompt and accept input for a few items, and then spit out some results. I want to do this in php, because all my classes and libraries are in php, and I just want to make a simple command line interface to a few things.

The prompting and accepting repeated command line inputs is the part that's tripping me up. How do I do this?

+2  A: 

Have a look at How to Prompt the User in a Command Line Interface Using PHP where they have an example of how to do exactly what you're asking.

Alistair
+4  A: 

The I/O Streams page from the PHP manual describes how you can use STDIN to read a line from the command line:

<?php
 $line = trim(fgets(STDIN)); // reads one line from STDIN
 fscanf(STDIN, "%d\n", $number); // reads number from STDIN
?>
Justin Ethier
+3  A: 

From PHP: Read from Keyboard – Get User Input from Keyboard Console by Typing:

You need a special file: php://stdin which stands for the standard input.

print "Type your message. Type '.' on a line by itself when you're done.\n";

$fp = fopen('php://stdin', 'r');
$last_line = false;
$message = '';
while (!$last_line) {
    $next_line = fgets($fp, 1024); // read the special file to get the user input from keyboard
    if (".\n" == $next_line) {
      $last_line = true;
    } else {
      $message .= $next_line;
    }
}
aioobe
You know your link is to this very question? Did you mean to link to another question?
`$next_line = fgets($fp, 1024);` should be `$next_line = fgets($fh, 1024);`
@user151841 sorry abouth the link. I got my browser tabs mixed up. thanks for the typo too. :) now it's corrected.
aioobe
A: 

Basically you read from standard input. See http://www.php.net/manual/en/features.commandline.io-streams.php.

Joonas Trussmann
+1  A: 

The algorithm is simple:

until done:
    display prompt
    line := read a command line of input
    handle line

It's very trivial to use an Array() that maps commands to callback functions that handle them. The entire challenge is roughly a while loop, and two function calls, PHP also has a readline interface for more advanced shell apps.

TerryP
The "read a command line of input" part trips me up. How do you get it to continuously poll the user input?
Normally it is done something like while ($line = readline($promptstring)); the fine details basically amount to the kind of error checking you want, and how the user asks to exit. Take a look at the fgets() and readline() functions in the manual.
TerryP
+1  A: 

Found an example on PHP.net : http://www.php.net/manual/fr/features.commandline.php#94924

$handle = fopen ("php://stdin","r");
$line = fgets($handle);
if(trim($line) != 'yes') {
...
Serty Oan
+1  A: 

I'm not sure how complex your input might be, but readline is an excellent way to handle it for interactive CLI programs.

You get the same creature comforts out of it that you would expect from your shell, such as command history.

Using it is as simple as:

$command = readline("Enter Command: ");
/* Then add the input to the command history */
readline_add_history($command);

If available, it really does make it simple.

Tim Post