Skip to content

Commit

Permalink
hidden commands
Browse files Browse the repository at this point in the history
  • Loading branch information
gschueler committed Nov 24, 2016
1 parent 1427f25 commit d74e019
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,11 @@ class Toolbelt {
Map<String, Class> arguments = [:]
CommandContext commandContext

@Override
boolean isHidden() {
return false
}

@Override
boolean isDefault() {
return false
Expand Down
5 changes: 5 additions & 0 deletions toolbelt/src/main/java/com/simplifyops/toolbelt/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@
*/
boolean isSolo() default false;

/**
* @return true if this method should not be printed in listings
*/
boolean isHidden() default false;

/**
* @return help text description
*/
Expand Down
15 changes: 15 additions & 0 deletions toolbelt/src/main/java/com/simplifyops/toolbelt/Hidden.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package com.simplifyops.toolbelt;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/**
* Mark a type as hidden from listings
*/

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface Hidden {
}
76 changes: 51 additions & 25 deletions toolbelt/src/main/java/com/simplifyops/toolbelt/ToolBelt.java
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,7 @@ private static class CommandSet implements Tool, CommandInvoker {
Tool other;
private boolean showBanner;
Supplier<String> banner;
public boolean hidden;

CommandSet(String name) {
this.name = name;
Expand All @@ -281,6 +282,11 @@ private static class CommandSet implements Tool, CommandInvoker {
context = new CommandContext();
}

@Override
public boolean isHidden() {
return hidden;
}

@Override
public boolean isSolo() {
return false;
Expand Down Expand Up @@ -355,7 +361,10 @@ public boolean run(final String[] args)

@Override
public Set<String> listCommands() {
TreeSet<String> strings = new TreeSet<>(commands.keySet());
TreeSet<String> strings = new TreeSet<>(commands.keySet()
.stream()
.filter(name -> !commands.get(name).isHidden())
.collect(Collectors.toList()));
if (null != other) {
strings.addAll(other.listCommands());
}
Expand Down Expand Up @@ -411,30 +420,32 @@ public void getHelp(boolean banner) {
commands.keySet()
.stream()
.sorted()
.forEach(name -> context.getOutput()
.output(
ANSIColorOutput.colorize(
ANSIColorOutput.Color.YELLOW,
String.format(
" %s",
name
),
String.format(
"%s - %s",
pad(
" ",
max -
name.length()
),
shortDescription(
commands.get(
name)
.getDescription()
)

)
)
)
.filter(name -> !commands.get(name).isHidden())
.forEach(name -> {
context.getOutput()
.output(
ANSIColorOutput.colorize(
ANSIColorOutput.Color.YELLOW,
String.format(
" %s",
name
),
String.format(
"%s - %s",
pad(
" ",
max -
name.length()
),
shortDescription(
commands.get(name)
.getDescription()
)

)
)
);
}
);

context.getOutput().output("");
Expand Down Expand Up @@ -579,6 +590,11 @@ private void introspect(CommandSet parent, final Object instance) {
isSub = true;
}
}
boolean isHidden = false;
Hidden annotation2 = aClass.getAnnotation(Hidden.class);
if (null != annotation2) {
isHidden = true;
}
Method[] methods = aClass.getMethods();
String defInvoke = null;
for (Method method : methods) {
Expand All @@ -591,6 +607,7 @@ private void introspect(CommandSet parent, final Object instance) {
MethodInvoker value = new MethodInvoker(name, method, instance, commands.context);
value.description = annotation.description();
value.solo = annotation.isSolo();
value.hidden = annotation.isHidden();
Set<String> annotationSynonyms = new HashSet<>();
if (annotation.synonyms().length > 0) {
annotationSynonyms.addAll(Arrays.asList(annotation.synonyms()));
Expand All @@ -611,6 +628,7 @@ private void introspect(CommandSet parent, final Object instance) {
}

CommandSet commandSet = new CommandSet(cmd);
commandSet.hidden = isHidden;
commandSet.context = commands.context;
commandSet.helpCommands = helpCommands;
commandSet.description = cmdDescription;
Expand Down Expand Up @@ -701,6 +719,8 @@ public static interface CommandInvoker {

boolean isDefault();

boolean isHidden();

Set<String> getSynonyms();

boolean run(String[] args) throws CommandRunFailure, InputError;
Expand All @@ -715,6 +735,7 @@ private static class MethodInvoker implements CommandInvoker {
Object instance;
private String description;
private boolean solo;
private boolean hidden;
private boolean isdefault;
CommandContext context;

Expand Down Expand Up @@ -836,6 +857,11 @@ public boolean isSolo() {
public boolean isDefault() {
return isdefault;
}

@Override
public boolean isHidden() {
return hidden;
}
}

private static String getParameterName(final Parameter param) {
Expand Down

0 comments on commit d74e019

Please sign in to comment.