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

Add support for argument file #690 #704

Merged
merged 5 commits into from
May 3, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -15,7 +15,14 @@
*/
package io.helidon.build.cli.harness;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.UncheckedIOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
Expand Down Expand Up @@ -75,7 +82,8 @@ static CommandParser create(String... args) {
Properties properties = new Properties();
Map<String, Parameter> params = new HashMap<>();
String error = null;
List<String> argsList = mapArgs(args);
String[] processedArgs = preProcessArgs(args);
List<String> argsList = mapArgs(processedArgs);
Iterator<String> it = argsList.iterator();
while (it.hasNext()) {
String rawArg = it.next();
Expand Down Expand Up @@ -112,6 +120,41 @@ static CommandParser create(String... args) {
return new CommandParser(argsList, commandName, new Resolver(params, properties), error);
}

static String[] preProcessArgs(String[] args) {
List<String> result = new ArrayList<>();
for (int i = 0; i < args.length; i++) {
if ("--args-file".equals(args[i]) && (i < args.length - 1)) {
String argsFile = args[i + 1];
result.addAll(readArgsFile(argsFile));
i++;
} else {
result.add(args[i]);
}
}
return result.toArray(new String[0]);
}

static List<String> readArgsFile(String argsFile) {
List<String> result = new ArrayList<>();
try {
URL fileURL = new URL(argsFile);
aserkes marked this conversation as resolved.
Show resolved Hide resolved
fileURL.toURI();
BufferedReader reader = new BufferedReader(new InputStreamReader(fileURL.openStream()));
String line;
while ((line = reader.readLine()) != null) {
if (!line.startsWith("#")) {
result.addAll(Arrays.asList(line.split("\\s+")));
}
}
reader.close();
} catch (IOException e) {
throw new UncheckedIOException(e);
} catch (URISyntaxException e) {
throw new IllegalArgumentException("Option argsFile is incorrect.", e);
}
return result;
}

static List<String> mapArgs(String... args) {
List<String> result = new LinkedList<>(Arrays.asList(args));
// Map version flag to version command if first argument
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# some comment
--help