用php实现交互式工具——How do I write a command-line interactive PHP script?
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?的更多相关文章
- RHEL6非交互式工具sshpass和expect安装
RHEL6非交互式工具sshpass和expect安装 1 sshpass 在rhel6.4上,没有sshpass的软件包,无法采用yum方式安装.从源码编译安装非常简单. 1) 下载sshpass源 ...
- Jupyter交互式工具安装使用
Jupyter交互式工具安装使用 Jupyter Notebook(此前被称为IPython notebook)是一个交互式笔记本,支持运行 40 多种编程语言. 文档:https://jupyter ...
- MAC 命令行工具(Command Line Tools)安装
不过升级后安装命令行工具(Command Line Tools)时发现官网没有clt的下载安装包了,原来改了,使用命令在线安装. 打开终端,输入命令:xcode-select --install 选择 ...
- OS X 10.9 Mavericks下如何安装Command Line Tools(命令行工具)
OS X 10.9 Mavericks下如何安装Command Line Tools(命令行工具) 今天OS X 10.9 Mavericks正式发布,免费更新,立即去更新看看效果. 不过升级后安装命 ...
- 如何在OS X 10.9 Mavericks下安装Command Line Tools(命令行工具)
随着OS X 10.9 于 2013年6月10日在旧金山WWDC(world wide developer conference)上发布.是首个不使用猫科动物命名的系统,而转用加利福尼亚的产物. 该系 ...
- rtop:一个通过 SSH 监控远程主机的交互式工具【转】
编译自: http://www.tecmint.com/rtop-monitor-remote-linux-server-over-ssh/ rtop[1] 是一个基于 SSH 的直接的交互式远程系统 ...
- Hadoop可视化与交互式工具:Zeppelin和Hue
https://yq.aliyun.com/articles/42282?spm=5176.team18.teamshow1.19.9TkKmZ#rd
- Spark交互式工具spark-shell
REPL Spark REPL Spark shell 下面我们启动一下(我这里搭建的是3节点集群) sc.后面按TAB键可以把提示调出来 查看hdfs上文件内容 这个数据从这里下载的 https:/ ...
- Xcode 命令行工具 Command Line Tools
xcode命令行工具包是一个小型独立包,可供下载独立于Xcode的和允许您执行命令行开发OS X. 在OS X10.9,就以及没有clt的下载安装包了,需要使用命令在线安装. xcode-select ...
随机推荐
- Ubuntu下实现软路由(转)
参考:http://www.openwrt.pro/post-292.html 个人看法: 1.实现路由在Linux下必须要用到iptables进行转发,这才是路由核心. 2.我觉得对于Linux来说 ...
- LayoutParams继承于Android.View.ViewGroup.LayoutParams(转)
LayoutParams相当于一个Layout的信息包,它封装了Layout的位置.高.宽等信息.假设在屏幕上一块区域是由一个Layout占领的,如果将一个View添加到一个Layout中,最好告诉L ...
- SQLSERVER误删Windows登录用户验证方式使用Windows身份验证的解决方法
今天看到这篇文章:没有了SA密码,无法Windows集成身份登录,DBA怎么办? 想起来之前着急哥问我的一个问题,一个DBA删除了Windows登录用户,而且SQLSERVER服务器的验证方式是Win ...
- IIS7.0下 HTTP 错误 404.15 - Not Found 请求筛选模块被配置为拒绝包含的查询字符串过长的请求
IIS7.0下 HTTP 错误 404.15 - Not Found 请求筛选模块被配置为拒绝包含的查询字符串过长的请求 IIS7.0下查询条件太多时,会报错,因为IIS 7对于Query Str ...
- android ActionBarImpl can only be used with a compatible window decor layout
在Activity中添加 requestWindowFeature(Window.FEATURE_ACTION_BAR); requestWindowFeature(Window.FEATURE_AC ...
- Linux学习7-tomcat部署多个项目(多个端口)
前言 前面已经在tomcat上搭建了jenkins的环境,如果我们有多个项目需要部署的话,如何在一个tomcat下部署多个项目呢? 前面是直接在:8080/jenkins访问的,如果有其它项目部署的话 ...
- PL/SQL 存储过程
PL/SQL复习九 存储过程 无参数的存储过程: create or replace procedure out_time is begin dbms_output.put_line(to_char( ...
- Java获取当前时间30天之前的时间
//方法一 SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd"); String maxDateStr = " ...
- Orchard模块开发全接触1:起步
在<http://www.cnblogs.com/luminji/p/3831281.html>中简单介绍了 Orchard 的模块开发,接下来,我们需要做个更复杂的例子,Orchard ...
- main函数的参数argc和argv
版权声明:本文为博主原创文章,转载请注明CSDN博客源地址!共同学习,一起进步~ https://blog.csdn.net/Eastmount/article/details/20413773 该篇 ...