/**
  * Returns methods annotated with a given annotatoons
  *
  * @param  lang.XPClass $self
  * @param  string $annotation
  * @return lang.reflect.Method[]
  */
 public static function methodsAnnotatedWith(\lang\XPClass $self, $annotation)
 {
     $name = substr($annotation, 1);
     $r = array();
     foreach ($self->getMethods() as $method) {
         if ($method->hasAnnotation($name)) {
             $r[] = $method;
         }
     }
     return $r;
 }
Beispiel #2
0
 /**
  * Show usage
  *
  * @param  lang.XPClass class
  * @return void
  */
 protected function commandUsage(XPClass $class)
 {
     // Description
     if (null !== ($comment = $class->getComment())) {
         self::$err->writeLine(self::textOf($comment));
         self::$err->writeLine(str_repeat('=', 72));
     }
     $extra = $details = $positional = [];
     foreach ($class->getMethods() as $method) {
         if (!$method->hasAnnotation('arg')) {
             continue;
         }
         $arg = $method->getAnnotation('arg');
         $name = strtolower(preg_replace('/^set/', '', $method->getName()));
         $comment = self::textOf($method->getComment());
         if (0 == $method->numParameters()) {
             $optional = true;
         } else {
             list($first, ) = $method->getParameters();
             $optional = $first->isOptional();
         }
         if (isset($arg['position'])) {
             $details['#' . ($arg['position'] + 1)] = $comment;
             $positional[$arg['position']] = $name;
         } else {
             if (isset($arg['name'])) {
                 $details['--' . $arg['name'] . ' | -' . (isset($arg['short']) ? $arg['short'] : $arg['name'][0])] = $comment;
                 $extra[$arg['name']] = $optional;
             } else {
                 $details['--' . $name . ' | -' . (isset($arg['short']) ? $arg['short'] : $name[0])] = $comment;
                 $extra[$name] = $optional;
             }
         }
     }
     // Usage
     asort($positional);
     self::$err->write('Usage: $ xpcli ', Commands::nameOf($class), ' ');
     foreach ($positional as $name) {
         self::$err->write('<', $name, '> ');
     }
     foreach ($extra as $name => $optional) {
         self::$err->write($optional ? '[' : '', '--', $name, $optional ? '] ' : ' ');
     }
     self::$err->writeLine();
     // Argument details
     self::$err->writeLine('Arguments:');
     foreach ($details as $which => $comment) {
         self::$err->writeLine('* ', $which, "\n  ", str_replace("\n", "\n  ", $comment), "\n");
     }
 }