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 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 @@ -16,6 +16,11 @@
package io.helidon.build.cli.harness;

import java.io.File;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
Expand All @@ -27,6 +32,7 @@
import java.util.Objects;
import java.util.Optional;
import java.util.Properties;
import java.util.stream.Collectors;

import io.helidon.build.cli.harness.CommandModel.ArgumentInfo;
import io.helidon.build.cli.harness.CommandModel.FlagInfo;
Expand Down Expand Up @@ -75,7 +81,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 +119,31 @@ 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) {
try {
return Files.lines(Path.of(argsFile))
.filter(line -> !line.startsWith("#"))
.flatMap(line -> Arrays.stream(line.split("\\s+")))
.collect(Collectors.toList());
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

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
Expand Up @@ -15,12 +15,20 @@
*/
package io.helidon.build.cli.harness;

import java.io.UncheckedIOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.Paths;
import java.util.Map;
import java.util.Properties;

import io.helidon.build.cli.harness.CommandModel.KeyValueInfo;
import io.helidon.build.cli.harness.CommandModel.FlagInfo;
import io.helidon.build.cli.harness.CommandParser.CommandParserException;

import org.junit.jupiter.api.Test;

import static org.hamcrest.CoreMatchers.containsString;
import static org.hamcrest.CoreMatchers.instanceOf;
import static org.hamcrest.CoreMatchers.is;
import static org.hamcrest.MatcherAssert.assertThat;
Expand Down Expand Up @@ -271,4 +279,30 @@ public void testDuplicateOptions() {
assertThat(resolver.resolve(verboseFlag1), is(true));
assertThat(resolver.resolve(verboseFlag2), is(true));
}

@Test
public void testArgsFileOptionWithExistingFile() throws URISyntaxException {
KeyValueInfo<String> param = new KeyValueInfo<>(String.class, "flavor", "flavor", null, false);
CommandParameters cmd = new CommandParameters(param);

URI argsFilePath = getClass().getResource("args.txt").toURI();
CommandParser parser = CommandParser.create("command", "--args-file", Paths.get(argsFilePath).toString());
CommandParser.Resolver resolver = parser.parseCommand(cmd);

Map<String, CommandParser.Parameter> params = resolver.params();
assertThat(params.containsKey("help"), is(true));
assertThat(((CommandParser.KeyValueParam) params.get("flavor")).value(), is("se"));

Properties properties = resolver.properties();
assertThat(properties.get("foo"), is("bar"));
}

@Test
public void testArgsFileOptionWithIncorrectFile() {
UncheckedIOException e = assertThrows(
UncheckedIOException.class,
() -> CommandParser.create("command", "--args-file", "not_existing_file.txt")
);
assertThat(e.getMessage(), containsString("java.nio.file.NoSuchFileException: not_existing_file.txt"));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# some comment
--flavor se
--help
-Dfoo=bar