Skip to content

Commit

Permalink
Better help layout and minor fixes.
Browse files Browse the repository at this point in the history
  • Loading branch information
sangupta committed Sep 1, 2016
1 parent 92517be commit 7dfc54d
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 25 deletions.
50 changes: 30 additions & 20 deletions src/main/java/com/sangupta/outline/help/OutlineHelpBuilder.java
Original file line number Diff line number Diff line change
Expand Up @@ -130,17 +130,6 @@ private void getCommandsSection(final IndentedStringWriter writer, final String
* @return
*/
private void getArgumentsSection(final IndentedStringWriter writer, final String command, final String group) {
writer.writeLine("Available arguments:");
buildArgumentsForCommand(writer, command, group);
}

/**
* Create the list of all arguments that are applicable to this particular command.
*
* @param class1
* @return
*/
private void buildArgumentsForCommand(final IndentedStringWriter writer, final String command, final String group) {
if(AssertUtils.isEmpty(command)) {
return;
}
Expand All @@ -150,6 +139,8 @@ private void buildArgumentsForCommand(final IndentedStringWriter writer, final S
return;
}

writer.writeLine("Available arguments:");

int count = 1;
for(Object obj : arguments) {
// iterate over them
Expand All @@ -160,6 +151,9 @@ private void buildArgumentsForCommand(final IndentedStringWriter writer, final S
writer.writeLine("<" + nonEmpty(arg.title(), "arg" + count++) + ">");
writer.setIndentLevel(2);
writer.writeLine(arg.description());
if(arg.required()) {
writer.writeLine("Required.");
}
}

if(obj instanceof Arguments) {
Expand Down Expand Up @@ -246,13 +240,23 @@ private void buildOptionsSectionForData(final IndentedStringWriter writer, final
}

private void getOptionHelp(IndentedStringWriter writer, Option option) {
writer.write(option.description());
if(!option.description().endsWith(".")) {
writer.write(".");
boolean descriptionAvailable = AssertUtils.isNotEmpty(option.description());
if(descriptionAvailable) {
writer.write(option.description());
if(!option.description().endsWith(".")) {
writer.write(".");
}
}

writer.newLine();
writer.newLine();
boolean hasOptionDetails = option.required() || option.arity() > 0 || AssertUtils.isNotEmpty(option.allowedValues());
if(!hasOptionDetails) {
return;
}

if(descriptionAvailable) {
writer.newLine();
writer.newLine();
}

if(option.required()) {
writer.write("Required. ");
Expand Down Expand Up @@ -303,14 +307,20 @@ private void getUsageLine(final IndentedStringWriter writer, final String comman

// group
if(AssertUtils.isNotEmpty(group)) {
Set<Option> options = new HashSet<>(this.meta.groupOptions.get(group).values());
buildOptionsSectionInUsage(writer, options);
Map<String, Option> map = this.meta.groupOptions.get(group);
if(AssertUtils.isNotEmpty(map)) {
Set<Option> options = new HashSet<>(map.values());
buildOptionsSectionInUsage(writer, options);
}
}

// command options
if(AssertUtils.isNotEmpty(command)) {
Set<Option> options = new HashSet<>(this.meta.commandOptions.get(command).values());
buildOptionsSectionInUsage(writer, options);
Map<String, Option> map = this.meta.commandOptions.get(command);
if(AssertUtils.isNotEmpty(map)) {
Set<Option> options = new HashSet<>(map.values());
buildOptionsSectionInUsage(writer, options);
}
}

// and lastly we need to display the arguments that can be passed
Expand Down
9 changes: 9 additions & 0 deletions src/test/java/com/sangupta/outline/GroupCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -86,4 +86,13 @@ public void testGroupWithCommand() {
Assert.assertEquals("radd", result.command);
Assert.assertEquals("remote", result.group);
}

public static void main(String[] args) {
Outline outline = OutlineTestSupport.getOutline();

args = "help remote remote-add".split(" ");
Object instance = outline.parse(args);

((OutlineHelp) instance).showHelpIfRequested();
}
}
10 changes: 5 additions & 5 deletions src/test/java/com/sangupta/outline/OutlineTestSupport.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,19 +144,19 @@ public static class RemoteCommand extends GlobalCommand {
@Command(group = "remote", name = "remote-add", description = "remote add global command")
public static class RemoteAddCommand extends RemoteCommand {

@Option(name = "-c1")
@Option(name = "-c1", description = "command specific option 1")
protected String c1;

@Option(name = "-c2", arity = 2)
@Option(name = "-c2", arity = 2, description = "command specific option 2")
protected String[] c2;

@Argument(order = 0)
@Argument(order = 0, description = "the first argument", required = true, title = "file")
protected String a1;

@Argument(order = 1)
@Argument(order = 1, description = "the second argument")
protected String a2;

@Arguments
@Arguments(description = "all other arguments")
protected String[] a3;

}
Expand Down

0 comments on commit 7dfc54d

Please sign in to comment.