2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
19 package org.apache.karaf.tooling.commands;
21 import org.apache.karaf.shell.api.action.Action;
22 import org.apache.karaf.shell.api.action.Argument;
23 import org.apache.karaf.shell.api.action.Command;
24 import org.apache.karaf.shell.api.action.Option;
25 import org.apache.karaf.shell.impl.action.command.HelpOption;
27 import java.io.PrintStream;
28 import java.lang.reflect.Field;
32 * Prints documentation in asciidoc syntax
34 public class AsciiDoctorCommandHelpPrinter extends AbstractCommandHelpPrinter {
37 public void printHelp(Action action, PrintStream out, boolean includeHelpOption) {
38 Command command = action.getClass().getAnnotation(Command.class);
39 Set<Option> options = new HashSet<>();
40 List<Argument> arguments = new ArrayList<Argument>();
41 Map<Argument, Field> argFields = new HashMap<>();
42 Map<Option, Field> optFields = new HashMap<>();
43 for (Class<?> type = action.getClass(); type != null; type = type.getSuperclass()) {
44 for (Field field : type.getDeclaredFields()) {
45 Option option = field.getAnnotation(Option.class);
50 Argument argument = field.getAnnotation(Argument.class);
51 if (argument != null) {
52 argument = replaceDefaultArgument(field, argument);
53 argFields.put(argument, field);
54 int index = argument.index();
55 while (arguments.size() <= index) {
58 if (arguments.get(index) != null) {
59 throw new IllegalArgumentException("Duplicate argument index: " + index + " on Action " + action.getClass().getName());
61 arguments.set(index, argument);
65 if (includeHelpOption)
66 options.add(HelpOption.HELP);
68 out.println("= " + command.scope() + ":" + command.name());
71 out.println("== Description");
73 out.println(command.description());
76 StringBuffer syntax = new StringBuffer();
77 syntax.append(String.format("%s:%s", command.scope(), command.name()));
78 if (options.size() > 0) {
79 syntax.append(" [options]");
81 if (arguments.size() > 0) {
83 for (Argument argument : arguments) {
84 syntax.append(String.format(argument.required() ? "%s " : "[%s] ", argument.name()));
87 out.println("== Syntax");
89 out.println(syntax.toString());
92 if (arguments.size() > 0) {
93 out.println("== Arguments");
96 out.println("|Name |Description");
97 for (Argument argument : arguments) {
98 String description = argument.description();
99 if (!argument.required()) {
100 Object o = getDefaultValue(action, argFields.get(argument));
101 String defaultValue = getDefaultValueString(o);
102 if (defaultValue != null) {
103 description += " (defaults to " + o.toString() + ")";
107 out.println("| " + argument.name());
108 out.println("| " + description);
113 if (options.size() > 0) {
114 out.println("== Options");
117 out.println("|Name |Description");
118 for (Option option : options) {
119 String opt = option.name();
120 String desc = option.description();
121 for (String alias : option.aliases()) {
124 Object o = getDefaultValue(action, optFields.get(option));
125 String defaultValue = getDefaultValueString(o);
126 if (defaultValue != null) {
127 desc += " (defaults to " + defaultValue + ")";
130 out.println("|" + opt);
131 out.println("|" + desc);
136 if (command.detailedDescription().length() > 0) {
137 out.println("== Details");
139 out.println(command.detailedDescription());
145 public void printOverview(Map<String, Set<String>> commands, PrintStream writer) {
146 writer.println("= Commands");
148 for (String key : commands.keySet()) {
149 writer.println("== " + key);
151 for (String cmd : commands.get(key)) {
152 writer.println("* [" + key + ":" + cmd + "|" + key + "-" + cmd + "]");