예제 #1
0
 public function test()
 {
     $console = new GenericConsole();
     $argv = array('my-cmd.php', '-abc', '-d=v', '-test', '--opt=test');
     $parser = new ArgvParser($argv);
     $parser->parse($console);
     $this->assertEquals(null, $console->getOption('a'));
 }
예제 #2
0
 /**
  * Runs the specified command
  * @param   Event          $event       The event
  * @return  int                         The command exit code
  * @throws
  */
 public function run(Event $event = null)
 {
     if ($event == null) {
         //create the event
         $event = new Event();
         $event->setName(self::EVENT_DISPATCH)->setApplication($this)->setInputStream(new PhpInputStream(STDIN, false))->setOutputStream(new PhpOutputStream(STDOUT, false))->setErrorStream(new PhpOutputStream(STDERR, false))->setOptions(array())->setArguments(array());
         //parse the command line arguments
         $parser = new ArgvParser();
         $parser->parse($event);
     }
     //start listening for signals
     $this->setUpSignalHandler();
     try {
         //get the command name
         if (count($this->commands) > 1) {
             //check for the command argument
             if (!$event->hasArgument(0)) {
                 throw new \InvalidArgumentException('Command argument required');
             }
             //get the name argument
             $name = $event->getArgument(0);
             //remove the command argument from the arguments
             $argv = $event->getArguments();
             array_shift($argv);
             $event->setArguments($argv);
         } else {
             $name = null;
         }
         //get the command to run
         if (!($command = $this->getCommand($name))) {
             throw new \InvalidArgumentException("Command \"{$name}\" not found.");
         }
         //set the command
         $event->setCommand($command);
         //trigger the event
         $this->em->trigger($event);
     } catch (\Exception $exception) {
         //stop listening for signals
         $this->tearDownSignalHandler();
         //trigger the exception event
         try {
             $eevent = clone $event;
             $eevent->setName(self::EVENT_EXCEPTION)->setException($exception);
             $this->em->trigger($eevent);
         } catch (\Exception $exception) {
             $this->printError($event, "An unhandled exception occurred: {$exception->getMessage()}");
         }
         return -1;
     }
     //stop listening for signals
     $this->tearDownSignalHandler();
     return $event->getExitCode();
 }