Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Print options sorted by name, meta and usage. #135

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 28 additions & 5 deletions args4j/src/org/kohsuke/args4j/CmdLineParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,12 @@
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.util.List;
import java.util.ResourceBundle;
import java.util.Set;

import org.kohsuke.args4j.spi.Getter;

import org.kohsuke.args4j.spi.OptionHandler;
Expand Down Expand Up @@ -282,7 +284,7 @@ public void printUsage(Writer out, ResourceBundle rb) {
* @param filter
* Controls which options to be printed.
*/
public void printUsage(Writer out, ResourceBundle rb, OptionHandlerFilter filter) {
public void printUsage(Writer out, final ResourceBundle rb, OptionHandlerFilter filter) {
PrintWriter w = new PrintWriter(out);
// determine the length of the option + metavar first
int len = 0;
Expand All @@ -295,11 +297,32 @@ public void printUsage(Writer out, ResourceBundle rb, OptionHandlerFilter filter
len = Math.max(len,curLen);
}

// ensure options/arguments are sorted before printing
List<OptionHandler> optionsAndArguments = new ArrayList<OptionHandler>(arguments.size() + options.size());
optionsAndArguments.addAll(arguments);
optionsAndArguments.addAll(options);
Collections.sort(optionsAndArguments, new Comparator<OptionHandler>() {
public int compare(OptionHandler o1, OptionHandler o2) {
// same instance
if(o1 == o2) {
return 0;
}
// sort primarily by option
int result = o1.getNameAndMeta(rb, parserProperties).compareTo(o2.getNameAndMeta(rb, parserProperties));
// then by usage
if(result == 0 && o1.option != null && o2.option != null) {
result = localize(o1.option.usage(),rb).compareTo(localize(o2.option.usage(), rb));
}
// finally by object identity
if(result == 0) {
return System.identityHashCode(o1) - System.identityHashCode(o2);
}
return result;
}
});

// then print
for (OptionHandler h : arguments) {
printOption(w, h, len, rb, filter);
}
for (OptionHandler h : options) {
for (OptionHandler h : optionsAndArguments) {
printOption(w, h, len, rb, filter);
}

Expand Down