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

cuter usage for ExplicitBoolean #121

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@

import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;

import org.kohsuke.args4j.CmdLineException;
import org.kohsuke.args4j.CmdLineParser;
import org.kohsuke.args4j.OptionDef;
import org.kohsuke.args4j.ParserProperties;

/**
* Boolean {@link OptionHandler} that (unlike the standard {@link BooleanOptionHandler}
Expand Down Expand Up @@ -61,4 +63,26 @@ private Boolean getBoolean(String parameter) throws CmdLineException {
public String getDefaultMetaVariable() {
return Messages.DEFAULT_META_EXPLICIT_BOOLEAN_OPTION_HANDLER.format();
}

@Override
public String getNameAndMeta(ResourceBundle rb, ParserProperties properties) {
String str = option.isArgument() ? "" : option.toString();
String meta = getMetaVariable(rb);
if (meta != null) {
if (option.isArgument()) {
str += '[';
} else {
String delimiter = properties.getOptionValueDelimiter();
if (delimiter.trim().equals(delimiter)) {
// not blank, bracket before delimiter
str += '[' + delimiter;
} else {
// blank, bracket after space
str += delimiter + '[';
}
}
str += meta + ']';
}
return str;
}
}
2 changes: 1 addition & 1 deletion args4j/src/org/kohsuke/args4j/spi/Messages.properties
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ ILLEGAL_UUID = \
ILLEGAL_PATH = \
Failed to parse path "{0}"
DEFAULT_META_EXPLICIT_BOOLEAN_OPTION_HANDLER = \
VALUE
true|false
DEFAULT_META_FILE_OPTION_HANDLER = \
FILE
DEFAULT_META_INET_ADDRESS_OPTION_HANDLER = \
Expand Down
2 changes: 1 addition & 1 deletion args4j/src/org/kohsuke/args4j/spi/Messages_de.properties
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ ILLEGAL_UUID = \
ILLEGAL_PATH = \
Pfad "{0}" ist nicht g\u00fcltig
DEFAULT_META_EXPLICIT_BOOLEAN_OPTION_HANDLER = \
WERT
true|false
DEFAULT_META_FILE_OPTION_HANDLER = \
DATEI
DEFAULT_META_INET_ADDRESS_OPTION_HANDLER = \
Expand Down
2 changes: 1 addition & 1 deletion args4j/src/org/kohsuke/args4j/spi/OptionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public final String getNameAndMeta(ResourceBundle rb) {
* @param properties
* Affects the formatting behaviours.
*/
public final String getNameAndMeta(ResourceBundle rb, ParserProperties properties) {
public String getNameAndMeta(ResourceBundle rb, ParserProperties properties) {
String str = option.isArgument() ? "" : option.toString();
String meta = getMetaVariable(rb);
if (meta != null) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ public void testUsage() {
try {
parser.parseArgument(args);
} catch (CmdLineException e) {
assertUsageContains("Usage message should contain 'VAL'", "VAL");
// This tests 2 things
assertUsageContains("Usage message should contain '[true|false]'", "[true|false]");
}
}

Expand Down
13 changes: 12 additions & 1 deletion args4j/test/org/kohsuke/args4j/ExplicitBooleanOptionTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -100,11 +100,22 @@ public void testIllegalBoolean() {

public void testUsage() {
args = new String[] { "-wrong" };

try {
parser.parseArgument(args);
} catch (CmdLineException e) {
assertUsageContains("Usage message should contain 'VAL'", "VAL");
assertUsageContains("Usage message should contain '[true|false]'", "[true|false]");
}
}

public void testUsageWithEquals() {
args = new String[] { "-wrong" };
parser = new CmdLineParser(testObject, ParserProperties.defaults().withOptionValueDelimiter("="));

try {
parser.parseArgument(args);
} catch (CmdLineException e) {
assertUsageContains("Usage message should contain '[=true|false]'", "[=true|false]");
}
}
}