sitphp/commands

一个简单而强大的库,用于从其他库运行命令或构建命令应用程序

v1.0.2 2019-07-10 07:41 UTC

This package is auto-updated.

Last update: 2024-09-10 19:28:27 UTC


README

Build Status

"sitphp/commands" 库可以帮助您轻松地为您的应用程序或库创建命令。您还可以使用它来构建自己的定制命令工具。

查看完整文档这里

command showcase

安装

在您的 composer.json 文件的 "require" 部分添加以下行 "sitphp/commands": "1.0.*"

{
    "require": {
        "sitphp/commands": "1.0.*"
    }
}

然后只需运行以下 composer 命令来安装库

composer update

创建一个命令

要构建一个新的命令,您应该在您的库或应用程序的 "Commands" 文件夹中创建一个新的类,该类扩展了 \SitPHP\Commands\Command 类。这个类应该实现 handle 方法。让我们创建一个名为 "YourCommand" 的类,例如

namespace App\Commands;

class YourCommand extends \SitPHP\Commands\Command {

    function handle(){
        $this->write('hello');
    }

}

运行一个命令

要运行您的命令,您应该使用位于 /vendor/bin 文件夹中的 command 应用程序。例如,要运行我们之前创建的 "YourCommand" 命令,请使用缩写表示法(命名空间:命令名称)

vendor/bin/command App:YourCommand

或使用完整路径(使用斜杠 "/" 而不是反斜杠 "" 替换类名称)

vendor/bin/command App/Commands/YourCommand

写入文本消息

要在您的终端中写入消息,请使用 writewriteLn 方法。writeLn 方法将在新行上写入消息,而 write 方法将在同一行上写入消息。

您还可以使用 lineBreak 方法来显示换行符。此方法可以接收一个整数参数来指定您希望写入多少行换行符。

namespace App\Commands;

class YourCommand extends \SitPHP\Commands\Command {

    function handle(){
        $this->write('Hello,');
        
        // Single line break
        $this->lineBreak();

        $this->write('I am ');
        $this->write('Alex');
        
        // Double line break
        $this->lineBreak(2);

        $this->write('I code with PHP');
    }

}

command write

参数和选项

为了检索传递给您的命令的参数和选项,您必须首先在您的命令类的 prepare 方法中注册它们。

  • 要注册一个参数,请使用 setArgumentInfos 方法,并指定参数名称及其位置(如果是第一个参数,则为 0,如果是第二个参数,则为 1,依此类推)
  • 要注册一个选项,请使用 setOptionInfos 方法,并指定选项名称。例如,我们将注册 "name" 参数和一个 "color" 选项。
// In your command class ...

function prepare()
{
   // Register "name" argument at position "0"
   $this->setArgumentInfos('name', 0);

   // Register "color" option
   $this->setOptionInfos('color');
}

function handle()
{
   // Retrieve name argument value
   $name = $this->getArgument('name');
   if ($name === null) {
       throw new \Exception('The "name" argument is required');
   }
   $message = 'My name is ' . $name;
   
   // Retrieve color option value
   $color = $this->getOption('color');
   if ($color !== null) {
       $message .= ' and I like the ' . $color . ' color';
   }

   $this->writeLn($message);
}

要将参数发送到您的命令,只需在您的终端中输入它们的值。选项应前面加上两个连字符(例如:--color)。选项可以像这样接受值 --color=red。如果没有指定值,则选项值将为 true

您可以在终端中键入类似以下的内容来运行我们的上一个命令

vendor/bin/command App:YourCommand Alex --color=red

这将写入:"我的名字是Alex,我喜欢红色这个颜色"。

样式

您可以使用 <cs> 标签轻松地为终端中写入的任何内容设置样式。

  • 您可以使用 color 属性更改文本颜色。可用的颜色有:'black'、'white'、'red'、'green'、'yellow'、'blue'、'purple'、'cyan'、'light_grey'、'dark_grey'、'light_red'、'light_green'、'light_yellow'、'light_blue'、'pink'、'light_cyan'。
  • 您可以使用 background-color 属性更改文本的背景颜色。可用的颜色有:'black'、'white'、'red'、'green'、'yellow'、'blue'、'purple'、'cyan'、'light_grey'、'dark_grey'、'light_red'、'light_green'、'light_yellow'、'light_blue'、'pink'、'light_cyan'。
  • 您可以使用 style 属性的 bold 参数使文本加粗
  • 您可以使用 style 属性的 highlight 参数突出显示文本
  • 您可以使用 style 属性的 underline 参数下划线文本
  • 您可以使用style属性的blink参数使文本闪烁(某些终端可能不支持闪烁)

以下是一些样式示例

// In the "handle" method of your command class ...
$this->writeLn('This will display in <cs color="blue">blue</cs>');
$this->writeLn('This will display <cs style="bold;highlight">highlighted and bold</cs>');
$this->writeLn('This will display <cs color="white" background-color="blue">with a white text in a blue background</cs>');

command style

工具

本软件包附带一些有用的工具。如果您使用的是自己的命令应用,也可以轻松构建自己的工具。

区块工具

区块工具可以显示在框中的内容。区块通过bloc方法创建,并通过display方法显示。区块的宽度将自动调整到内容的宽度。

// In the "handle" method of your command class ...
$this->bloc('I am a simple bloc ...')
    ->display();

进度条工具

要创建进度条,请使用带有指定进度条步数的progress方法。然后使用display方法显示它。您可以使用progress方法将进度条向前移动。您可能还想使用placeHere方法“固定”进度条,这样它就不会在每次进度中显示在新的一行上。

// In the "handle" method of your command class ...

// Create a 5 steps progress bar
$progress_bar = $this->progressBar(5)
    ->placeHere()
    ->display();

for($i = 1; $i <= 5; $i++){
    sleep(1);
    $progress_bar->progress();
}

command progress bar

问题工具

问题工具允许您请求用户输入。使用question方法创建一个新的问题。此方法可以接受两个参数:问题提示和自动完成值数组。

// In the "handle" method of your command class ...
function handle(){
    $genres = ['pop', 'rock', 'hip hop', 'classical'];
    $genre = $this->question('Which music genre do you like ?', $genres)
        ->ask();
    
    $this->lineBreak();
    $this->writeLn('Your favorite music genre is : '.$genre);
}

command question

选择工具

选择工具允许您在预定义的选择集中请求用户选择。使用choice方法创建新的选择,并使用ask方法请求用户选择。您还可以使用enableQuit方法让用户在回答之前退出。选择问题将重新显示,直到用户给出正确的选择或退出(如果可能)。当用户选择退出时,选择方法将返回null

choice方法可以接受最多三个参数:选择数组、问题提示和标题。

// In the "handle" method of your command class ...
function handle(){
    $choices = ['red', 'blue', 'green'];
    $color_index = $this->choice($choices, 'Which color do you like best ?', 'Colors')
        ->enableQuit()
        ->ask();
        
    if($color_index !== null){   
        $this->lineBreak(); 
        $this->writeLn('You like the '.$choices[$color_index].' color the best');
    }
}

command choice

章节工具

章节用于更新或移动屏幕上预定义位置的内容。您可以使用section方法创建章节,并使用placeHere方法将其放置在您决定的位置。章节中的所有内容都将写入放置的位置。以下是一个示例来说明这一点

// In the "handle" method of your command class ...
$this->writeLn('This goes before');
$section = $this->section()->placeHere();
$this->writeLn('This goes after');

$section->writeLn('This goes in the <cs color="blue">middle</cs>');
sleep(1);
$section->overwriteLn('This goes in the <cs color="red">middle</cs>');

command section

表格工具

您可以使用表格工具显示组织成行和列的内容。使用table方法创建表格。然后定义数组中的每个表格行。您还可以使用line项插入表格。

// In the "handle" method of your command class ...
$this->table([
    ['<cs style="bold">Animal</cs>', '<cs style="bold">Classification</cs>'],
    'line',
    ['elephant', 'mammal'],
    ['parrot', 'bird']
])->display();

table command