grano22/simplecli 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

grano22/simplecli

最新稳定版本:v1.0.0

Composer 安装命令:

composer require grano22/simplecli

包简介

Create CLI apps easily with modular and user-friendly way

README 文档

README

Project details

  • Version: v1.0.0

Project Motto

'Create CLI Apps with easy and modular way :)'

Description

This project is created to make much easier to create awesome CLI app with additional modules (addons) to handle various use cases for example: OutputWriter is standalone component, also TUI and more! :)

Features

  • Parse input args into top level command, subcommands, arguments and options,
  • Multi levels commands (coming soon),
  • Event-driven way - handle any kind of errors and other actions,
  • Modular - App addons (coming soon),

Examples

General example:

use Grano22\SimpleCli\App\Factory\SimpleCliUniversalFilterConstraint;
use Grano22\SimpleCli\App\SimpleCliAppFactory;
use Grano22\SimpleCli\Command\Input\SimpleCliArgument;
use Grano22\SimpleCli\Command\Input\SimpleCliCommandInput;
use Grano22\SimpleCli\Command\Input\SimpleCliOption;

$myAwesomeCliApp = SimpleCliAppFactory::create()
    // Define help independent option for all commands
    ->withCommandsOption(
        commandNames: SimpleCliUniversalFilterConstraint::wildcard('*'),
        name: 'help',
        options: SimpleCliOption::OPTIONAL | SimpleCliOption::IGNORE_REST_REQUIRED | SimpleCliOption::NEGABLE
    )
    // Add your custom unknown error handler
    ->setUnexpectedErrorHandler(static function(Exception $exception) {
        // Do something with your custom exception
        
        echo $exception->getMessage();
        
        exit(1);
    })
    ->withCommandsArguments(
        commandNames: ['greetings'],
        name: 'tone',
        options: SimpleCliArgument::REQUIRED
    )
    ->withCommandOption(
        commandName: ['greetings', 'info'],
        name: 'verbose',
        options: SimpleCliOption::OPTIONAL
    )
    // Define your command logic :)
    ->withCommand(
        name: 'greetings',
        executionLogic: static function(SimpleCliCommandInput $input): int {
            $message = "Hello world";
                    
            $tone = $input->getArguments()->getByName('tone')->getValue();
            
            if ($tone = 'high') {
                $message .= '!';
            }
            
            echo $message;
        
            return 0;
        }
    )
    ->build()
;

$myAwesomeCliApp->autoExecuteCommand();

Adding event listeners:

$myAwesomeCliApp = SimpleCliAppFactory::create()
    // Define your event listener for one or multiple events
    ->addEventsListener(static function(CommandNotFoundEvent|MissingArgumentEvent $event) {
        echo "Missing argument or command";
        
        exit(1);
    }, [CommandNotFoundEvent::class, MissingArgumentEvent::class])
;

Adding options with aliases and default values

$myAwesomeCliApp = SimpleCliAppFactory::create()
    ->withCommandOption(
        commandName: 'cmd1',
        name: 'optWithAliases',
        options: SimpleCliOption::REQUIRED,
        aliases: ['l', 'q']
    )
    ->withCommandOption(
        commandName: 'cmd1',
        name: 'optWithDefaultValue',
        options: SimpleCliOption::OPTIONAL,
        defaultValue: 'DEFAULT_VALUE'
    )

统计信息

  • 总下载量: 4
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2023-09-16