palmtree/argparser

Palmtree PHP 的 ArgParser 组件

v2.1.0 2021-03-22 18:03 UTC

This package is auto-updated.

Last update: 2024-08-23 02:31:49 UTC


README

License

解析参数数组并调用匹配的设置器或与默认参数合并。

需求

  • PHP >= 7.1

安装

使用 composer 将包添加到依赖项

composer require palmtree/argparser

用法

<?php

use Palmtree\ArgParser\ArgParser;

class SomeClass {
    public static $defaultArgs = [
        'force' => false,
    ];

    private $name;
    private $args = [];

    public function __construct($args = []) {
        $parser = new ArgParser($args);

        $parser->parseSetters($this);
        $this->args = $parser->resolveOptions(static::$defaultArgs);
    }

    public function setName($name) {
        $this->name = $name;
    }
}
<?php
// Calls $obj->setName('Andy') and sets the force arg to true
$obj = new SomeClass([
    'name' => 'Andy',
    'force' => true,
]);

许可证

MIT 许可证 下发布