如何编写命令行交互式PHP脚本?

如何编写命令行交互式PHP脚本?,php,shell,Php,Shell,我想写一个可以从命令行使用的PHP脚本。我希望它提示并接受一些项目的输入,然后输出一些结果。我想用PHP实现这一点,因为我所有的类和库都是用PHP实现的,我只想为一些事情创建一个简单的命令行界面 提示和接受重复的命令行输入是让我绊倒的部分。如何执行此操作?PHP手册中的页面介绍了如何使用STDIN从命令行读取一行: <?php $line = trim(fgets(STDIN)); // reads one line from STDIN fscanf(STDIN, "%d\n", $

我想写一个可以从命令行使用的PHP脚本。我希望它提示并接受一些项目的输入,然后输出一些结果。我想用PHP实现这一点,因为我所有的类和库都是用PHP实现的,我只想为一些事情创建一个简单的命令行界面

提示和接受重复的命令行输入是让我绊倒的部分。如何执行此操作?

PHP手册中的页面介绍了如何使用STDIN从命令行读取一行:

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

来自:

您需要一个特殊文件:
php://stdin
代表标准输入


基本上你是从标准输入中读取的。请参阅。

算法很简单:

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

使用将命令映射到处理它们的回调函数的数组非常简单。整个挑战大致是一个
while
循环和两个函数调用。PHP还为更高级的shell应用程序提供了一个示例。

我在PHP.net上找到了一个示例:


我不确定您的输入可能有多复杂,但对于交互式CLI程序来说,这是一种很好的处理方法

你可以从中得到与你期望的外壳相同的生物安慰,比如命令历史

使用它非常简单:

$command = readline("Enter Command: ");
/* Then add the input to the command history */
readline_add_history($command);
,这确实使它变得简单


这里是控制台实现的典型do情况:

do {
  $cmd = trim(strtolower( readline("\n> Command: ") ));
  readline_add_history($cmd);
  switch ($cmd) {
    case 'hello': print "\n -- HELLO!\n"; break;
    case 'bye': break;
    default: print "\n -- You say '$cmd'... say 'bye' or 'hello'.\n";
  }
} while ($cmd!='bye');
用户可以使用箭头(向上和向下)访问历史记录。

一行代码(第2行):

简单:

#!/usr/bin/php
<?php
define('CONFIRMED_NO', 1);

while (1) {
    fputs(STDOUT, "\n"."***WARNING***: This action causes permanent data deletion.\nAre you sure you're not going to wine about it later? [y,n]: ");

    $response = strtolower(trim(fgets(STDIN)));
    if( $response == 'y' ) {
        break;
    } elseif( $response == 'n' ) {
        echo "\n",'So I guess you changed your mind eh?', "\n";
        exit (CONFIRMED_NO);
    } else {
        echo "\n", "Dude, that's not an option you idiot. Let's try this again.", "\n";
        continue;
    }
}

echo "\n","You're very brave. Let's continue with what we wanted to do.", "\n\n";
#/usr/bin/php
我的五美分:
使用
STDOUT
STDIN

fwrite(STDOUT, "Please enter your Message (enter 'quit' to leave):\n");

do{
    do{
        $message = trim(fgets(STDIN));
    } while($message == '');

    if(strcasecmp($message, 'quit') != 0){
        fwrite(STDOUT, "Your message: ".$message."\n");
    }

}while(strcasecmp($message,'quit') != 0);
// Exit correctly
exit(0);

“读取输入的命令行”部分让我大吃一惊。如何让它持续轮询用户输入?通常是这样做的:while($line=readline($promptstring));这些细节基本上相当于您想要的错误检查类型,以及用户要求退出的方式。请看一下手册中的fgets()和readline()函数。实现简单!
<?php
    $name = trim(shell_exec("read -p 'Enter your name: ' name\necho \$name"));
    echo "Hello $name, this is PHP speaking\n";
    exit;
#!/usr/bin/php
<?php
define('CONFIRMED_NO', 1);

while (1) {
    fputs(STDOUT, "\n"."***WARNING***: This action causes permanent data deletion.\nAre you sure you're not going to wine about it later? [y,n]: ");

    $response = strtolower(trim(fgets(STDIN)));
    if( $response == 'y' ) {
        break;
    } elseif( $response == 'n' ) {
        echo "\n",'So I guess you changed your mind eh?', "\n";
        exit (CONFIRMED_NO);
    } else {
        echo "\n", "Dude, that's not an option you idiot. Let's try this again.", "\n";
        continue;
    }
}

echo "\n","You're very brave. Let's continue with what we wanted to do.", "\n\n";
fwrite(STDOUT, "Please enter your Message (enter 'quit' to leave):\n");

do{
    do{
        $message = trim(fgets(STDIN));
    } while($message == '');

    if(strcasecmp($message, 'quit') != 0){
        fwrite(STDOUT, "Your message: ".$message."\n");
    }

}while(strcasecmp($message,'quit') != 0);
// Exit correctly
exit(0);