Search in sources :

Example 1 with OptionMetadata

use of io.airlift.airline.model.OptionMetadata in project atlasdb by palantir.

the class AtlasDbCliCommand method getOptionTypesForCommandMetadata.

@VisibleForTesting
static Map<String, OptionType> getOptionTypesForCommandMetadata(CommandMetadata subCommand) {
    Map<String, OptionType> optionTypes = new HashMap<>();
    List<OptionMetadata> commandOptions = ImmutableList.<OptionMetadata>builder().addAll(subCommand.getCommandOptions()).addAll(subCommand.getGroupOptions()).addAll(subCommand.getGlobalOptions()).build();
    for (OptionMetadata option : commandOptions) {
        if (option.isHidden()) {
            continue;
        }
        List<String> sortedOptions = option.getOptions().stream().sorted((first, second) -> Integer.compareUnsigned(first.length(), second.length())).collect(Collectors.toList());
        String longOption = Iterables.getLast(sortedOptions);
        optionTypes.put(longOption, option.getOptionType());
    }
    return ImmutableMap.copyOf(optionTypes);
}
Also used : AtlasCli(com.palantir.atlasdb.cli.AtlasCli) Iterables(com.google.common.collect.Iterables) Bootstrap(io.dropwizard.setup.Bootstrap) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) AtlasDbConfigurationProvider(com.palantir.atlasdb.dropwizard.AtlasDbConfigurationProvider) OptionMetadata(io.airlift.airline.model.OptionMetadata) Configuration(io.dropwizard.Configuration) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) Namespace(net.sourceforge.argparse4j.inf.Namespace) Argument(net.sourceforge.argparse4j.inf.Argument) Logger(org.slf4j.Logger) ImmutableMap(com.google.common.collect.ImmutableMap) Subparser(net.sourceforge.argparse4j.inf.Subparser) Cli(io.airlift.airline.Cli) Collectors(java.util.stream.Collectors) Maps(com.google.common.collect.Maps) CommandGroupMetadata(io.airlift.airline.model.CommandGroupMetadata) List(java.util.List) Arguments(net.sourceforge.argparse4j.impl.Arguments) OptionType(io.airlift.airline.OptionType) VisibleForTesting(com.google.common.annotations.VisibleForTesting) CommandMetadata(io.airlift.airline.model.CommandMetadata) AtlasDbConfig(com.palantir.atlasdb.config.AtlasDbConfig) OptionMetadata(io.airlift.airline.model.OptionMetadata) HashMap(java.util.HashMap) OptionType(io.airlift.airline.OptionType) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with OptionMetadata

use of io.airlift.airline.model.OptionMetadata in project atlasdb by palantir.

the class AtlasDbCliCommand method addOptionToParser.

@VisibleForTesting
static void addOptionToParser(Subparser parser, OptionMetadata option) {
    if (option.isHidden()) {
        return;
    }
    List<String> sortedOptions = option.getOptions().stream().sorted((first, second) -> Integer.compareUnsigned(first.length(), second.length())).collect(Collectors.toList());
    String longOption = Iterables.getLast(sortedOptions);
    Argument arg = parser.addArgument(sortedOptions.toArray(new String[] {})).required(option.isRequired()).help(option.getDescription()).dest(longOption);
    if (option.getArity() == 0) {
        arg.action(Arguments.storeConst());
        arg.setConst(AtlasDbCommandUtils.ZERO_ARITY_ARG_CONSTANT);
    } else {
        arg.nargs(option.getArity());
    }
}
Also used : AtlasCli(com.palantir.atlasdb.cli.AtlasCli) Iterables(com.google.common.collect.Iterables) Bootstrap(io.dropwizard.setup.Bootstrap) LoggerFactory(org.slf4j.LoggerFactory) HashMap(java.util.HashMap) Callable(java.util.concurrent.Callable) AtlasDbConfigurationProvider(com.palantir.atlasdb.dropwizard.AtlasDbConfigurationProvider) OptionMetadata(io.airlift.airline.model.OptionMetadata) Configuration(io.dropwizard.Configuration) ImmutableList(com.google.common.collect.ImmutableList) Map(java.util.Map) Namespace(net.sourceforge.argparse4j.inf.Namespace) Argument(net.sourceforge.argparse4j.inf.Argument) Logger(org.slf4j.Logger) ImmutableMap(com.google.common.collect.ImmutableMap) Subparser(net.sourceforge.argparse4j.inf.Subparser) Cli(io.airlift.airline.Cli) Collectors(java.util.stream.Collectors) Maps(com.google.common.collect.Maps) CommandGroupMetadata(io.airlift.airline.model.CommandGroupMetadata) List(java.util.List) Arguments(net.sourceforge.argparse4j.impl.Arguments) OptionType(io.airlift.airline.OptionType) VisibleForTesting(com.google.common.annotations.VisibleForTesting) CommandMetadata(io.airlift.airline.model.CommandMetadata) AtlasDbConfig(com.palantir.atlasdb.config.AtlasDbConfig) Argument(net.sourceforge.argparse4j.inf.Argument) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with OptionMetadata

use of io.airlift.airline.model.OptionMetadata in project atlasdb by palantir.

the class AtlasDbCliCommand method addCommandToParser.

private void addCommandToParser(Subparser subparser, CommandMetadata subCommand, List<String> commandRoot) {
    Subparser parser = subparser.addSubparsers().addParser(subCommand.getName()).description(subCommand.getDescription()).setDefault(COMMAND_NAME_ATTR, ImmutableList.builder().addAll(commandRoot).add(subCommand.getName()).build());
    for (OptionMetadata option : subCommand.getAllOptions()) {
        addOptionToParser(parser, option);
    }
    super.configure(parser);
}
Also used : OptionMetadata(io.airlift.airline.model.OptionMetadata) Subparser(net.sourceforge.argparse4j.inf.Subparser)

Example 4 with OptionMetadata

use of io.airlift.airline.model.OptionMetadata in project atlasdb by palantir.

the class AtlasDbCliCommandTest method nargsIsSetOnArgumentWhenThereAreExpectedArguments.

@Test
public void nargsIsSetOnArgumentWhenThereAreExpectedArguments() {
    int expectedArity = 5;
    OptionMetadata metadata = new OptionMetadata(OptionType.COMMAND, ImmutableSet.of("arg"), "name", "description", expectedArity, false, false, ImmutableSet.of(), getOptionFields());
    Argument argument = getArgumentMockFromAddOptionToParser(metadata, "arg");
    verify(argument).nargs(5);
}
Also used : OptionMetadata(io.airlift.airline.model.OptionMetadata) Argument(net.sourceforge.argparse4j.inf.Argument) Test(org.junit.Test)

Example 5 with OptionMetadata

use of io.airlift.airline.model.OptionMetadata in project atlasdb by palantir.

the class AtlasDbCliCommandTest method destIsSetToLongestArg.

@Test
public void destIsSetToLongestArg() {
    String expectedDest = "long string here";
    OptionMetadata metadata = new OptionMetadata(OptionType.COMMAND, ImmutableSet.of("arg", "longer arg", expectedDest), "name", "description", 0, false, false, ImmutableSet.of(), getOptionFields());
    Argument argument = getArgumentMockFromAddOptionToParser(metadata, "arg", "longer arg", expectedDest);
    verify(argument).dest(expectedDest);
}
Also used : OptionMetadata(io.airlift.airline.model.OptionMetadata) Argument(net.sourceforge.argparse4j.inf.Argument) Test(org.junit.Test)

Aggregations

OptionMetadata (io.airlift.airline.model.OptionMetadata)9 Argument (net.sourceforge.argparse4j.inf.Argument)8 Test (org.junit.Test)6 Subparser (net.sourceforge.argparse4j.inf.Subparser)4 ImmutableMap (com.google.common.collect.ImmutableMap)3 OptionType (io.airlift.airline.OptionType)3 CommandMetadata (io.airlift.airline.model.CommandMetadata)3 Map (java.util.Map)3 Arguments (net.sourceforge.argparse4j.impl.Arguments)3 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 ImmutableList (com.google.common.collect.ImmutableList)2 Iterables (com.google.common.collect.Iterables)2 Maps (com.google.common.collect.Maps)2 AtlasCli (com.palantir.atlasdb.cli.AtlasCli)2 AtlasDbConfig (com.palantir.atlasdb.config.AtlasDbConfig)2 AtlasDbConfigurationProvider (com.palantir.atlasdb.dropwizard.AtlasDbConfigurationProvider)2 Cli (io.airlift.airline.Cli)2 CommandGroupMetadata (io.airlift.airline.model.CommandGroupMetadata)2 Configuration (io.dropwizard.Configuration)2 Bootstrap (io.dropwizard.setup.Bootstrap)2