From aec47350792273615aeb2daab484799be928b47a Mon Sep 17 00:00:00 2001 From: Abdelhamid Bakhta Date: Fri, 12 Apr 2019 15:10:24 +0200 Subject: [PATCH] register converters --- .../pegasys/pantheon/cli/CommandLineUtils.java | 18 ++++++++++++++++-- .../pegasys/pantheon/cli/PantheonCommand.java | 18 +++++++++++------- 2 files changed, 27 insertions(+), 9 deletions(-) diff --git a/pantheon/src/main/java/tech/pegasys/pantheon/cli/CommandLineUtils.java b/pantheon/src/main/java/tech/pegasys/pantheon/cli/CommandLineUtils.java index 92e6d0487b..fb128bd66f 100644 --- a/pantheon/src/main/java/tech/pegasys/pantheon/cli/CommandLineUtils.java +++ b/pantheon/src/main/java/tech/pegasys/pantheon/cli/CommandLineUtils.java @@ -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; @@ -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( @@ -65,4 +68,15 @@ static void checkOptionDependencies( } } } + + @SafeVarargs + static void registerConverters( + final CommandLine commandLine, Map.Entry... converters) { + stream(converters) + .forEach(entry -> commandLine.registerConverter(entry.getKey(), entry.getValue())); + } + + static Map.Entry entryOf(K key, V value) { + return new AbstractMap.SimpleImmutableEntry<>(key, value); + } } diff --git a/pantheon/src/main/java/tech/pegasys/pantheon/cli/PantheonCommand.java b/pantheon/src/main/java/tech/pegasys/pantheon/cli/PantheonCommand.java index 78c59868d0..0da3d542a4 100644 --- a/pantheon/src/main/java/tech/pegasys/pantheon/cli/PantheonCommand.java +++ b/pantheon/src/main/java/tech/pegasys/pantheon/cli/PantheonCommand.java @@ -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; @@ -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(