Skip to content

Commit

Permalink
Add defaultValue to @APIOption
Browse files Browse the repository at this point in the history
  • Loading branch information
pejovica committed Apr 9, 2018
1 parent 00d8fa5 commit 87b8dec
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,13 @@

APIOptionKind kind() default APIOptionKind.Default;

/**
* The value that will be passed to a non-boolean option when no {@code =} is specified.
* <p>
* By default {@code --option} form is equivalent to {@code --option=} (it passes empty string).
*/
String[] defaultValue() default {};

/**
* APIOptionKind can be used to customize how an {@link APIOption} gets rewritten to its
* {@link org.graalvm.compiler.options.Option} counterpart.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,13 @@ class APIOptionHandler extends NativeImage.OptionHandler<NativeImage> {

static final class OptionInfo {
final String builderOption;
final String defaultValue;
final String helpText;
final boolean hasPathArguments;

OptionInfo(String builderOption, String helpText, boolean hasPathArguments) {
OptionInfo(String builderOption, String defaultValue, String helpText, boolean hasPathArguments) {
this.builderOption = builderOption;
this.defaultValue = defaultValue;
this.helpText = helpText;
this.hasPathArguments = hasPathArguments;
}
Expand All @@ -74,10 +76,14 @@ boolean consume(Queue<String> args) {
if (option != null) {
args.poll();
String builderOption = option.builderOption;
String optionValue = option.defaultValue;
if (optionParts.length == 2) {
String optionValue = optionParts[1];
optionValue = optionParts[1];
}
if (optionValue != null) {
if (option.hasPathArguments) {
optionValue = Arrays.stream(optionValue.split(","))
.filter(s -> !s.isEmpty())
.map(s -> nativeImage.canonicalize(Paths.get(s)).toString())
.collect(Collectors.joining(","));
}
Expand Down Expand Up @@ -123,17 +129,20 @@ private void extractOption(String optionPrefix, OptionDescriptor optionDescripto
String builderOption = optionPrefix;
if (optionDescriptor.getOptionValueType().equals(Boolean.class)) {
VMError.guarantee(!apiAnnotation.kind().equals(APIOptionKind.Paths));
VMError.guarantee(apiAnnotation.defaultValue().length == 0);
builderOption += apiAnnotation.kind().equals(APIOptionKind.Negated) ? "-" : "+";
builderOption += optionDescriptor.getName();
} else {
VMError.guarantee(!apiAnnotation.kind().equals(APIOptionKind.Negated));
VMError.guarantee(apiAnnotation.defaultValue().length <= 1, "SustrateVM APIOption cannot have more than one default value");
builderOption += optionDescriptor.getName();
builderOption += "=";
}
String defaultValue = apiAnnotation.defaultValue().length == 0 ? null : apiAnnotation.defaultValue()[0];
String helpText = optionDescriptor.getHelp();
VMError.guarantee(helpText != null && !helpText.isEmpty(), "SustrateVM APIOption needs to provide help text");
helpText = helpText.substring(0, 1).toLowerCase() + helpText.substring(1);
options.put("--" + apiAnnotation.name(), new APIOptionHandler.OptionInfo(builderOption, helpText, apiAnnotation.kind().equals(APIOptionKind.Paths)));
options.put("--" + apiAnnotation.name(), new APIOptionHandler.OptionInfo(builderOption, defaultValue, helpText, apiAnnotation.kind().equals(APIOptionKind.Paths)));
}
} catch (NoSuchFieldException e) {
/* Does not qualify as APIOption */
Expand Down

0 comments on commit 87b8dec

Please sign in to comment.