nunomaduro/laravel-console-menu

Laravel Console Menu 是 Laravel/Laravel Zero 命令的输出方式。

v3.5.0 2024-03-05 12:53 UTC

This package is auto-updated.

Last update: 2024-09-05 13:59:38 UTC


README

Static Analysis Latest Stable Version License

关于 Laravel Console Menu

Laravel Console Menu 由 Nuno Maduro 创建并维护,是 php-school/cli-menu 的 Laravel Console 命令包装器。

安装

需要 PHP 8.1+

使用 Composer 安装 Laravel Console Menu

composer require nunomaduro/laravel-console-menu

使用方法

快速设置

class MenuCommand extends Command
{
    /**
     * Execute the console command.
     *
     * @return void
     */
    public function handle()
    {
        $option = $this->menu('Pizza menu', [
            'Freshly baked muffins',
            'Freshly baked croissants',
            'Turnovers, crumb cake, cinnamon buns, scones',
        ])->open();

        $this->info("You have chosen the option number #$option");
    }
}

带问题的设置

class MenuCommand extends Command
{
    /**
     * Execute the console command.
     *
     * @return void
     */
    public function handle()
    {
        $option = $this->menu('Pizza menu')
                    ->addOption('mozzarella', 'Mozzarella')
                    ->addOption('chicken_parm', 'Chicken Parm')
                    ->addOption('sausage', 'Sausage')
                    ->addQuestion('Make your own', 'Describe your pizza...')
                    ->addOption('burger', 'Prefer burgers')
                    ->setWidth(80)
                    ->open();
        
        $this->info("You have chosen the text option: $option");
    }
}

带高级选项的设置,在这种情况下,是一个密码

class MenuCommand extends Command
{
    /**
     * Execute the console command.
     *
     * @return void
     */
    public function handle()
    {
        $menu = $this->menu('Pizza menu')
                    ->addOption('mozzarella', 'Mozzarella')
                    ->addOption('chicken_parm', 'Chicken Parm')
                    ->addOption('sausage', 'Sausage')
                    ->addQuestion('Make your own', 'Describe your pizza...');
        
        $itemCallable = function (CliMenu $cliMenu) use ($menu) {
            $cliMenu->askPassword()
                ->setValidator(function ($password) {
                    return $password === 'secret';
                })
                ->setPromptText('Secret password?')
                ->ask();

            $menu->setResult('Free spice!');

            $cliMenu->close();
        };
        $menu->addItem('Add extra spice for free (password needed)', $itemCallable);


        $option = $menu->addOption('burger', 'Prefer burgers')
            ->setWidth(80)
            ->open();

        $this->info("You have chosen the text option: $option");
    }
}

外观

可用颜色: blackredgreenyellowbluemagentacyanwhite

  $this->menu($title, $options)
      ->setForegroundColour('green')
      ->setBackgroundColour('black')
      ->setWidth(200)
      ->setPadding(10)
      ->setMargin(5)
      ->setExitButtonText("Abort") // remove exit button with ->disableDefaultItems()
      ->setTitleSeparator('*-')
      ->addLineBreak('<3', 2)
      ->addStaticItem('AREA 2')
      ->open();

查看完整的文档 这里

贡献

感谢您考虑为 Laravel Console Menu 做出贡献。所有贡献指南均在此处提及 此处

您可以查看 变更日志 以获取持续更新及有关更改的详细信息。您还可以关注推特账号以获取最新公告或只是打个招呼:@enunomaduro

支持开发

你喜欢这个项目吗?通过捐赠来支持它

许可证

Laravel Console Menu 是一个开源软件,在 MIT 许可证 下授权。