使用PHP+Readline庫實現類似於Shell解析命令功能

#!/usr/bin/php -q
<?php

set_time_limit(0);
@ob_end_flush();
ob_implicit_flush(true);

class prompt {
  var $tty;

  function prompt() {
    if (substr(PHP_OS, 0, 3) == "WIN") {
      $this->tty = fOpen("\con", "rb");
    } else {
      if (!($this->tty = fOpen("/dev/tty", "r"))) {
        $this->tty = fOpen("php://stdin", "r");
      }
    }
  }

  function get($string, $length = 1024) {
    echo $string;
    $result = trim(fGets($this->tty, $length));
    echo "\n";
    return $result;
  }
}

echo "Enter command or 'exit' to quit\n";
do {
  $cmdline = new prompt();
  $buffer = $cmdline->get("PHP: ");
  echo "accept cmd : $buffer\n";
} while ($buffer !== "exit");
echo "waiting for exit....\n";

?>




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章