pharext\Cli\Args::validate PHP Method

validate() public method

The Generator yields any validation errors.
public validate ( ) : Generator
return Generator
    public function validate()
    {
        $required = array_filter($this->orig, function ($spec) {
            return $spec[3] & self::REQUIRED;
        });
        foreach ($required as $req) {
            if ($req[3] & self::MULTI) {
                if (is_array($this[$req[0]])) {
                    continue;
                }
            } elseif (strlen($this[$req[0]])) {
                continue;
            }
            if (is_numeric($req[0])) {
                (yield sprintf("Argument <%s> is required", $req[1]));
            } else {
                (yield sprintf("Option --%s is required", $req[1]));
            }
        }
    }

Usage Example

Beispiel #1
0
 public function testValidate()
 {
     $this->args->compile([["r", "required-option", "This option is required", CliArgs::REQUIRED | CliArgs::NOARG]]);
     foreach ($this->args->parse(0, []) as $error) {
         throw new \Exception("Unexpected parse error: {$error}");
     }
     foreach ($this->args->validate() as $error) {
         $this->assertStringMatchesFormat("%srequired-option%srequired", $error);
     }
     $this->assertTrue(isset($error));
 }