Skip to content
Snippets Groups Projects
Commit a6add576 authored by dgelessus's avatar dgelessus
Browse files

Add positional parameter types for repeated/multiple-value parameters

parent b41a63e6
Branches
Tags
No related merge requests found
package de.prob2.jupyter; package de.prob2.jupyter;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Optional; import java.util.Optional;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
...@@ -49,6 +52,50 @@ public abstract class PositionalParameter<T> extends Parameter<T> { ...@@ -49,6 +52,50 @@ public abstract class PositionalParameter<T> extends Parameter<T> {
} }
} }
public static final class RequiredMultiple extends PositionalParameter<@NotNull List<@NotNull String>> {
public RequiredMultiple(final @NotNull String identifier) {
super(identifier);
}
@Override
public boolean isOptional() {
return false;
}
@Override
public @NotNull List<@NotNull String> getDefaultValue() {
throw new UnsupportedOperationException("Not an optional parameter");
}
@Override
public @NotNull Parameter.ParseResult<@NotNull List<@NotNull String>> parse(final @NotNull String argString) {
final String[] split = CommandUtils.ARG_SPLIT_PATTERN.split(argString);
return new Parameter.ParseResult<>(Arrays.asList(split), "");
}
}
public static final class OptionalMultiple extends PositionalParameter<@NotNull List<@NotNull String>> {
public OptionalMultiple(final @NotNull String identifier) {
super(identifier);
}
@Override
public boolean isOptional() {
return true;
}
@Override
public @NotNull List<@NotNull String> getDefaultValue() {
return Collections.emptyList();
}
@Override
public @NotNull Parameter.ParseResult<@NotNull List<@NotNull String>> parse(final @NotNull String argString) {
final String[] split = CommandUtils.ARG_SPLIT_PATTERN.split(argString);
return new Parameter.ParseResult<>(Arrays.asList(split), "");
}
}
public static final class RequiredRemainder extends PositionalParameter<@NotNull String> { public static final class RequiredRemainder extends PositionalParameter<@NotNull String> {
public RequiredRemainder(final @NotNull String identifier) { public RequiredRemainder(final @NotNull String identifier) {
super(identifier); super(identifier);
......
...@@ -2,7 +2,6 @@ package de.prob2.jupyter.commands; ...@@ -2,7 +2,6 @@ package de.prob2.jupyter.commands;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.Collections; import java.util.Collections;
import java.util.List;
import java.util.Map; import java.util.Map;
import com.google.inject.Inject; import com.google.inject.Inject;
...@@ -25,7 +24,7 @@ import org.jetbrains.annotations.NotNull; ...@@ -25,7 +24,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
public final class LoadCellCommand implements Command { public final class LoadCellCommand implements Command {
private static final @NotNull PositionalParameter.OptionalRemainder PREFS_PARAM = new PositionalParameter.OptionalRemainder("prefs"); private static final @NotNull PositionalParameter.OptionalMultiple PREFS_PARAM = new PositionalParameter.OptionalMultiple("prefs");
private static final @NotNull PositionalParameter.RequiredRemainder CODE_PARAM = new PositionalParameter.RequiredRemainder("code"); private static final @NotNull PositionalParameter.RequiredRemainder CODE_PARAM = new PositionalParameter.RequiredRemainder("code");
private final @NotNull ClassicalBFactory classicalBFactory; private final @NotNull ClassicalBFactory classicalBFactory;
...@@ -75,8 +74,7 @@ public final class LoadCellCommand implements Command { ...@@ -75,8 +74,7 @@ public final class LoadCellCommand implements Command {
@Override @Override
public @NotNull DisplayData run(final @NotNull ParsedArguments args) { public @NotNull DisplayData run(final @NotNull ParsedArguments args) {
final String body = args.get(CODE_PARAM); final String body = args.get(CODE_PARAM);
final List<String> prefsSplit = args.get(PREFS_PARAM).map(CommandUtils::splitArgs).orElse(Collections.emptyList()); final Map<String, String> preferences = CommandUtils.parsePreferences(args.get(PREFS_PARAM));
final Map<String, String> preferences = CommandUtils.parsePreferences(prefsSplit);
this.animationSelector.changeCurrentAnimation(new Trace(CommandUtils.withSourceCode(body, () -> this.animationSelector.changeCurrentAnimation(new Trace(CommandUtils.withSourceCode(body, () ->
this.classicalBFactory.create("(machine from Jupyter cell)", body).load(preferences) this.classicalBFactory.create("(machine from Jupyter cell)", body).load(preferences)
......
...@@ -5,7 +5,6 @@ import java.nio.file.Files; ...@@ -5,7 +5,6 @@ import java.nio.file.Files;
import java.nio.file.Path; import java.nio.file.Path;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
...@@ -40,7 +39,7 @@ public final class LoadFileCommand implements Command { ...@@ -40,7 +39,7 @@ public final class LoadFileCommand implements Command {
private static final @NotNull Logger LOGGER = LoggerFactory.getLogger(LoadFileCommand.class); private static final @NotNull Logger LOGGER = LoggerFactory.getLogger(LoadFileCommand.class);
private static final @NotNull PositionalParameter.RequiredSingle FILE_NAME_PARAM = new PositionalParameter.RequiredSingle("fileName"); private static final @NotNull PositionalParameter.RequiredSingle FILE_NAME_PARAM = new PositionalParameter.RequiredSingle("fileName");
private static final @NotNull PositionalParameter.OptionalRemainder PREFS_PARAM = new PositionalParameter.OptionalRemainder("prefs"); private static final @NotNull PositionalParameter.OptionalMultiple PREFS_PARAM = new PositionalParameter.OptionalMultiple("prefs");
private final @NotNull Injector injector; private final @NotNull Injector injector;
private final @NotNull AnimationSelector animationSelector; private final @NotNull AnimationSelector animationSelector;
...@@ -98,12 +97,7 @@ public final class LoadFileCommand implements Command { ...@@ -98,12 +97,7 @@ public final class LoadFileCommand implements Command {
if (!FactoryProvider.isExtensionKnown(extension)) { if (!FactoryProvider.isExtensionKnown(extension)) {
throw new UserErrorException("Unsupported file type: ." + extension); throw new UserErrorException("Unsupported file type: ." + extension);
} }
final Map<String, String> preferences; final Map<String, String> preferences = CommandUtils.parsePreferences(args.get(PREFS_PARAM));
if (args.get(PREFS_PARAM).isPresent()) {
preferences = CommandUtils.parsePreferences(CommandUtils.splitArgs(args.get(PREFS_PARAM).get()));
} else {
preferences = Collections.emptyMap();
}
try { try {
final ModelFactory<?> factory = this.injector.getInstance(FactoryProvider.factoryClassFromExtension(extension)); final ModelFactory<?> factory = this.injector.getInstance(FactoryProvider.factoryClassFromExtension(extension));
......
...@@ -27,7 +27,7 @@ import org.jetbrains.annotations.NotNull; ...@@ -27,7 +27,7 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.Nullable;
public final class PrefCommand implements Command { public final class PrefCommand implements Command {
private static final @NotNull PositionalParameter.OptionalRemainder PREFS_PARAM = new PositionalParameter.OptionalRemainder("prefs"); private static final @NotNull PositionalParameter.OptionalMultiple PREFS_PARAM = new PositionalParameter.OptionalMultiple("prefs");
private final @NotNull AnimationSelector animationSelector; private final @NotNull AnimationSelector animationSelector;
...@@ -67,7 +67,7 @@ public final class PrefCommand implements Command { ...@@ -67,7 +67,7 @@ public final class PrefCommand implements Command {
@Override @Override
public @NotNull DisplayData run(final @NotNull ParsedArguments args) { public @NotNull DisplayData run(final @NotNull ParsedArguments args) {
final StringBuilder sb = new StringBuilder(); final StringBuilder sb = new StringBuilder();
if (!args.get(PREFS_PARAM).isPresent()) { if (args.get(PREFS_PARAM).isEmpty()) {
final GetCurrentPreferencesCommand cmd = new GetCurrentPreferencesCommand(); final GetCurrentPreferencesCommand cmd = new GetCurrentPreferencesCommand();
this.animationSelector.getCurrentTrace().getStateSpace().execute(cmd); this.animationSelector.getCurrentTrace().getStateSpace().execute(cmd);
// TreeMap is used to sort the preferences by name. // TreeMap is used to sort the preferences by name.
...@@ -78,7 +78,7 @@ public final class PrefCommand implements Command { ...@@ -78,7 +78,7 @@ public final class PrefCommand implements Command {
sb.append('\n'); sb.append('\n');
}); });
} else { } else {
final List<String> prefsSplit = CommandUtils.splitArgs(args.get(PREFS_PARAM).get()); final List<String> prefsSplit = args.get(PREFS_PARAM);
if (prefsSplit.get(0).contains("=")) { if (prefsSplit.get(0).contains("=")) {
final List<SetPreferenceCommand> cmds = new ArrayList<>(); final List<SetPreferenceCommand> cmds = new ArrayList<>();
CommandUtils.parsePreferences(prefsSplit).forEach((pref, value) -> { CommandUtils.parsePreferences(prefsSplit).forEach((pref, value) -> {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment