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 usage: print options before arguments, add optional prefix #138

Open
wants to merge 2 commits 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
87 changes: 57 additions & 30 deletions args4j/src/org/kohsuke/args4j/CmdLineParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@
import java.util.List;
import java.util.ResourceBundle;
import java.util.Set;
import static org.kohsuke.args4j.Utilities.checkNonNull;
import org.kohsuke.args4j.spi.Getter;

import org.kohsuke.args4j.spi.OptionHandler;
import org.kohsuke.args4j.spi.Parameters;
import org.kohsuke.args4j.spi.Setter;

import static org.kohsuke.args4j.Utilities.checkNonNull;

/**
* Command line argument owner.
*
Expand Down Expand Up @@ -114,7 +112,7 @@ public ParserProperties getProperties() {
public void addArgument(Setter setter, Argument a) {
checkNonNull(setter, "Setter");
checkNonNull(a, "Argument");

OptionHandler h = createOptionHandler(new OptionDef(a,setter.isMultiValued()),setter);
int index = a.index();
// make sure the argument will fit in the list
Expand All @@ -138,7 +136,7 @@ public void addArgument(Setter setter, Argument a) {
public void addOption(Setter setter, Option o) {
checkNonNull(setter, "Setter");
checkNonNull(o, "Option");

checkOptionNotInMap(o.name());
for (String alias : o.aliases()) {
checkOptionNotInMap(alias);
Expand All @@ -162,7 +160,7 @@ public List<OptionHandler> getOptions() {

private void checkOptionNotInMap(String name) throws IllegalAnnotationError {
checkNonNull(name, "name");

if(findOptionByName(name)!=null) {
throw new IllegalAnnotationError(Messages.MULTIPLE_USE_OF_OPTION.format(name));
}
Expand Down Expand Up @@ -231,7 +229,7 @@ public String printExample(OptionHandlerFilter mode, ResourceBundle rb) {
StringBuilder buf = new StringBuilder();

checkNonNull(mode, "mode");

for (OptionHandler h : options) {
OptionDef option = h.option;
if(option.usage().length()==0) continue; // ignore
Expand Down Expand Up @@ -286,20 +284,20 @@ public void printUsage(Writer out, ResourceBundle rb, OptionHandlerFilter filter
PrintWriter w = new PrintWriter(out);
// determine the length of the option + metavar first
int len = 0;
for (OptionHandler h : arguments) {
for (OptionHandler h: options) {
int curLen = getPrefixLen(h, rb);
len = Math.max(len,curLen);
}
for (OptionHandler h: options) {
for (OptionHandler h : arguments) {
int curLen = getPrefixLen(h, rb);
len = Math.max(len,curLen);
}

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

Expand Down Expand Up @@ -333,7 +331,7 @@ protected void printOption(PrintWriter out, OptionHandler handler, int len, Reso
int widthUsage = totalUsageWidth - 4 - widthMetadata;

String defaultValuePart = createDefaultValuePart(handler);

// Line wrapping
// the 'left' side
List<String> namesAndMetas = wrapLines(handler.getNameAndMeta(rb, parserProperties), widthMetadata);
Expand All @@ -348,7 +346,7 @@ protected void printOption(PrintWriter out, OptionHandler handler, int len, Reso
? " %1$-" + widthMetadata + "s : %2$-1s"
: " %1$-" + widthMetadata + "s %2$-1s";
String output = String.format(format, nameAndMeta, usage);

out.println(output);
}
}
Expand Down Expand Up @@ -471,9 +469,9 @@ public void parseArgument(Collection<String> args) throws CmdLineException {
* @throws NullPointerException if {@code args} is {@code null}.
*/
public void parseArgument(final String... args) throws CmdLineException {

checkNonNull(args, "args");

String expandedArgs[] = args;
if (parserProperties.getAtSyntax()) {
expandedArgs = expandAtFiles(args);
Expand Down Expand Up @@ -534,16 +532,16 @@ public void parseArgument(final String... args) throws CmdLineException {
checkRequiredOptionsAndArguments(present);
}
}

/**
* Expands every entry prefixed with the AT sign by
* reading the file. The AT sign is used to reference
* another file that contains command line options separated
* by line breaks.
* by line breaks.
* @param args the command line arguments to be preprocessed.
* @return args with the @ sequences replaced by the text files referenced
* by the @ sequences, split around the line breaks.
* @throws CmdLineException
* @throws CmdLineException
*/
private String[] expandAtFiles(String args[]) throws CmdLineException {
List<String> result = new ArrayList<String>();
Expand All @@ -563,7 +561,7 @@ private String[] expandAtFiles(String args[]) throws CmdLineException {
}
return result.toArray(new String[result.size()]);
}

/**
* Reads all lines of a file with the platform encoding.
*/
Expand Down Expand Up @@ -603,7 +601,7 @@ private void checkRequiredOptionsAndArguments(Set<OptionHandler> present) throws
handler.option.toString(), Arrays.toString(((NamedOptionDef)handler.option).depends()));
}
}

//make sure that all forbids arguments are not present
for (OptionHandler handler : present) {
if (handler.option instanceof NamedOptionDef && !isHandlerAllowOtherOptions((NamedOptionDef) handler.option, present)) {
Expand Down Expand Up @@ -634,7 +632,7 @@ private boolean isHandlerAllowOtherOptions(NamedOptionDef option, Set<OptionHand
}
return true;
}

private OptionHandler findOptionHandler(String name) {
// Look for key/value pair first.
int pos = name.indexOf(parserProperties.getOptionValueDelimiter());
Expand Down Expand Up @@ -674,7 +672,7 @@ private OptionHandler findOptionByName(String name) {
*/
protected boolean isOption(String arg) {
checkNonNull(arg, "arg");

return parsingOptions && arg.startsWith("-");
}

Expand Down Expand Up @@ -715,7 +713,7 @@ public void setUsageWidth(int usageWidth) {

/**
* Signals the parser that parsing the options has finished.
*
*
* <p>
* Everything seen after this call is treated as an argument
* as opposed to an option.
Expand All @@ -724,6 +722,19 @@ public void stopOptionParsing() {
parsingOptions = false;
}

/**
* Prints a single-line usage to the screen.
*
* <p>
* This is a convenience method for calling {@code printUsage(new OutputStreamWriter(out),null)}
* so that you can do {@code printUsage(System.err)}.
* @param prefix Custom usage prefix e.g. {@code java -jar my-app.jar}
* @throws NullPointerException if {@code out} is {@code null}.
*/
public void printSingleLineUsage(final OutputStream out, final String prefix) {
printSingleLineUsage(new OutputStreamWriter(out), null, prefix);
}

/**
* Prints a single-line usage to the screen.
*
Expand All @@ -734,33 +745,49 @@ public void stopOptionParsing() {
*/
public void printSingleLineUsage(OutputStream out) {
checkNonNull(out, "OutputStream");

printSingleLineUsage(new OutputStreamWriter(out), null);
}

/**
* Prints a single-line usage with a custom prefix to the screen.
*
* @param w Target Writer
* @param rb
* if this is non-{@code null}, {@link Option#usage()} is treated
* as a key to obtain the actual message from this resource bundle.
* @param prefix Custom usage prefix e.g. {@code java -jar my-app.jar}
* @throws NullPointerException if {@code w} is {@code null}.
*/
public void printSingleLineUsage(final Writer w, final ResourceBundle rb, final String prefix) {
new PrintWriter(w).print(prefix);
printSingleLineUsage(w, rb);
}

/**
* Prints a single-line usage to the screen.
*
* @param w Target Writer
* @param rb
* if this is non-{@code null}, {@link Option#usage()} is treated
* as a key to obtain the actual message from this resource bundle.
* @throws NullPointerException if {@code w} is {@code null}.
*/
// TODO test this!
public void printSingleLineUsage(Writer w, ResourceBundle rb) {
public void printSingleLineUsage(final Writer w, final ResourceBundle rb) {
checkNonNull(w, "Writer");
PrintWriter pw = new PrintWriter(w);
for (OptionHandler h : arguments) {

final PrintWriter pw = new PrintWriter(w);
for (OptionHandler h : options) {
printSingleLineOption(pw, h, rb);
}
for (OptionHandler h : options) {
for (OptionHandler h : arguments) {
printSingleLineOption(pw, h, rb);
}
pw.flush();
}

private void printSingleLineOption(PrintWriter pw, OptionHandler h, ResourceBundle rb) {
private void printSingleLineOption(final PrintWriter pw, final OptionHandler h, final ResourceBundle rb) {
pw.print(' ');
if (!h.option.required())
pw.print('[');
Expand Down