
io.airlift.airline.GlobalUsageSummary Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of airline Show documentation
Show all versions of airline Show documentation
Java annotation-based framework for parsing Git like command line structures
The newest version!
package io.airlift.airline;
import com.google.common.base.Function;
import com.google.common.base.Objects;
import com.google.common.base.Preconditions;
import com.google.common.collect.Collections2;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.Iterables;
import io.airlift.airline.model.CommandGroupMetadata;
import io.airlift.airline.model.CommandMetadata;
import io.airlift.airline.model.GlobalMetadata;
import io.airlift.airline.model.OptionMetadata;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import static com.google.common.collect.Lists.newArrayList;
import static com.google.common.collect.Maps.newTreeMap;
import static io.airlift.airline.UsageHelper.toUsage;
public class GlobalUsageSummary
{
private final int columnSize;
public GlobalUsageSummary()
{
this(79);
}
public GlobalUsageSummary(int columnSize)
{
Preconditions.checkArgument(columnSize > 0, "columnSize must be greater than 0");
this.columnSize = columnSize;
}
/**
* Display the help on System.out.
*/
public void usage(GlobalMetadata global)
{
StringBuilder stringBuilder = new StringBuilder();
usage(global, stringBuilder);
System.out.println(stringBuilder.toString());
}
/**
* Store the help in the passed string builder.
*/
public void usage(GlobalMetadata global, StringBuilder out)
{
usage(global, new UsagePrinter(out, columnSize));
}
public void usage(GlobalMetadata global, UsagePrinter out)
{
//
// Usage
//
// build arguments
List commandArguments = newArrayList();
commandArguments.addAll(Collections2.transform(global.getOptions(), new Function()
{
public String apply(OptionMetadata option)
{
if (option.isHidden()) {
return null;
}
return toUsage(option);
}
}));
out.newPrinterWithHangingIndent(8)
.append("usage:")
.append(global.getName())
.appendWords(commandArguments)
.append(" []")
.newline()
.newline();
//
// Common commands
//
Map commands = newTreeMap();
for (CommandMetadata commandMetadata : global.getDefaultGroupCommands()) {
if (!commandMetadata.isHidden()) {
commands.put(commandMetadata.getName(), commandMetadata.getDescription());
}
}
for (CommandGroupMetadata commandGroupMetadata : global.getCommandGroups()) {
commands.put(commandGroupMetadata.getName(), commandGroupMetadata.getDescription());
}
out.append("The most commonly used ").append(global.getName()).append(" commands are:").newline();
out.newIndentedPrinter(4).appendTable(Iterables.transform(commands.entrySet(), new Function, Iterable>()
{
public Iterable apply(Entry entry)
{
return ImmutableList.of(entry.getKey(), Objects.firstNonNull(entry.getValue(), ""));
}
}));
out.newline();
out.append("See").append("'" + global.getName()).append("help ' for more information on a specific command.").newline();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy