Skip to content
Yo-An Lin edited this page Jul 6, 2015 · 3 revisions

To validate the type of given option value, you can setup an isa property for each option:

$opt->add( 'f|foo:' , 'with string type value' )
    ->isa('string');

To ensure the given value is a number, e.g., [0-9]+. options with number type will be validated with the built-in PHP is_numeric function:

$opt->add( 'b|bar+' , 'with number type value' )
    ->isa('number');

To ensure the given value is a valid boolean value, e.g., true, false, 0, 1:

$opt->add( 'z|zoo?' , 'with boolean type value' )
    ->isa('boolean');

To ensure the given value is an existing file:

$opt->add( 'file:', 'with file type value' )
    ->isa('file');

To ensure the given value is a valid datetime string, the value will be passed to DateTime object:

$opt->add( 'date:' , 'with date type value' )
    ->isa('date');

Other Type Constraints

$specs->add('ip+', 'Ip constraint' )
    ->isa('Ip');

$specs->add('ip+', 'Ip constraint' )
    ->isa('Ipv4');

$specs->add('email+', 'Email address constraint' )
    ->isa('Email');

$specs->add('z|zoo?', 'option with optional value.' )
    ->isa('Boolean');

$specs->add('file:', 'option value should be a file.' )
    ->isa('File');

Clone this wiki locally