Skip to content

Commit

Permalink
register converters
Browse files Browse the repository at this point in the history
  • Loading branch information
AbdelStark committed Apr 12, 2019
1 parent 2d0e9bc commit aec4735
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,13 @@
*/
package tech.pegasys.pantheon.cli;

import static java.util.Arrays.stream;

import tech.pegasys.pantheon.util.StringUtils;

import java.util.Arrays;
import java.util.AbstractMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import org.apache.logging.log4j.Logger;
Expand Down Expand Up @@ -50,7 +53,7 @@ static void checkOptionDependencies(
commandLine.getCommandSpec().options().stream()
.filter(
option ->
Arrays.stream(option.names()).anyMatch(dependentOptionsNames::contains)
stream(option.names()).anyMatch(dependentOptionsNames::contains)
&& !option.stringValues().isEmpty())
.map(option -> option.names()[0])
.collect(
Expand All @@ -65,4 +68,15 @@ static void checkOptionDependencies(
}
}
}

@SafeVarargs
static void registerConverters(
final CommandLine commandLine, Map.Entry<Class, CommandLine.ITypeConverter>... converters) {
stream(converters)
.forEach(entry -> commandLine.registerConverter(entry.getKey(), entry.getValue()));
}

static <K, V> Map.Entry<K, V> entryOf(K key, V value) {
return new AbstractMap.SimpleImmutableEntry<>(key, value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
import static java.nio.charset.StandardCharsets.UTF_8;
import static java.util.Arrays.asList;
import static tech.pegasys.pantheon.cli.CommandLineUtils.checkOptionDependencies;
import static tech.pegasys.pantheon.cli.CommandLineUtils.entryOf;
import static tech.pegasys.pantheon.cli.CommandLineUtils.registerConverters;
import static tech.pegasys.pantheon.cli.DefaultCommandValues.getDefaultPantheonDataPath;
import static tech.pegasys.pantheon.cli.NetworkName.MAINNET;
import static tech.pegasys.pantheon.controller.PantheonController.DATABASE_PATH;
Expand Down Expand Up @@ -571,13 +573,15 @@ public void parse(
commandLine.addSubcommand(
RLPSubCommand.COMMAND_NAME, new RLPSubCommand(resultHandler.out(), in));

commandLine.registerConverter(Address.class, Address::fromHexStringStrict);
commandLine.registerConverter(BytesValue.class, BytesValue::fromHexString);
commandLine.registerConverter(Level.class, Level::valueOf);
commandLine.registerConverter(SyncMode.class, SyncMode::fromString);
commandLine.registerConverter(UInt256.class, (arg) -> UInt256.of(new BigInteger(arg)));
commandLine.registerConverter(Wei.class, (arg) -> Wei.of(Long.parseUnsignedLong(arg)));
commandLine.registerConverter(PositiveNumber.class, PositiveNumber::fromString);
registerConverters(
commandLine,
entryOf(Address.class, Address::fromHexStringStrict),
entryOf(BytesValue.class, BytesValue::fromHexString),
entryOf(Level.class, Level::valueOf),
entryOf(SyncMode.class, SyncMode::fromString),
entryOf(UInt256.class, (arg) -> UInt256.of(new BigInteger(arg))),
entryOf(Wei.class, (arg) -> Wei.of(Long.parseUnsignedLong(arg))),
entryOf(PositiveNumber.class, PositiveNumber::fromString));

// Add performance options
UnstableOptionsSubCommand.createUnstableOptions(
Expand Down

0 comments on commit aec4735

Please sign in to comment.