12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485 |
- <?php
- namespace Boris;
- class CLIOptionsHandler {
-
- public function handle($boris) {
- $args = getopt('hvr:', array('help', 'version', 'require:'));
- foreach ($args as $option => $value) {
- switch ($option) {
-
- case 'r':
- case 'require':
- $this->_handleRequire($boris, $value);
- break;
-
- case 'h':
- case 'help':
- $this->_handleUsageInfo();
- break;
-
- case 'v':
- case 'version':
- $this->_handleVersion();
- break;
- }
- }
- }
-
- private function _handleRequire($boris, $paths) {
- $require = array_reduce(
- (array) $paths,
- function($acc, $v) { return array_merge($acc, explode(',', $v)); },
- array()
- );
- $boris->onStart(function($worker, $scope) use($require) {
- foreach($require as $path) {
- require $path;
- }
- $worker->setLocal(get_defined_vars());
- });
- }
- private function _handleUsageInfo() {
- echo <<<USAGE
- Usage: boris [options]
- boris is a tiny REPL for PHP
- Options:
- -h, --help show this help message and exit
- -r, --require a comma-separated list of files to require on startup
- -v, --version show Boris version
- USAGE;
- exit(0);
- }
- private function _handleVersion() {
- printf("Boris %s\n", Boris::VERSION);
- exit(0);
- }
- }
|