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

solve issue #501 #520

Merged
merged 1 commit into from
Aug 11, 2024
Merged
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
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++) {
mkarg marked this conversation as resolved.
Show resolved Hide resolved
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
Loading