Skip to content

Commit

Permalink
Fix: enable parameter values to start with @
Browse files Browse the repository at this point in the history
  • Loading branch information
HennyNile authored and mkarg committed Aug 11, 2024
1 parent 9cc3be8 commit 59ccac9
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 11 deletions.
38 changes: 29 additions & 9 deletions src/main/java/com/beust/jcommander/JCommander.java
Original file line number Diff line number Diff line change
Expand Up @@ -457,17 +457,11 @@ private String[] expandArgs(String[] originalArgv) {
List<String> vResult1 = Lists.newArrayList();

//
// Expand @
// Expand dynamic args
//
for (String arg : originalArgv) {

if (arg.startsWith("@") && options.expandAtSign) {
String fileName = arg.substring(1);
vResult1.addAll(readFile(fileName));
} else {
List<String> expanded = expandDynamicArg(arg);
vResult1.addAll(expanded);
}
List<String> expanded = expandDynamicArg(arg);
vResult1.addAll(expanded);
}

// Expand separators
Expand Down Expand Up @@ -735,6 +729,32 @@ private void parseValues(String[] args, boolean validate) {
boolean isDashDash = false; // once we encounter --, everything goes into the main parameter
while (i < args.length && !commandParsed) {
String arg = args[i];

//
// Expand @
//
if (arg.startsWith("@") && options.expandAtSign) {
String fileName = arg.substring(1);
List<String> fileArgs = readFile(fileName);

// Create a new array to hold the expanded arguments
String[] newArgs = new String[args.length + fileArgs.size() - 1];

// Copy the existing arguments before the '@' argument
System.arraycopy(args, 0, newArgs, 0, i);

// Copy the arguments from the file
for (int j = 0; j < fileArgs.size(); j++) {
newArgs[i + j] = fileArgs.get(j);
}

// Copy the remaining arguments after the '@' argument
System.arraycopy(args, i + 1, newArgs, i + fileArgs.size(), args.length - i - 1);

args = newArgs;
continue;
}

String a = trim(arg);
args[i] = a;
p("Parsing arg: " + a);
Expand Down
3 changes: 1 addition & 2 deletions src/test/java/com/beust/jcommander/JCommanderTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -1055,8 +1055,7 @@ class Params {
Assert.assertEquals(params.username, "@tzellman");
}

@Test(enabled = true, description = "Enable top-level @/ampersand file expansion, which should throw in this case",
expectedExceptions = ParameterException.class)
@Test
public void enabledAtSignExpansionTest() {
class Params {
@Parameter(names = {"-username"})
Expand Down

0 comments on commit 59ccac9

Please sign in to comment.