Skip to content

Commit

Permalink
Fix #379
Browse files Browse the repository at this point in the history
  • Loading branch information
XxxXxxXxx233 authored and mkarg committed Dec 17, 2023
1 parent c266433 commit d273b60
Show file tree
Hide file tree
Showing 7 changed files with 70 additions and 1 deletion.
15 changes: 14 additions & 1 deletion src/main/java/com/beust/jcommander/DefaultUsageFormatter.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,20 @@ public void appendAllParametersDetails(StringBuilder out, int indentCount, Strin
if (hasDescription) {
wrapDescription(out, indentCount, s(indentCount) + description);
}
Object def = pd.getDefaultValueDescription();

String category = pd.getCategory();
if (!category.equals("")) {
String categoryType = "Category: " + category;

if (hasDescription) {
out.append(newLineAndIndent(indentCount));
} else {
out.append(s(indentCount));
}
out.append(categoryType);
}

Object def = pd.getDefaultDescription();

if (pd.isDynamicParameter()) {
String syntax = "Syntax: " + parameter.names()[0] + "key" + parameter.getAssignment() + "value";
Expand Down
7 changes: 7 additions & 0 deletions src/main/java/com/beust/jcommander/DynamicParameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,11 @@
* @return
*/
int order() default -1;

/**
* If specified, the category name will be used to order the description of this parameter when usage() is invoked before the number order() is used.
* @return (default or specified) category name
*/
String category() default "";

}
3 changes: 3 additions & 0 deletions src/main/java/com/beust/jcommander/JCommander.java
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,9 @@ private static class Options {
public int compare(ParameterDescription p0, ParameterDescription p1) {
WrappedParameter a0 = p0.getParameter();
WrappedParameter a1 = p1.getParameter();
if (a0 != null && a1 != null && !a0.category().equals(a1.category())) {
return a0.category().compareTo(a1.category());
}
if (a0 != null && a0.order() != -1 && a1 != null && a1.order() != -1) {
return Integer.compare(a0.order(), a1.order());
} else if (a0 != null && a0.order() != -1) {
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/com/beust/jcommander/Parameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -137,5 +137,11 @@
* @return
*/
int order() default -1;

/**
* If specified, the category name will be used to order the description of this parameter when usage() is invoked before the number order() is used.
* @return (default or specified) category name
*/
String category() default "";

}
4 changes: 4 additions & 0 deletions src/main/java/com/beust/jcommander/ParameterDescription.java
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,10 @@ public String getNames() {
return sb.toString();
}

public String getCategory() {
return wrappedParameter.category();
}

public WrappedParameter getParameter() {
return wrappedParameter;
}
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/com/beust/jcommander/WrappedParameter.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ public int order() {
return parameter != null ? parameter.order() : dynamicParameter.order();
}

public String category() {
return parameter != null ? parameter.category() : dynamicParameter.category();
}

public Class<? extends IParameterValidator>[] validateWith() {
return parameter != null ? parameter.validateWith() : dynamicParameter.validateWith();
}
Expand Down
32 changes: 32 additions & 0 deletions src/test/java/com/beust/jcommander/ParameterOrderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,38 @@ public void testOrder3() {
testOrder(new ManualOrder3(), "--arg_a","--arg_b","--arg_c", "--arg_d");
}

private static class ManualCategoryOrder1 {
@Parameter(names = "--arg_a", category = "Category 1")
public boolean isA;
@Parameter(names = "--arg_b", category = "Category 2")
public boolean isB;
@Parameter(names = "--arg_c", category = "Category 1")
public boolean isC;
@Parameter(names = "--arg_d", category = "Category 2")
public boolean isD;
}

@Test
public void testCategoryOrder1() {
testOrder(new ManualCategoryOrder1(), "--arg_a","--arg_c","--arg_b", "--arg_d");
}

private static class ManualCategoryOrder2 {
@Parameter(names = "--arg_a", category = "Category 1", order = 2)
public boolean isA;
@Parameter(names = "--arg_b", category = "Category 2", order = 2)
public boolean isB;
@Parameter(names = "--arg_c", category = "Category 1", order = 1)
public boolean isC;
@Parameter(names = "--arg_d", category = "Category 2", order = 1)
public boolean isD;
}

@Test
public void testCategoryOrder2() {
testOrder(new ManualCategoryOrder2(), "--arg_c","--arg_a","--arg_d", "--arg_b");
}

public void testOrder(Object cmd, String ... expected) {
JCommander commander = new JCommander(cmd);

Expand Down

0 comments on commit d273b60

Please sign in to comment.