华为防火墙cli控制台

tech2022-09-05  105

华为防火墙cli控制台

I have always been a big fan of console commands and I try to provide a command line interface (CLI) as much as possible in most of my PHP projects.

我一直是控制台命令的忠实拥护者,并且我尝试在大多数PHP项目中尽可能提供命令行界面(CLI)。

In this article, I’ll briefly compare three PHP console command libraries:

在本文中,我将简要比较三个PHP控制台命令库:

The Symfony console component (symfony/Console)

Symfony控制台组件( symfony / Console )

The Hoa console (hoa/console)

Hoa控制台( hoa /控制台 )

The Webmozart console (webmozart/console)

Webmozart控制台( webmozart /控制台 )

起源故事 (Origin Stories)

The Symfony console is the oldest and the most popular one, used in many projects (and obviously part of the Symfony framework). With dozens of contributors, it became the first choice for many developers.

Symfony控制台是最古老和最受欢迎的控制台 ,在许多项目中使用(显然是Symfony框架的一部分)。 拥有数十名贡献者,它已成为许多开发人员的首选。

Hoa is a modular, extensible and structured set of PHP libraries that includes the Hoa console. It aims to be a bridge between industrial and research worlds, and this makes that project quite interesting.

Hoa是一组模块化,可扩展和结构化PHP库,其中包括Hoa控制台 。 它旨在成为工业界和研究界之间的桥梁,这使该项目非常有趣。

The Webmozart console is the newest project, wants to be easier, test friendly and add new functionality on top of the Symfony console.

Webmozart控制台是最新的项目,希望更简单,易于测试并在Symfony控制台之上添加新功能。

依赖关系,大小和复杂性 (Dependencies, Size, and Complexity)

The Symfony console has only suggested dependencies, as opposed to the Hoa console library that depends on some Hoa project libraries. The Webmozart project, too, directly depends on the Symfony console.

Symfony控制台仅建议了依赖项,而不是依赖于某些Hoa项目库的Hoa控制台库。 Webmozart项目也直接取决于Symfony控制台。

The Hoa console has the smallest number of LOC (Logical Lines of Code) ~1397, followed by the Symfony console ~2226 and the Webmozart ~3126 (without dependencies).

Hoa控制台的LOC (逻辑代码行)数量最少,〜1397,其次是Symfony控制台〜2226和Webmozart 〜3126(无依赖项)。

In order to have a rough indicator of the complexity of these projects, below is some data from their PHPLOC analysis*:

为了粗略地说明这些项目的复杂性,以下是来自其PHPLOC分析的一些数据*:



DescriptionSymfonyHoaWebmozartCyclomatic ComplexityAverage Complexity per LLOC0.370.360.26Average Complexity per Class14.7325.148.84Average Complexity per Method2.553.381.99DependenciesGlobal Accesses3201Attribute Accesses8072171285Method Calls11033241320 描述 Symfony a Webmozart 圈复杂度 每个LLOC的平均复杂度 0.37 0.36 0.26 每班平均复杂度 14.73 25.14 8.84 每种方法的平均复杂度 2.55 3.38 1.99 依存关系 全局访问 3 20 1个 属性访问 807 217 1285 方法调用 1103 324 1320

*The analysis is performed only in the main source directory, excluding the test folder when present.

*仅在主源目录中执行分析,但不存在测试文件夹。

实际例子 (Practical example)

描述 (Description)

To have an overview of each library’s functionality and see the code in action, let’s write a business feature to describe a usage example:

要概述每个库的功能并查看实际的代码,让我们编写一个业务功能来描述一个用法示例:

Feature: I want to output a message to several people. The message should be passed via the `--message` option and should be optional (default="Hello"), the message should be followed by two or more names, the message should be coloured with `--color=` (default="white") and/or in uppercase with `--up` (default=lowercase).

The final console call should be something like: somemsg --message='Good Morning' Nicola Bruno --color=green --up and the output should be:

最终的控制台调用应类似于: somemsg --message='Good Morning' Nicola Bruno --color=green --up ,输出应为:

GOOD MORNING NICOLA AND BRUNO

实作 (Implementation)

First, we need to define a PHP Message, used in every console implementation, to handle the example.

首先,我们需要定义每个控制台实现中使用PHP Message来处理示例。

Below is some pretty straightforward code:

下面是一些非常简单的代码:

class Message { /** * Construct the class and initializes the properties. * * @param $names * @param string $message * @param bool $uppercase */ public function __construct($names, $message="Hello", $uppercase=false) { $this->names = implode(' and ', $names); $this->message = $message; $this->uppercase = $uppercase; } /** * Generates the output. * * @return string */ public function getMessage() { $output = $this->message . ' ' . $this->names; if ($this->uppercase) { $output = strtoupper($output); } return $output; } }

Symfony控制台 (Symfony console)

In order to create a console command in Symfony, it’s necessary to:

为了在Symfony中创建控制台命令,必须执行以下操作:

Create the command class

创建命令类

– Configure the arguments and options

–配置参数和选项

– Write the logic

–编写逻辑

Create the application

创建应用

创建命令 (Create The command)

use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputArgument; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; class MessageCommand extends Command { /** * Configures the argument and options */ protected function configure() { $this ->setName('demo:msg') ->setDescription('Simple message delivery') ->addArgument('names', InputArgument::IS_ARRAY | InputArgument::REQUIRED, 'Who do you want to message?') ->addOption('message', null, InputOption::VALUE_REQUIRED, 'Set the message', 'Hello') ->addOption('up', null, InputOption::VALUE_NONE, 'Set the output in uppercase') ->addOption('color', null, InputOption::VALUE_REQUIRED, 'Which colors do you like?', 'white') ; } /** * Executes the logic and creates the output. * * @param InputInterface $input * @param OutputInterface $output */ protected function execute(InputInterface $input, OutputInterface $output) { $names = $input->getArgument('names'); $message = $input->getOption('message'); $uppercase = $input->getOption('up'); $color = $input->getOption('color'); $message = new Message($names, $message, $uppercase); $messageString = $message->getMessage(); $coloredMsg = '<fg='.$color.'>'.$messageString.'</fg='.$color.'>'; $output->writeln($coloredMsg); } }

The configure method is used to set up the arguments and options for the command.

configure方法用于设置命令的参数和选项。

The addArgument method can receive the following parameters: addArgument($name, $mode, $description, $default)

addArgument方法可以接收以下参数: addArgument($name, $mode, $description, $default)



typenamedescriptionstring$nameThe argument nameint$modeThe argument mode: InputArgument::REQUIRED or InputArgument::OPTIONALstring$descriptionA description textmixed$defaultThe default value (for InputArgument::OPTIONAL mode only) 类型 名称 描述 串 $名称 参数名称 整型 $模式 参数模式:InputArgument :: REQUIRED或InputArgument :: OPTIONAL 串 $说明 说明文字 混合的 $默认 默认值(仅适用于InputArgument :: OPTIONAL模式)

The addOption can receive the following parameters: addArgument($name, $shortcut, $mode, $description, $default)

addOption可以接收以下参数: addArgument($name, $shortcut, $mode, $description, $default)



typenamedescriptionstring$nameThe option namestring$shortcutThe shortcut (can be null)int$modeThe option mode: One of the InputOption::VALUE_* constantsstring$descriptionA description textmixed$defaultThe default value (must be null for InputOption::VALUE_REQUIRED or InputOption::VALUE_NONE) 类型 名称 描述 串 $名称 选项名称 串 $捷径 快捷方式(可以为空) 整型 $模式 选项模式:InputOption :: VALUE_ *常量之一 串 $说明 说明文字 混合的 $默认 默认值(对于InputOption :: VALUE_REQUIRED或InputOption :: VALUE_NONE必须为null)

There are three options available to color the output:

可以使用三个选项为输出着色:

Use a preset tag (es: $output->writeln('<info>foo</info>'); for green output)

使用预设标签(例如: $output->writeln('<info>foo</info>');用于绿色输出)

Define a style using the OutputFormatterStyle class

使用OutputFormatterStyle类定义样式

Set the color inside the tag name es:

在标签名称es内设置颜色: // red text on a cyan background $output->writeln('<fg=red;bg=cyan>foo</fg=red;bg=cyan>');

The available foreground and background colors are: black, red, green, yellow, blue, magenta, cyan and white. The available options are: bold, underscore, blink, reverse and conceal.

可用的前景色和背景色是:黑色,红色,绿色,黄色,蓝色,洋红色,青色和白色。 可用选项为:粗体,下划线,闪烁,反向和隐藏。

More information in the official Symfony documentation.

有关更多信息,请参见Symfony官方文档 。

Note: by default, the Windows command console doesn’t support output coloring. You’ll need (and should install) Git tools or another more advanced command console.

注意 :默认情况下,Windows命令控制台不支持输出颜色。 您将需要(并且应该安装) Git工具或其他更高级的命令控制台。

创建应用程序 (Create The application)

After the configuration and the execution, we’re almost done. The last step is creating a PHP file to run the command.

在配置和执行之后,我们差不多完成了。 最后一步是创建一个PHP文件来运行命令。

//file myconsole.php require __DIR__.'/vendor/autoload.php'; use MessageCommand; use Symfony\Component\Console\Application; $application = new Application(); $application->add(new MessageCommand()); $application->run();

Console call example:

控制台调用示例:

php myconsole.php demo:msg Nicola Bruno --message='Good Morning' --color=blue --up

The Symfony console also automatically provides the output helper with the --help argument.

Symfony控制台还会自动为输出帮助器提供--help参数。

Hoa控制台 (Hoa console)

The Hoa console follows a less structured approach to configuring the console command.

Hoa控制台采用结构化方法来配置console命令。

That process consists of the following steps:

该过程包括以下步骤:

Parse the command

解析命令 Get options and input

获取选项和输入 Execute the logic

执行逻辑

解析命令 (Parse the command)

/** * $argv contains an array of all the arguments passed to the script, * the first argument is always the name of the PHP file. * the Hoa Parser->parse method accept a string in input, so it's necessary to convert the $argv array in a string without the first argument as below. */ unset($argv[0]); $command = implode(' ', $argv); $parser = new Hoa\Console\Parser(); $parser->parse($command); //options definition //['longname', TYPE, 'shortname'] $options = new Hoa\Console\GetOption( [ ['up', Hoa\Console\GetOption::NO_ARGUMENT, 'u'], ['message', Hoa\Console\GetOption::REQUIRED_ARGUMENT, 'm'], ['color', Hoa\Console\GetOption::OPTIONAL_ARGUMENT, 'c'] ], $parser );

获取选项和输入 (Get options and inputs)

//definition of default values $uppercase = false; $message = "Hello"; $color = "white"; $names = $parser->getInputs(); //The following while with the switch will assign the values to the variables. while (false !== $shortName = $options->getOption($value)) { switch ($shortName) { case 'u': $uppercase = true; break; case 'm': $message = $value; break; case 'c': $color = $value; break; } }

执行逻辑 (Execute the logic)

$message = new Message($names, $message, $uppercase); $messageString = $message->getMessage(); Hoa\Console\Cursor::colorize('fg('.$color.')'); echo $messageString; Hoa\Console\Cursor::colorize('fg(white)'); //reset the cursor to default white

For coloring the output, it’s possible to change the Cursor color.

为了给输出着色,可以更改Cursor颜色。

The Hoa console supports a wide range of colors. The color can be set by name (black, red, green, yellow…), by number (from 0 to 256 representing the 264 color palette) or by hexadecimal code #rrggbb, example: Hoa\Console\Cursor::colorize('fg(yellow) bg(#932e2e) underlined');

Hoa控制台支持多种颜色。 可以通过名称(黑色,红色,绿色,黄色...),数字(从0到256代表264调色板)或十六进制代码#rrggbb设置颜色,例如: Hoa\Console\Cursor::colorize('fg(yellow) bg(#932e2e) underlined');

The basic usage in the example doesn’t provide an automatic helper output, and is not strongly OOP oriented but extending the Hoa\Console\Dispatcher\Kit (requires hoa/dispatcher) could add more flexibility (more information in the official documentation)

示例中的基本用法不提供自动帮助程序输出,也不是严格面向OOP的,但是扩展Hoa\Console\Dispatcher\Kit (需要hoa/dispatcher )可以增加更多的灵活性(更多信息在官方文档中 )

The command can be called with:

可以使用以下命令调用该命令:

php message.php -u --message=Hello --color=green Nicola Bruno

One of the strongpoints of the Hoa console is that it provides additional API classes to manipulate important elements, supporting the different terminal profiles:

Hoa控制台的优点之一是它提供了其他API类来操作重要元素,并支持不同的终端配置文件:

The Cursor (move, clear, show, colorize, …)

Cursor (移动,清除,显示,着色等)

The Mouse (listening to mouse action)

Mouse (听鼠标动作)

The Window (setSize, scroll, minimize, …)

Window (setSize,滚动,最小化……)

The terminal line with Readline (history, autocompletion, etc)

带有Readline的终端线(历史记录,自动完成等)

Webmozart控制台 (Webmozart console)

The Webmozart console command creation workflow consists of:

Webmozart控制台命令创建工作流程包括:

configuring the argument and options

配置参数和选项 writing the logic

编写逻辑 creating the application

创建应用程序

Webmozart’s console follows an approach similar to the Symfony Console, but with a clear separation between the configuration and the logical execution.

Webmozart的控制台采用与Symfony控制台类似的方法,但在配置和逻辑执行之间有明确的区分。

组态 (Configuration)

use Webmozart\Console\Api\Args\Format\Argument; use Webmozart\Console\Api\Args\Format\Option; use Webmozart\Console\Config\DefaultApplicationConfig; /** * Configuration of arguments and options */ class MsgApplicationConfig extends DefaultApplicationConfig { protected function configure() { parent::configure(); $this ->setName('msg') ->setVersion('0.1') ->beginCommand('msg') ->setDescription('Show a nice message') ->setHandler(new MsgCommandHandler()) ->addArgument('names', Argument::MULTI_VALUED | Argument::REQUIRED, 'Who do you want to message?') ->addOption('message', null, Option::OPTIONAL_VALUE, 'Set the message', 'Hello') ->addOption('up', null, Option::NO_VALUE, 'Set the output in uppercase') ->addOption('color', null, Option::REQUIRED_VALUE, 'Which colors do you like?', 'white') ->end() ; } }

逻辑 (Logic)

use Webmozart\Console\Api\Args\Args; use Webmozart\Console\Api\IO\IO; /** * Handling the command logic */ class MsgCommandHandler { public function handle(Args $args, IO $io) { //gets the argument and option $names = $args->getArgument('names'); $message = $args->getOption('message'); $uppercase = $args->getOption('up'); $color = $args->getOption('color'); $message = new Message($names, $message, $uppercase); $messageString = $message->getMessage(); $coloredMsg = '<fg='.$color.'>'.$messageString.'</fg='.$color.'>'; $io->writeLine($coloredMsg); } }

The strong separation of configuration and logic allows more flexibility for easy testing and for a project that will grow with additional commands.

配置和逻辑之间的强烈分离为轻松的测试以及随附加命令扩展的项目提供了更大的灵活性。

Other advantages of the Webmozart’s console are:

Webmozart控制台的其他优点是:

sub commands support:

子命令支持:

php mycommand.php msg send --arg1 --arg2 php mycommand.php msg receive --someoptions=somevalue

support for manpage documentation (like with “git help remote”)

支持联机帮助文档(例如“ git help remote”)

adapters for the Symfony console (to use Symfony’s classes like ProgressBar)

Symfony控制台的适配器(以使用Symfony的类(例如ProgressBar))

创建应用程序 (Creating The application)

The application file to run the command is similar to the Symfony one:

运行命令的应用程序文件类似于Symfony之一:

require 'vendor/autoload.php'; use Webmozart\Console\ConsoleApplication; $cli = new ConsoleApplication(new MsgApplicationConfig()); $cli->run();

Console call:

控制台呼叫:

php myconsole.php msg --message='Good Morning' Nicola Bruno --color=blue --up

最后的想法 (Final Thoughts)

Each console covered above provides different functionalities for different use types and user preferences.

上面介绍的每个控制台为不同的使用类型和用户首选项提供不同的功能。

The Symfony console is well tested, robust, with good documentation and features to solve most of the average use cases.

Symfony控制台经过了良好的测试,功能强大,具有完善的文档和功能,可以解决大多数平均用例。 The Hoa console is more industry oriented, perfect for manipulating the terminal environment (mouse, cursor, window, and so on).

Hoa控制台更面向行业,非常适合操纵终端环境(鼠标,光标,窗口等)。 The Webmozart console is new (there will be a stable release soon) but it’s very useful for handling projects that tend to grow to large sizes.

Webmozart控制台是新的(很快将有稳定的版本),但是对于处理趋于大型的项目非常有用。

Do you use any of them regularly? If so, which one? Why? What would you say the pros and cons of each are? Do you have other contenders to suggest? Let us know!

您是否经常使用它们? 如果是这样,哪一个? 为什么? 您会说每种优点和缺点是什么? 您还有其他建议吗? 让我们知道!

翻译自: https://www.sitepoint.com/console-wars-php-cli-libraries/

华为防火墙cli控制台

相关资源:华为 ME60 命令CLI -最新
最新回复(0)