shomisha/laravel-console-wizard

此包为在Laravel应用程序中创建交互式控制台向导提供了一个框架。

1.12.0 2024-05-04 13:21 UTC

This package is auto-updated.

Last update: 2024-09-04 14:14:03 UTC


README

Latest Stable Version Software License

此包提供了一个基础,用于在控制台中创建具有复杂输入的多步向导。它最适合用于定制生成器命令的输出,但也可用于处理任何类型的任务。

<?php

namespace App\Console\Commands;

use Shomisha\LaravelConsoleWizard\Command\Wizard;
use Shomisha\LaravelConsoleWizard\Steps\ChoiceStep;
use Shomisha\LaravelConsoleWizard\Steps\TextStep;

class IntroductionWizard extends Wizard
{
    protected $signature = "wizard:introduction";

    protected $description = 'Introduction wizard.';

    public function getSteps(): array
    {
        return [
            'name'   => new TextStep("What's your name?"),
            'age'    => new TextStep("How old are you?"),
            'gender' => new ChoiceStep("Your gender?", ["Male", "Female"]),
        ];
    }

    public function completed()
    {
        $this->line(sprintf(
            "This is %s and %s is %s years old.",
            $this->answers->get('name'),
            ($this->answers->get('gender') === 'Male') ? 'he' : 'she',
            $this->answers->get('age')
        ));
    }
}

上面的示例展示了如何创建一个包含多个输入提示的向导,并使用用户提供的答案执行操作。在您的控制台中运行 php artisan wizard:introduction 将执行上述向导并生成以下输出

shomisha:laravel-console-wizard shomisha$ php artisan wizard:introduction

 What's your name?:
 > Misa

 How old are you?:
 > 25

 Your gender?:
  [0] Male
  [1] Female
 > 0

This is Misa and he is 25 years old.

请查看我们的wiki页面获取更多说明和其他向导功能。