-
Notifications
You must be signed in to change notification settings - Fork 24
Option Constraint
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');$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');Related Works:
- CLIFramework: https://github.com/c9s/CLIFramework
Applications using GetOptionKit and CLIFramework:
- PHPBrew: https://github.com/phpbrew/phpbrew
- LazyRecord: https://github.com/c9s/LazyRecord
- PHPRelease: https://github.com/c9s/PHPRelease
- AssetKit: https://github.com/c9s/AssetKit
- Onion: https://github.com/c9s/Onion