/** * Shows usage * * @param lang.XPClass $class * @return void */ protected function commandUsage(XPClass $class) { $comment = $class->getComment(); if ('' === (string) $comment) { $markdown = '# ' . $class->getSimpleName() . "\n\n"; $text = ''; } else { @(list($headline, $text) = explode("\n", $comment, 2)); $markdown = '# ' . ltrim($headline, ' #') . "\n\n"; } $markdown .= "- Usage\n ```sh\n\$ xp cmd " . Commands::nameOf($class); $extra = $details = $positional = []; foreach ($class->getMethods() as $method) { if (!$method->hasAnnotation('arg')) { continue; } $arg = $method->getAnnotation('arg'); $name = strtolower(preg_replace('/^set/', '', $method->getName())); $optional = 0 === $method->numParameters() || $method->getParameters()[0]->isOptional(); $comment = $method->getComment(); if (isset($arg['position'])) { $details[$name] = [$comment, null]; $positional[$arg['position']] = $name; } else { if (isset($arg['name'])) { $details['--' . $arg['name']] = [$comment, isset($arg['short']) ? $arg['short'] : $arg['name'][0]]; $extra[$arg['name']] = $optional; } else { $details['--' . $name] = [$comment, isset($arg['short']) ? $arg['short'] : $name[0]]; $extra[$name] = $optional; } } } // Usage asort($positional); foreach ($positional as $name) { $markdown .= ' ' . $name; } foreach ($extra as $name => $optional) { $markdown .= ' ' . (($optional ? '[' : '') . '--' . $name . ($optional ? '] ' : ' ')); } $markdown .= "\n ```\n"; // Argument details foreach ($details as $which => $detail) { $markdown .= sprintf(" **%s**: %s%s\n\n", $which, str_replace("\n", "\n ", $detail[0]), $detail[1] ? ' *(also: -' . $detail[1] . ')*' : ''); } Help::render(self::$err, substr($markdown, 0, -1) . $text, $class->getClassLoader()); }