nacosvel/console

The Nacosvel Console package.

v1.0.0 2024-09-02 10:52 UTC

This package is auto-updated.

Last update: 2024-09-03 04:49:58 UTC


README

GitHub Tag Total Downloads Packagist Version Packagist PHP Version Support Packagist License

环境和依赖

安装

推荐使用 PHP 包管理工具 Composer 安装 SDK:

composer require nacosvel/console

Getting Started

php ns
Nacosvel Console 1.0.0

Usage:
  command [options] [arguments]

Options:
  -h, --help            Display help for the given command. When no command is given display help for the list command
  -q, --quiet           Do not output any message
  -V, --version         Display this application version
      --ansi|--no-ansi  Force (or disable --no-ansi) ANSI output
  -n, --no-interaction  Do not ask any interactive question
  -v|vv|vvv, --verbose  Increase the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Available commands:
  completion  Dump the shell completion script
  help        Display help for a command
  list        List commands

Quick Examples

创建命令类

<?php

namespace Nacosvel\Console\Command;

use Symfony\Component\Console\Input\InputArgument;
use Symfony\Component\Console\Input\InputOption;

class DemoCommand extends Command
{
    /**
     * @var string|null The default command name
     */
    protected static $defaultName = 'demo';

    /**
     * @var string|null The default command description
     */
    protected static $defaultDescription = 'demo description';

    protected function handle(): int
    {
        $this->line($this->argument('name'));
        $this->line($this->option('option'));

        $this->line('line');
        $this->newLine();
        $this->info('info');
        $this->warn('warn');
        $this->error('error');
        $this->question('question');
        $this->comment('comment');

        return self::SUCCESS;
    }

    /**
     * Get the console command arguments.
     *
     * @return array
     */
    protected function getArguments(): array
    {
        return [
            ['name', InputArgument::OPTIONAL, 'The name of the class', 'demo'],
        ];
    }

    /**
     * Get the console command options.
     *
     * @return array
     */
    protected function getOptions(): array
    {
        return [
            ['option', 'o', InputOption::VALUE_OPTIONAL, 'description', 'default'],
        ];
    }

}

注册命令

<?php

use Nacosvel\Console;

$console = new Console\Kernel();
$console->add(new Console\Command\DemoCommand());
$console->run();

配置命令自动注册

{
    // ...
    "extra": {
        "nacosvel": {
            "commands": [
                // "Nacosvel\\Console\\Command\\DemoCommand"
                // Add your custom Command
            ]
        }
    }
}

执行命令

php ns demo nacosvel -o console

License

Guzzle is made available under the MIT License (MIT). Please see License File for more information.