yannoff/console

一个简单、轻量级的命令行PHP应用程序实现。

2.3.0 2024-03-09 20:33 UTC

This package is auto-updated.

Last update: 2024-09-09 23:56:28 UTC


README

一个简单、轻量级的命令行PHP应用程序实现。

Latest Stable Version Total Downloads License

我们在这里的原因?

这个库最初是作为symfony/console组件的替代品而构思的。

事实上,symfony组件可能适合快速应用程序开发或概念验证,但另一方面,似乎并不是大多数用例的最佳选择,因为其中只有少数功能是必需的。

因此,需要yannoff/console组件。

然而,这个想法并不是要重新发明轮子,而仅仅是提供一个更简单、更轻量(在大小和资源上)且更符合POSIX规范的PHP命令行应用程序实现。

安装

通过 composer

$ composer require yannoff/console

用法

这里是一个 你好,世界 命令示例

首先是应用程序脚本,该脚本将通过 php bin/app.php 调用

#!/usr/bin/env php
<?php
// bin/app.php

// Mute PHP deprecation warnings & notices
error_reporting(E_ERROR);

require __DIR__ . '/../vendor/autoload.php';

use Yannoff\Component\Console\Application;
use Acme\Demo\HelloCommand;

$application = new Application('acme', '0.0.0');

$application->addCommands([
    new HelloCommand('hello'),
]);

$application->run();

然后是命令文件

<?php
// src/Acme/Demo/

namespace Acme\Demo;

use Yannoff\Component\Console\Command;
use Yannoff\Component\Console\Definition\Argument;
use Yannoff\Component\Console\Definition\Option;

class HelloCommand extends Command
{
    public function configure()
    {
        $this
            ->setName('hello')
            ->setHelp('Hello world')
            ->setDescription('Hello world demo application')
            ->addArgument(
                'name',
                Argument::OPTIONAL,
                'Optional name to greet'
            )
            ->addOption(
                'upper',
                'u',
                Option::FLAG,
                'Print the greetings in upper case'
            )
            ;
    }

    public function execute()
    {
        $name = $this->getArgument('name');
        $upper = $this->getOption('upper');

        $message = 'Hello ' . (null === $name ? 'World' : $name);
        if ($upper) {
            $message = strtoupper($message);
        }

        $this->write($message);

        return 0;
    }
}

那些想要从symfony/console迁移过来的人可能想看看文档中的迁移部分。

许可

MIT 许可证下许可。