入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

使用PHP+Readline库实现类似于Shell解析命令功能

创建时间:2016-11-22 投稿人: 浏览次数:125
#!/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 "
";
    return $result;
  }
}

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

?>




声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
  • 上一篇:没有了
  • 下一篇:没有了
未上传头像