Commando\Option::setRule PHP Method

setRule() public method

public setRule ( Closure | string $rule ) : Option
$rule Closure | string regex, closure
return Option
    public function setRule($rule)
    {
        $this->rule = $rule;
        return $this;
    }

Usage Example

 public function testRule()
 {
     $option = new Option('f');
     $option->setRule(function ($value) {
         return is_numeric($value);
     });
     $this->assertFalse($option->validate('a'));
     $this->assertFalse($option->validate('abc'));
     $this->assertTrue($option->validate('2'));
     $this->assertTrue($option->validate(2));
     $this->assertTrue($option->validate(0));
     $option->setValue(2);
     $caught = false;
     try {
         $option->setValue('abc');
     } catch (\Exception $e) {
         $caught = true;
     }
     $this->assertTrue($caught);
 }