首页 > 其他分享 >CLI模式

CLI模式

时间:2023-01-03 14:48:23浏览次数:46  
标签:index PHP CLI Show -- args 模式 php

CLI模式其实就是命令行运行模式,英文全称Command-Line Interface(命令行接口)

$ php -h
Usage: php [options] [-f] <file> [--] [args...]
   php [options] -r <code> [--] [args...]
   php [options] [-B <begin_code>] -R <code> [-E <end_code>] [--] [args...]
   php [options] [-B <begin_code>] -F <file> [-E <end_code>] [--] [args...]
   php [options] -S <addr>:<port> [-t docroot] [router]
   php [options] -- [args...]
   php [options] -a

  -a               Run as interactive shell
                   以交互shell模式运行
  -c <path>|<file> Look for php.ini file in this directory
                   指定php.ini文件所在的目录
  -n               No configuration (ini) files will be used
                   指定不使用php.ini文件
  -d foo[=bar]     Define INI entry foo with value 'bar'
                   定义一个INI实体,key为foo,value为'bar'
  -e               Generate extended information for debugger/profiler
                   为调试和分析生成扩展信息
  -f <file>        Parse and execute <file>.
                   解释和执行文件<file>
  -h               This help
                   打印帮助信息
  -i               PHP information
                   显示PHP的基本信息
  -l               Syntax check only (lint)
                   进行语法检查(lint)
  -m               Show compiled in modules
                   显示编译到内核的模块
  -r <code>        Run PHP <code> without using script tags <?..?>
                   运行PHP代码<code>,不需要使用标签<?..?>
  -B <begin_code>  Run PHP <begin_code> before processing input lines
                   在处理输入之前先执行PHP代码<begin_code>
  -R <code>        Run PHP <code> for every input line
                   对输入的每一行作为PHP代码<code>运行
  -F <file>        Parse and execute <file> for every input line
                   对输入的每一行解析和执行<file>
  -E <end_code>    Run PHP <end_code> after processing all input lines
                   在处理所有输入的行之后执行PHP代码<end_code>
  -H               Hide any passed arguments from external tools.
                   隐藏任何来自外部工具传递的参数
  -S <addr>:<port> Run with built-in web server.
                   运行内置的web服务器
  -t <docroot>     Specify document root <docroot> for built-in web server.
                   指定用于内置web服务器的文档根目录<docroot>
  -s               Output HTML syntax highlighted source.
                   输出HTML语法高亮的源码
  -v               Version number
                   输出PHP的版本号
  -w               Output source with stripped comments and whitespace.
                   输出去掉注释和空格的源码
  -z <file>        Load Zend extension <file>.
                   载入Zend扩展文件<file>

  args...          Arguments passed to script. Use -- args when first argument
                   starts with - or script is read from stdin
                   传递给要运行的脚本的参数。当第一个参数以'-'开始或者是脚本是从标准输入读取的时候,使用'--'参数

  --ini            Show configuration file names
                   显示PHP的配置文件名

  --rf <name>      Show information about function <name>.
                   显示关于函数<name>的信息
  --rc <name>      Show information about class <name>.
                   显示关于类<name>的信息
  --re <name>      Show information about extension <name>.
                   显示关于扩展<name>的信息
  --rz <name>      Show information about Zend extension <name>.
                   显示关于Zend扩展<name>的信息
  --ri <name>      Show configuration for extension <name>.
                   显示扩展<name>的配置信息
当前目录作为Root Document只需要这条命令即可:
php -S localhost:3300
也可以指定其它路径
php -S localhost:3300 -t /path/to/root
还可以指定路由
php -S localhost:3300 router.php

以交互式Shell模式运行PHP

http://php.net/manual/en/features.commandline.interactive.php
The interactive shell stores your history which can be accessed using the up and down keys. The history is saved in the ~/.php_history file.
交互shell模式保存输入的历史命令,可以使用上下键访问到。历史被保存在~/.php_history文件。

$ php -a
Interactive shell

php > echo 5+8;
13
php > function addTwo($n)
php > {
php { return $n + 2;
php { }
php > var_dump(addtwo(2));
int(4)

查找相关类、扩展或者函数的信息

通常,我们可以使用php --info命令或者在在web服务器上的php程序中使用函数phpinfo()显示php的信息,然后再查找相关类、扩展或者函数的信息,这样做实在是麻烦了一些。

$ php --info | grep redis
redis
Registered save handlers => files user redis
This program is free software; you can redistribute it and/or modify

语法检查

只需要检查php脚本是否存在语法错误,而不需要执行它,比如在一些编辑器或者IDE中检查PHP文件是否存在语法错误。
使用-l(--syntax-check)可以只对PHP文件进行语法检查。

$ php -l index.php
No syntax errors detected in index.php

假如index.php中存在语法错误。

$ php -l index.php
PHP Parse error:  syntax error, unexpected 'echo' (T_ECHO) in index.php on line 3
Parse error: syntax error, unexpected 'echo' (T_ECHO) in index.php on line 3
Errors parsing index.php

命令行脚本

$argc 包含了 $argv数组包含元素的数目
$argv 是一个数组,包含了提供的参数,第一个参数总是脚本文件名称
console.php的命令行脚本文件

<?php
echo '命令行参数个数: ' . $argc . "n";
echo "命令行参数:n";
foreach ($argv as $index => $arg) {
    echo "    {$index} : {$arg}n";
}
$ php console.php hello world
命令行参数个数: 3
命令行参数:
    0 : console.php
    1 : hello
    2 : world

可以看到,第0个参数是我们执行的脚本名称。需要注意的是,如果提供的第一个参数是以-开头的话,需要在前面增加--,以告诉php这后面的参数是提供给我们的脚本的,而不是php执行文件的(php -r 'var_dump($argv);' -- -h)。
另外,在脚本中,我们可以通过php_sapi_name()函数判断是否是在命令行下运行的。

$ php -r 'echo php_sapi_name(), PHP_EOL;'
cli

标签:index,PHP,CLI,Show,--,args,模式,php
From: https://www.cnblogs.com/fuqian/p/17022121.html

相关文章

  • 直播app开发,实现简单的发布订阅者模式
    直播app开发,实现简单的发布订阅者模式1.首先想好谁是发布者 2.给发布者添加一个缓存列表,存放回调函数通知订阅者 3.发布消息,发布者遍历缓存列表,依次触发里面存放的......
  • kafka学习笔记03消息队列的两种模式
     ①点对点模式  该种模式就是消费者会自动消费消息,消息收到之后会向消息队列进行确认收到消息,然后将该数据进行删除。 ②发布/订阅模式  可以有多个的topic,topic......
  • Docker 的四种网络模式
    Docker有bridge、none、host、container四种网络模式,提供网络隔离、端口映射、容器间互通网络等各种支持,下面开门见山地直接介绍这四种网络模式。这四种网络模式可以......
  • 享元模式
    一、什么是享元模式? 在一个系统中如果有多个相同的对象,那么只共享一份就可以了,不必每个都去实例化一个对象。采用一个共享来避免大量拥有相同内容对象的开销。这种开销中最......
  • 桥接模式
    1、什么是桥接模式?  变化部分与主类分离开来,从而将多个维度的变化彻底分离。最后,提供一个管理类来组合不同维度上的变化,通过这种组合来满足业务的需要。 意图:   ......
  • 原型模式
    概念:用原型实例指定创建对象的种类,并通过拷贝这些原型创建新的对象。模式图:    原理图:  ​​​​      原型模式主要用于对象的复制,它的核心就是类图中的......
  • 抽象工厂模式
       概述:     抽象工厂模式是所有形态的工厂模式中最为抽象和最具一般性的一种形态。抽象工厂模式是指当有多个抽象角色时,使用的一种工厂模式。抽象工厂模式......
  • 单例模式和多例模式的总结
    学习交流关注微信公众号:钟渊博客今天开始学习设计模式之单例和多例单例模式的关键有两点:1.构造方法为私有,这样外界就不能随意调用。2.get的方法为静态,由类直接调用 多例模......
  • HTTP客户端之Spring WebClient
    对于HTTP客户端,其实有很多种,而SpringBoot也提供了一种方式叫SpringWebClient。它是在Spring5中引入的异步、反应式HTTP客户端,用于取代较旧的RestTemplate,以便在使用Sprin......
  • 在多线程创建TFPHTTPClient对象并发送请求时出现Could not initialize OpenSSL librar
    在多线程创建TFPHTTPClient对象并发送请求时出现CouldnotinitializeOpenSSLlibrary应该怎么解决?单线程的时候没有遇到。经网友指导下使用以下方法就可以解决这个问题:......