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

Support multi-word and multi-line string options #172

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
20 changes: 18 additions & 2 deletions args4j/src/org/kohsuke/args4j/spi/StringOptionHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,24 @@ public StringOptionHandler(CmdLineParser parser, OptionDef option, Setter<? supe

@Override
public int parseArguments(Parameters params) throws CmdLineException {
setter.addValue(params.getParameter(0));
return 1;
int consumed = 0;
String param = params.getParameter(consumed);

if(param.startsWith("\"") || param.startsWith("'")) {
String end = String.valueOf(param.charAt(0));
StringBuilder builder = new StringBuilder();
param = param.substring(1); // remove the quote-starting character
while(!param.endsWith(end)) {
builder.append(param); builder.append(" ");
consumed++;
param = params.getParameter(consumed);
}
builder.append(param, 0, param.length()-1); // append without the quote-ending character
setter.addValue(builder.toString());
} else {
setter.addValue(param);
}
return consumed + 1;
}

@Override
Expand Down
24 changes: 23 additions & 1 deletion args4j/test/org/kohsuke/args4j/SimpleStringTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,28 @@ public void testSettingString() {
}
}

public void testSettingLongString() {
SimpleString bo = testObject;
args = new String[]{"-str", "\"longer", "string", "inside", "quotes\""};
try {
parser.parseArgument(args);
assertEquals( "longer string inside quotes", bo.str);
} catch (CmdLineException e) {
fail("Setting a string inside quotes should be possible");
}
}

public void testSettingMultiLineString() {
SimpleString bo = testObject;
args = new String[]{"-str", "\"multiline", "\n", "string\""};
try {
parser.parseArgument(args);
assertEquals( "multiline \n string", bo.str);
} catch (CmdLineException e) {
fail("Setting a string inside quotes should be possible");
}
}

public void testSettingUsage() {
args = new String[]{"-wrong-usage"};
try {
Expand Down Expand Up @@ -60,7 +82,7 @@ public void testMissingParameter() {
assertEquals("Got wrong usage message", expectedUsage, usageLines[0]);
}
}

/*
* Bug 5: Option without "usage" are hidden.
* TODO: it seems that this is intended:
Expand Down