diff --git a/notice.md b/notice.md index 87ca1a252..445e8c67a 100644 --- a/notice.md +++ b/notice.md @@ -1,6 +1,7 @@ Copyright Notices ================= +Copyright 2013 Michael Grove Copyright 2011 Dain Sundstrom Copyright 2010 Cedric Beust diff --git a/src/main/java/io/airlift/command/Command.java b/src/main/java/io/airlift/command/Command.java index da0734f86..ef8dffdf3 100644 --- a/src/main/java/io/airlift/command/Command.java +++ b/src/main/java/io/airlift/command/Command.java @@ -47,4 +47,8 @@ * If true, this command won't appear in the usage(). */ boolean hidden() default false; + + String[] examples() default {}; + + String discussion() default ""; } diff --git a/src/main/java/io/airlift/command/CommandUsage.java b/src/main/java/io/airlift/command/CommandUsage.java index ffeb9617d..27ada80f2 100644 --- a/src/main/java/io/airlift/command/CommandUsage.java +++ b/src/main/java/io/airlift/command/CommandUsage.java @@ -1,6 +1,8 @@ package io.airlift.command; import com.google.common.base.Preconditions; +import com.google.common.base.Splitter; +import com.google.common.collect.Iterables; import io.airlift.command.model.ArgumentsMetadata; import io.airlift.command.model.CommandMetadata; import io.airlift.command.model.OptionMetadata; @@ -9,6 +11,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; +import java.util.Iterator; import java.util.List; import static com.google.common.collect.Lists.newArrayList; @@ -142,6 +145,39 @@ public void usage(@Nullable String programName, @Nullable String groupName, Comm } } + if (command.getDiscussion() != null) { + out.append("DISCUSSION").newline(); + UsagePrinter disc = out.newIndentedPrinter(8); + + disc.append(command.getDiscussion()) + .newline() + .newline(); + } + + if (command.getExamples() != null && !command.getExamples().isEmpty()) { + out.append("EXAMPLES").newline(); + UsagePrinter ex = out.newIndentedPrinter(8); + +// ex.append(command.getExamples()).newline().newline(); + + ex.appendTable(Iterables.partition(command.getExamples(), 1)); + +// for (String aEx : command.getExamples()) { +// if (aEx.isEmpty()) { +// ex.newline(); +// continue; +// } +// +// final Iterator aStrings = Splitter.on("\n").split(aEx).iterator(); +// if (aStrings.hasNext()) { +// UsagePrinter ex2 = ex.newIndentedPrinter(4); +// ex2.append("* " + aStrings.next()).newline(); +// while (aStrings.hasNext()) { +// ex2.append(aStrings.next()).newline(); +// } +// } +// } + } } private List sortOptions(List options) diff --git a/src/main/java/io/airlift/command/UsagePrinter.java b/src/main/java/io/airlift/command/UsagePrinter.java index a31eb93ec..b30e9bce3 100644 --- a/src/main/java/io/airlift/command/UsagePrinter.java +++ b/src/main/java/io/airlift/command/UsagePrinter.java @@ -80,7 +80,7 @@ public UsagePrinter appendTable(Iterable> table) line.append(" "); column++; } - out.append(spaces(indent)).append(line.toString().trim()).append("\n"); + out.append(spaces(indent)).append(line.toString()).append("\n"); } return this; diff --git a/src/main/java/io/airlift/command/model/CommandMetadata.java b/src/main/java/io/airlift/command/model/CommandMetadata.java index e9f4f647b..a174bb0f4 100644 --- a/src/main/java/io/airlift/command/model/CommandMetadata.java +++ b/src/main/java/io/airlift/command/model/CommandMetadata.java @@ -19,14 +19,19 @@ public class CommandMetadata private final List metadataInjections; private final Class type; + private final List examples; + private final String discussion; + public CommandMetadata(String name, - String description, - boolean hidden, Iterable globalOptions, - Iterable groupOptions, - Iterable commandOptions, - ArgumentsMetadata arguments, - Iterable metadataInjections, - Class type) + String description, + final String discussion, + final List examples, + boolean hidden, Iterable globalOptions, + Iterable groupOptions, + Iterable commandOptions, + ArgumentsMetadata arguments, + Iterable metadataInjections, + Class type) { this.name = name; this.description = description; @@ -37,6 +42,8 @@ public CommandMetadata(String name, this.arguments = arguments; this.metadataInjections = ImmutableList.copyOf(metadataInjections); this.type = type; + this.discussion = discussion; + this.examples = examples; } public String getName() @@ -60,6 +67,14 @@ public List getAllOptions() } + public List getExamples() { + return examples; + } + + public String getDiscussion() { + return discussion; + } + public List getGlobalOptions() { return globalOptions; @@ -97,6 +112,8 @@ public String toString() sb.append("CommandMetadata"); sb.append("{name='").append(name).append('\''); sb.append(", description='").append(description).append('\''); + sb.append(", discussion='").append(discussion).append('\''); + sb.append(", examples='").append(examples).append('\''); sb.append(", globalOptions=").append(globalOptions); sb.append(", groupOptions=").append(groupOptions); sb.append(", commandOptions=").append(commandOptions); diff --git a/src/main/java/io/airlift/command/model/MetadataLoader.java b/src/main/java/io/airlift/command/model/MetadataLoader.java index bf3faeaa6..541110437 100644 --- a/src/main/java/io/airlift/command/model/MetadataLoader.java +++ b/src/main/java/io/airlift/command/model/MetadataLoader.java @@ -6,6 +6,7 @@ import com.google.common.collect.ImmutableList; import com.google.common.collect.Iterables; import com.google.common.collect.ListMultimap; +import com.google.common.collect.Lists; import io.airlift.command.Accessor; import io.airlift.command.Arguments; import io.airlift.command.Command; @@ -92,6 +93,8 @@ public static CommandMetadata loadCommand(Class commandType) CommandMetadata commandMetadata = new CommandMetadata( name, description, + command.discussion().isEmpty() ? null : command.discussion(), + command.examples().length == 0 ? null : Lists.newArrayList(command.examples()), hidden, injectionMetadata.globalOptions, injectionMetadata.groupOptions, injectionMetadata.commandOptions,