当前位置: 首页 > news >正文

symfony/console

github地址:GitHub - symfony/console: Eases the creation of beautiful and testable command line interfaces

文档地址:The Console Component (Symfony 5.4 Docs)

默认命令list,可以用register注册一个command命令,之后可以设置其他内容,或者设置命令类再加到框架中。

#Symfony\Component\Console\Application
public function __construct(string $name = 'UNKNOWN', string $version = 'UNKNOWN'){$this->name = $name;$this->version = $version;$this->terminal = new Terminal();$this->defaultCommand = 'list';if (\defined('SIGINT') && SignalRegistry::isSupported()) {$this->signalRegistry = new SignalRegistry();$this->signalsToDispatchEvent = [\SIGINT, \SIGTERM, \SIGUSR1, \SIGUSR2];}}
public function register(string $name){return $this->add(new Command($name));}
public function add(Command $command){……$this->commands[$command->getName()] = $command;……}#Symfony\Component\Console\Command\Command
//设置执行代码
public function setCode(callable $code){if ($code instanceof \Closure) {$r = new \ReflectionFunction($code);if (null === $r->getClosureThis()) {set_error_handler(static function () {});try {if ($c = \Closure::bind($code, $this)) {$code = $c;}} finally {restore_error_handler();}}}$this->code = $code;return $this;}
//设置命令名
public function setName(string $name){$this->validateName($name);$this->name = $name;return $this;}
//设置别名public function setAliases(iterable $aliases){$list = [];foreach ($aliases as $alias) {$this->validateName($alias);$list[] = $alias;}$this->aliases = \is_array($aliases) ? $aliases : $list;return $this;}
//设置帮助内容
public function setHelp(string $help){$this->help = $help;return $this;}
//设置是否隐藏
public function setHidden(bool $hidden /* = true */){$this->hidden = $hidden;return $this;}
//设置描述
public function setDescription(string $description){$this->description = $description;return $this;}
#运行文件
require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/Test1Command.php';use Symfony\Component\Console\Application;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;$application = new Application();
$application->register('test')->addArgument('username', InputArgument::REQUIRED, '用户名')->addOption('pwd', "P", InputArgument::REQUIRED, '密码')->setDescription("test command")->setCode(function (InputInterface $input, OutputInterface $output) {return Command::SUCCESS;});
$test1command = new Test1Command();
$application->add($test1command);
$application->run();#Test1Command
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\ArrayInput;
use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;class Test1Command extends Command
{protected function configure(){$this->setName("test1");$this->setHelp('This command allows you to create a user...');$this->addArgument('test', InputArgument::REQUIRED, 'test');$this->setDescription("test1 command");}protected function execute(InputInterface $input, OutputInterface $output): int{var_dump($input->getArguments());//$listcommand = $this->getApplication()->get('list');//$listcommand->run($input, $output);$input = new ArrayInput(['command' => 'list']);$listcommand = $this->getApplication()->get('list');$listcommand->run($input, $output);$output->writeln(['Test1 Command','============','',]);return Command::SUCCESS;}
}

InputInterface实体类为Symfony\Component\Console\Input\ArgvInput,该类继承Symfony\Component\Console\Input\Input,会校验参数和用于获取参数。

#Symfony\Component\Console\Application
public function run(InputInterface $input = null, OutputInterface $output = null){if (\function_exists('putenv')) {@putenv('LINES=' . $this->terminal->getHeight());@putenv('COLUMNS=' . $this->terminal->getWidth());}if (null === $input) {//默认input$input = new ArgvInput();}if (null === $output) {//默认输出$output = new ConsoleOutput();}……$exitCode = $this->doRun($input, $output);……}
public function doRun(InputInterface $input, OutputInterface $output){$input->bind($this->getDefinition());……$name = $this->getCommandName($input);……if (!$name) {$name = $this->defaultCommand;$definition = $this->getDefinition();$definition->setArguments(array_merge($definition->getArguments(),['command' => new InputArgument('command', InputArgument::OPTIONAL, $definition->getArgument('command')->getDescription(), $name),]));}……try {$this->runningCommand = null;// the command name MUST be the first element of the input$command = $this->find($name);} catch (\Throwable $e) {……$alternatives = $e->getAlternatives();……$alternative = $alternatives[0];……$command = $this->find($alternative);}if ($command instanceof LazyCommand) {$command = $command->getCommand();}$this->runningCommand = $command;$exitCode = $this->doRunCommand($command, $input, $output);$this->runningCommand = null;return $exitCode;}#Symfony\Component\Console\Exception\CommandNotFoundException
class CommandNotFoundException extends \InvalidArgumentException implements ExceptionInterface
{private $alternatives;/*** @param string          $message      Exception message to throw* @param string[]        $alternatives List of similar defined names* @param int             $code         Exception code* @param \Throwable|null $previous     Previous exception used for the exception chaining*/public function __construct(string $message, array $alternatives = [], int $code = 0, \Throwable $previous = null){parent::__construct($message, $code, $previous);$this->alternatives = $alternatives;}/*** @return string[]*/public function getAlternatives(){return $this->alternatives;}
}#Symfony\Component\Console\Input\Input
public function __construct(InputDefinition $definition = null){if (null === $definition) {$this->definition = new InputDefinition();} else {//校验选项$this->bind($definition);//校验参数$this->validate();}}
//校验参数
public function validate(){$definition = $this->definition;$givenArguments = $this->arguments;$missingArguments = array_filter(array_keys($definition->getArguments()), function ($argument) use ($definition, $givenArguments) {return !\array_key_exists($argument, $givenArguments) && $definition->getArgument($argument)->isRequired();});if (\count($missingArguments) > 0) {throw new RuntimeException(sprintf('Not enough arguments (missing: "%s").', implode(', ', $missingArguments)));}}
//获取所有参数
public function getArguments(){return array_merge($this->definition->getArgumentDefaults(), $this->arguments);}
//根据变量名获取参数
public function getArgument(string $name){if (!$this->definition->hasArgument($name)) {throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));}return $this->arguments[$name] ?? $this->definition->getArgument($name)->getDefault();}
//设置参数值public function setArgument(string $name, $value){if (!$this->definition->hasArgument($name)) {throw new InvalidArgumentException(sprintf('The "%s" argument does not exist.', $name));}$this->arguments[$name] = $value;}
//判断是否有参数
public function hasArgument(string $name){return $this->definition->hasArgument($name);}
//获取全部选项public function getOptions(){return array_merge($this->definition->getOptionDefaults(), $this->options);}
//根据名称获取选项值
public function getOption(string $name){if ($this->definition->hasNegation($name)) {if (null === $value = $this->getOption($this->definition->negationToName($name))) {return $value;}return !$value;}if (!$this->definition->hasOption($name)) {throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));}return \array_key_exists($name, $this->options) ? $this->options[$name] : $this->definition->getOption($name)->getDefault();}
//设置选项值
public function setOption(string $name, $value){if ($this->definition->hasNegation($name)) {$this->options[$this->definition->negationToName($name)] = !$value;return;} elseif (!$this->definition->hasOption($name)) {throw new InvalidArgumentException(sprintf('The "%s" option does not exist.', $name));}$this->options[$name] = $value;}
//判断是否有选项值
public function hasOption(string $name){return $this->definition->hasOption($name) || $this->definition->hasNegation($name);}#Symfony\Component\Console\Input\ArgvInput
protected function parse(){$parseOptions = true;$this->parsed = $this->tokens;while (null !== $token = array_shift($this->parsed)) {$parseOptions = $this->parseToken($token, $parseOptions);}}
protected function parseToken(string $token, bool $parseOptions): bool{if ($parseOptions && '' == $token) {$this->parseArgument($token);} elseif ($parseOptions && '--' == $token) {return false;} elseif ($parseOptions && str_starts_with($token, '--')) {$this->parseLongOption($token);} elseif ($parseOptions && '-' === $token[0] && '-' !== $token) {$this->parseShortOption($token);} else {$this->parseArgument($token);}return $parseOptions;}
private function parseShortOption(string $token){$name = substr($token, 1);if (\strlen($name) > 1) {if ($this->definition->hasShortcut($name[0]) && $this->definition->getOptionForShortcut($name[0])->acceptValue()) {// an option with a value (with no space)$this->addShortOption($name[0], substr($name, 1));} else {$this->parseShortOptionSet($name);}} else {$this->addShortOption($name, null);}}private function addShortOption(string $shortcut, $value){if (!$this->definition->hasShortcut($shortcut)) {throw new RuntimeException(sprintf('The "-%s" option does not exist.', $shortcut));}$this->addLongOption($this->definition->getOptionForShortcut($shortcut)->getName(), $value);}

运行

 php test.php test 123
php test.php test --helpDescription:test commandUsage:test [options] [--] <username>Arguments:username              用户名Options:-P, --pwd             密码-h, --help            Display help for the given command. When no command is given display help for the list command-q, --quiet           Do not output any message-V, --version         Display this application version--ansi|--no-ansi  Force (or disable --no-ansi) ANSI output-n, --no-interaction  Do not ask any interactive question-v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debugHelp:123
php test.php test1 --helpDescription:test1 commandUsage:test1 <test>Arguments:test                  testOptions:-h, --help            Display help for the given command. When no command is given display help for the list command-q, --quiet           Do not output any message-V, --version         Display this application version--ansi|--no-ansi  Force (or disable --no-ansi) ANSI output-n, --no-interaction  Do not ask any interactive question-v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debugHelp:This command allows you to create a user...
php test.php test1 111array(2) {'command' =>string(5) "test1"'test' =>string(3) "111"
}
Console ToolUsage:command [options] [arguments]Options:-h, --help            Display help for the given command. When no command is given display help for the list command-q, --quiet           Do not output any message-V, --version         Display this application version--ansi|--no-ansi  Force (or disable --no-ansi) ANSI output-n, --no-interaction  Do not ask any interactive question-v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debugAvailable commands:completion  Dump the shell completion scripthelp        Display help for a commandlist        List commandstest        test commandtest1       test1 command
Test1 Command
============

相关文章:

symfony/console

github地址&#xff1a;GitHub - symfony/console: Eases the creation of beautiful and testable command line interfaces 文档地址&#xff1a;The Console Component (Symfony 5.4 Docs) 默认命令list&#xff0c;可以用register注册一个command命令&#xff0c;之后可以…...

OSI模型简介及socket,tcp,http三者之间的区别和原理

1.OSI模型简介&#xff08;七层网络模型&#xff09; OSI 模型(Open System Interconnection model)&#xff1a;一个由国际标准化组织提出的概念模型&#xff0c;试图提供一个使各种不同的计算机和网络在世界范围内实现互联的标准框架。 它将计算机网络体系结构划分为七层,每…...

【leetcode】leetcode69 x的平方根

文章目录 给你一个非负整数 x &#xff0c;计算并返回 x 的 算术平方根 。原理牛顿法&#xff08;数值分析中使用到的&#xff09;:二分法 解决方案java 实现实例执行结果 python 实现实例 给你一个非负整数 x &#xff0c;计算并返回 x 的 算术平方根 。 由于返回类型是整数&…...

springboot与rabbitmq的整合【演示5种基本交换机】

前言&#xff1a; &#x1f44f;作者简介&#xff1a;我是笑霸final&#xff0c;一名热爱技术的在校学生。 &#x1f4dd;个人主页&#xff1a;个人主页1 || 笑霸final的主页2 &#x1f4d5;系列专栏&#xff1a;后端专栏 &#x1f4e7;如果文章知识点有错误的地方&#xff0c;…...

【设计模式】设计原则-单一职责原则

单一职责原则 类的设计原则之单一职责原则&#xff0c;是最常用的类的设计的原则之一。 百度百科&#xff1a;就一个类而言&#xff0c;应该仅有一个引起它变化的原因。应该只有一个职责。 通俗的讲就是&#xff1a;一个类只做一件事 这个解释更通俗易懂&#xff0c;也更符…...

【C++】-多态的底层原理

&#x1f496;作者&#xff1a;小树苗渴望变成参天大树&#x1f388; &#x1f389;作者宣言&#xff1a;认真写好每一篇博客&#x1f4a4; &#x1f38a;作者gitee:gitee✨ &#x1f49e;作者专栏&#xff1a;C语言,数据结构初阶,Linux,C 动态规划算法&#x1f384; 如 果 你 …...

【部署】让你的电脑多出一个磁盘来用!使用SSHFS将远程服务器目录挂载到Windows本地,挂载并共享服务器资源

让你的电脑多出一个磁盘来用&#xff01;---使用SSHFS将远程服务器目录挂载到Windows本地 1. 方法原理介绍2.SSHFS-Win使用教程—实现远程服务器磁盘挂载本地 由于日常主要用 Windows 系统&#xff0c;每次都得 ssh 到服务器上进行取资源&#xff08;本地磁盘不富裕&#xff09…...

/var/lock/subsys目录的作用

总的来说&#xff0c;系统关闭的过程&#xff08;发出关闭信号&#xff0c;调用服务自身的进程&#xff09;中会检查/var/lock/subsys下的文件&#xff0c;逐一关闭每个服务&#xff0c;如果某一运行的服务在/var/lock/subsys下没有相应的选项。在系统关闭的时候&#xff0c;会…...

DETR (DEtection TRansformer)基于自建数据集开发构建目标检测模型超详细教程

目标检测系列的算法模型可以说是五花八门&#xff0c;不同的系列有不同的理论依据&#xff0c;DETR的亮点在于它是完全端到端的第一个目标检测模型&#xff0c;DETR&#xff08;Detection Transformer&#xff09;是一种基于Transformer的目标检测模型&#xff0c;由Facebook A…...

C++初阶 - 5.C/C++内存管理

目录 1.C/C的内存分布 2.C语言中动态内存管理方式&#xff1a;malloc、calloc、realloc、free 3.C内存管理方式 3.1 new/delete操作内置类型 3.2 new 和 delete操作自定义类型 4.operator new 与 operator delete 函数&#xff08;重要点&#xff09; 4.1 operator new 与…...

数学建模学习(3):综合评价类问题整体解析及分析步骤

一、评价类算法的简介 对物体进行评价&#xff0c;用具体的分值评价它们的优劣 选这两人其中之一当男朋友&#xff0c;你会选谁&#xff1f; 不同维度的权重会产生不同的结果 所以找到每个维度的权重是最核心的问题 0.25 二、评价前的数据处理 供应商ID 可靠性 指标2 指…...

【后端面经】微服务构架 (1-5) | 限流:濒临奔溃?限流守护者拯救系统于水火之中!

文章目录 一、前置知识1、什么是限流?2、限流算法A) 静态算法a) 漏桶b) 令牌桶c) 固定窗口d) 滑动窗口B) 动态算法3、限流的模式4、 限流对象4、限流后应该怎么做?二、面试环节1、面试准备2、基本思路3、亮点展现A) 突发流量(针对请求个数而言)B) 请求大小(针对请求大小而言)…...

HDFS异构存储详解

异构存储 HDFS异构存储类型什么是异构存储异构存储类型如何让HDFS知道集群中的数据存储目录是那种类型存储介质 块存储选择策略选择策略说明选择策略的命令 案例&#xff1a;冷热温数据异构存储对应步骤 HDFS内存存储策略支持-- LAZY PERSIST介绍执行使用 HDFS异构存储类型 冷…...

《面试1v1》Kafka消息是采用Pull还是Push模式

&#x1f345; 作者简介&#xff1a;王哥&#xff0c;CSDN2022博客总榜Top100&#x1f3c6;、博客专家&#x1f4aa; &#x1f345; 技术交流&#xff1a;定期更新Java硬核干货&#xff0c;不定期送书活动 &#x1f345; 王哥多年工作总结&#xff1a;Java学习路线总结&#xf…...

Windows环境Docker安装

目录 安装Docker Desktop的步骤 Docker Desktop 更新WSL WSL 的手动安装步骤 Windows PowerShell 拉取&#xff08;Pull&#xff09;镜像 查看已下载的镜像 输出"Hello Docker!" Docker Desktop是Docker官方提供的用于Windows的图形化桌面应用程序&#xff0c…...

Spring 6.0官方文档示例(23): singleton类型的bean和prototype类型的bean协同工作的方法(二)

使用lookup-method: 一、实体类&#xff1a; package cn.edu.tju.domain2;import java.time.LocalDateTime; import java.util.Map;public class Command {private Map<String, Object> state;public Map<String, Object> getState() {return state;}public void …...

Docker Compose 容器编排

Docker compose Docker compose 实现单机容器集群编排管理&#xff08;使用一个模板文件定义多个应用容器的启动参数和依赖关系&#xff0c;并使用docker compose来根据这个模板文件的配置来启动容器&#xff09; 通俗来说就是把之前的多条docker run启动容器命令 转换为docker…...

while循环

while循环是一种常见的循环结构&#xff0c;它会重复执行一段代码&#xff0c;直到指定的条件不再满足。 基本语法如下&#xff1a; while 条件: # 循环体代码 其中&#xff0c;条件是一个布尔表达式&#xff0c;如果为True&#xff0c;则执行循环体中的代码&#xff1b;如果…...

从JVM指令看String对象的比较

在翻看各类 java 知识中&#xff0c;总会提到如下知识&#xff1a;比较 String 对象&#xff0c;例如&#xff1a; String a1new String("10"); String a2"10"; String a3"1""0";//结果 System.out.println(a1a2); //false System.ou…...

python与深度学习(六):CNN和手写数字识别二

目录 1. 说明2. 手写数字识别的CNN模型测试2.1 导入相关库2.2 加载数据和模型2.3 设置保存图片的路径2.4 加载图片2.5 图片预处理2.6 对图片进行预测2.7 显示图片 3. 完整代码和显示结果4. 多张图片进行测试的完整代码以及结果 1. 说明 本篇文章是对上篇文章训练的模型进行测试…...

SciencePlots——绘制论文中的图片

文章目录 安装一、风格二、1 资源 安装 # 安装最新版 pip install githttps://github.com/garrettj403/SciencePlots.git# 安装稳定版 pip install SciencePlots一、风格 简单好用的深度学习论文绘图专用工具包–Science Plot 二、 1 资源 论文绘图神器来了&#xff1a;一行…...

django filter 统计数量 按属性去重

在Django中&#xff0c;如果你想要根据某个属性对查询集进行去重并统计数量&#xff0c;你可以使用values()方法配合annotate()方法来实现。这里有两种常见的方法来完成这个需求&#xff1a; 方法1&#xff1a;使用annotate()和Count 假设你有一个模型Item&#xff0c;并且你想…...

TRS收益互换:跨境资本流动的金融创新工具与系统化解决方案

一、TRS收益互换的本质与业务逻辑 &#xff08;一&#xff09;概念解析 TRS&#xff08;Total Return Swap&#xff09;收益互换是一种金融衍生工具&#xff0c;指交易双方约定在未来一定期限内&#xff0c;基于特定资产或指数的表现进行现金流交换的协议。其核心特征包括&am…...

前端开发面试题总结-JavaScript篇(一)

文章目录 JavaScript高频问答一、作用域与闭包1.什么是闭包&#xff08;Closure&#xff09;&#xff1f;闭包有什么应用场景和潜在问题&#xff1f;2.解释 JavaScript 的作用域链&#xff08;Scope Chain&#xff09; 二、原型与继承3.原型链是什么&#xff1f;如何实现继承&a…...

QT3D学习笔记——圆台、圆锥

类名作用Qt3DWindow3D渲染窗口容器QEntity场景中的实体&#xff08;对象或容器&#xff09;QCamera控制观察视角QPointLight点光源QConeMesh圆锥几何网格QTransform控制实体的位置/旋转/缩放QPhongMaterialPhong光照材质&#xff08;定义颜色、反光等&#xff09;QFirstPersonC…...

处理vxe-table 表尾数据是单独一个接口,表格tableData数据更新后,需要点击两下,表尾才是正确的

修改bug思路&#xff1a; 分别把 tabledata 和 表尾相关数据 console.log() 发现 更新数据先后顺序不对 settimeout延迟查询表格接口 ——测试可行 升级↑&#xff1a;async await 等接口返回后再开始下一个接口查询 ________________________________________________________…...

MySQL 知识小结(一)

一、my.cnf配置详解 我们知道安装MySQL有两种方式来安装咱们的MySQL数据库&#xff0c;分别是二进制安装编译数据库或者使用三方yum来进行安装,第三方yum的安装相对于二进制压缩包的安装更快捷&#xff0c;但是文件存放起来数据比较冗余&#xff0c;用二进制能够更好管理咱们M…...

深度学习水论文:mamba+图像增强

&#x1f9c0;当前视觉领域对高效长序列建模需求激增&#xff0c;对Mamba图像增强这方向的研究自然也逐渐火热。原因在于其高效长程建模&#xff0c;以及动态计算优势&#xff0c;在图像质量提升和细节恢复方面有难以替代的作用。 &#x1f9c0;因此短时间内&#xff0c;就有不…...

RabbitMQ入门4.1.0版本(基于java、SpringBoot操作)

RabbitMQ 一、RabbitMQ概述 RabbitMQ RabbitMQ最初由LShift和CohesiveFT于2007年开发&#xff0c;后来由Pivotal Software Inc.&#xff08;现为VMware子公司&#xff09;接管。RabbitMQ 是一个开源的消息代理和队列服务器&#xff0c;用 Erlang 语言编写。广泛应用于各种分布…...

C++课设:简易日历程序(支持传统节假日 + 二十四节气 + 个人纪念日管理)

名人说:路漫漫其修远兮,吾将上下而求索。—— 屈原《离骚》 创作者:Code_流苏(CSDN)(一个喜欢古诗词和编程的Coder😊) 专栏介绍:《编程项目实战》 目录 一、为什么要开发一个日历程序?1. 深入理解时间算法2. 练习面向对象设计3. 学习数据结构应用二、核心算法深度解析…...