Skip to content

Commit

Permalink
adding optional discussion and example section at the end of the usag…
Browse files Browse the repository at this point in the history
…e output for a command, fix #8
  • Loading branch information
mhgrove committed Jan 23, 2013
1 parent f8bb877 commit 18b1446
Showing 6 changed files with 69 additions and 8 deletions.
1 change: 1 addition & 0 deletions notice.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
Copyright Notices
=================

Copyright 2013 Michael Grove <[email protected]>
Copyright 2011 Dain Sundstrom <[email protected]>
Copyright 2010 Cedric Beust <[email protected]>

4 changes: 4 additions & 0 deletions src/main/java/io/airlift/command/Command.java
Original file line number Diff line number Diff line change
@@ -47,4 +47,8 @@
* If true, this command won't appear in the usage().
*/
boolean hidden() default false;

String[] examples() default {};

String discussion() default "";
}
36 changes: 36 additions & 0 deletions src/main/java/io/airlift/command/CommandUsage.java
Original file line number Diff line number Diff line change
@@ -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<String> 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<OptionMetadata> sortOptions(List<OptionMetadata> options)
2 changes: 1 addition & 1 deletion src/main/java/io/airlift/command/UsagePrinter.java
Original file line number Diff line number Diff line change
@@ -80,7 +80,7 @@ public UsagePrinter appendTable(Iterable<? extends Iterable<String>> 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;
31 changes: 24 additions & 7 deletions src/main/java/io/airlift/command/model/CommandMetadata.java
Original file line number Diff line number Diff line change
@@ -19,14 +19,19 @@ public class CommandMetadata
private final List<Accessor> metadataInjections;
private final Class<?> type;

private final List<String> examples;
private final String discussion;

public CommandMetadata(String name,
String description,
boolean hidden, Iterable<OptionMetadata> globalOptions,
Iterable<OptionMetadata> groupOptions,
Iterable<OptionMetadata> commandOptions,
ArgumentsMetadata arguments,
Iterable<Accessor> metadataInjections,
Class<?> type)
String description,
final String discussion,
final List<String> examples,
boolean hidden, Iterable<OptionMetadata> globalOptions,
Iterable<OptionMetadata> groupOptions,
Iterable<OptionMetadata> commandOptions,
ArgumentsMetadata arguments,
Iterable<Accessor> 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<OptionMetadata> getAllOptions()

}

public List<String> getExamples() {
return examples;
}

public String getDiscussion() {
return discussion;
}

public List<OptionMetadata> 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);
3 changes: 3 additions & 0 deletions src/main/java/io/airlift/command/model/MetadataLoader.java
Original file line number Diff line number Diff line change
@@ -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,

0 comments on commit 18b1446

Please sign in to comment.