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?

参考:https://stackoverflow.com/questions/2929629/how-do-i-write-a-command-line-interactive-php-script

1. readline实现

<?php
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. fgets实现

 #!/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);
} elseif( $response == 'hello' ){
echo "\n", "hello, world;\n";
}
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";

  test

3. fopen php://stdin实现

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;
}
}

  

用php实现交互式工具——How do I write a command-line interactive PHP script?的更多相关文章

  1. RHEL6非交互式工具sshpass和expect安装

    RHEL6非交互式工具sshpass和expect安装 1 sshpass 在rhel6.4上,没有sshpass的软件包,无法采用yum方式安装.从源码编译安装非常简单. 1) 下载sshpass源 ...

  2. Jupyter交互式工具安装使用

    Jupyter交互式工具安装使用 Jupyter Notebook(此前被称为IPython notebook)是一个交互式笔记本,支持运行 40 多种编程语言. 文档:https://jupyter ...

  3. MAC 命令行工具(Command Line Tools)安装

    不过升级后安装命令行工具(Command Line Tools)时发现官网没有clt的下载安装包了,原来改了,使用命令在线安装. 打开终端,输入命令:xcode-select --install 选择 ...

  4. OS X 10.9 Mavericks下如何安装Command Line Tools(命令行工具)

    OS X 10.9 Mavericks下如何安装Command Line Tools(命令行工具) 今天OS X 10.9 Mavericks正式发布,免费更新,立即去更新看看效果. 不过升级后安装命 ...

  5. 如何在OS X 10.9 Mavericks下安装Command Line Tools(命令行工具)

    随着OS X 10.9 于 2013年6月10日在旧金山WWDC(world wide developer conference)上发布.是首个不使用猫科动物命名的系统,而转用加利福尼亚的产物. 该系 ...

  6. rtop:一个通过 SSH 监控远程主机的交互式工具【转】

    编译自: http://www.tecmint.com/rtop-monitor-remote-linux-server-over-ssh/ rtop[1] 是一个基于 SSH 的直接的交互式远程系统 ...

  7. Hadoop可视化与交互式工具:Zeppelin和Hue

    https://yq.aliyun.com/articles/42282?spm=5176.team18.teamshow1.19.9TkKmZ#rd

  8. Spark交互式工具spark-shell

    REPL Spark REPL Spark shell 下面我们启动一下(我这里搭建的是3节点集群) sc.后面按TAB键可以把提示调出来 查看hdfs上文件内容 这个数据从这里下载的 https:/ ...

  9. Xcode 命令行工具 Command Line Tools

    xcode命令行工具包是一个小型独立包,可供下载独立于Xcode的和允许您执行命令行开发OS X. 在OS X10.9,就以及没有clt的下载安装包了,需要使用命令在线安装. xcode-select ...

随机推荐

  1. ARM Cortex Design Considerations for Debug

    JTAG was the traditional mechanism for debug connections for ARM7/9 parts, but with the Cortex-M fam ...

  2. 使用Java进行串口SerialPort通讯

    1.准备工作        在进行串口连接通讯前,必须保证你当前操作电脑上有可用且闲置的串口.因为一般的电脑上只有一个或者两个串口,如COM1或COM2,但大多数情况下,这些串口可能会被其他的程序或者 ...

  3. layer.confirm 询问框 的层遮盖

    function admin_del(obj) { layer.confirm('确认要重启吗?', { btn : [ '确定', '取消' ]//按钮 }, function(index) { l ...

  4. mysql slave 主从 指定表 通配符

    slave配置 slave端有可能,只复制部分表,有一些表不需要备份配置如下: 有一些表你可能做水平或则垂直的处理.如果表的前几位一样,就可以用通配符%匹配 replicate-wild-ignore ...

  5. tms web core程序部署

    tms web core程序部署 笔者把已经开发好的TMS WEB CORE程序部署到阿里云服务器上面,来作为例子. 1)复制TMS WEB CORE前端程序到服务器的c:\room\ 2)复制TMS ...

  6. 【python】python读写文件,都不乱码

    读是按照文本的编码方式读取,写是按照文本的编码方式追加 import chardet fileName = 'E:/2/采集数据_pswf12_180大0小35750_20181206.txt' # ...

  7. oracle exp/imp命令详解

    E:\>exp help=y 通过输入 EXP 命令和用户名/口令,您可以 在用户 / 口令之后的命令: 实例: EXP SCOTT/TIGER 或者,您也可以通过输入跟有各种参数的 EXP 命 ...

  8. IOS Devices Version

    游戏项目中有一个专门用于收集IOS崩溃的接口和查询页,运营/测试的同事有时候会通过查询页大概看一下每日崩溃的情况,经常会问iPhone6,1是什么,iPhone7,1又是什么设备?   我从网上仔细搜 ...

  9. mac下配置android开发环境

    从昨天下午到如今.用了差点儿相同一整天的时间去熟悉mac和配置环境,基本已经OK了,大体记录了整个过程中遇到的问题. 1.安装java环境 之前听说mac自带java环境.java -version一 ...

  10. LaTeX技巧23:BIBTeX制作参考文献

    23BibTeX 是一种格式和一个程序,用于协调LaTeX的参考文献处理.BibTeX 使用数据库的的方式来管理参考文献. BibTeX 文件的后缀名为 .bib . 先来看一个例子 @article ...