Search in sources :

Example 1 with Subparser

use of net.sourceforge.argparse4j.inf.Subparser 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 2 with Subparser

use of net.sourceforge.argparse4j.inf.Subparser 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 3 with Subparser

use of net.sourceforge.argparse4j.inf.Subparser in project dropwizard by dropwizard.

the class InheritedServerCommandTest method usesDefaultConfigPath.

@Test
void usesDefaultConfigPath() throws Exception {
    class SingletonConfigurationFactory implements ConfigurationFactory<Configuration> {

        @Override
        public Configuration build(final ConfigurationSourceProvider provider, final String path) {
            return configuration;
        }

        @Override
        public Configuration build() {
            throw new AssertionError("Didn't use the default config path variable");
        }
    }
    when(configuration.getLoggingFactory()).thenReturn(mock(LoggingFactory.class));
    final Bootstrap<Configuration> bootstrap = new Bootstrap<>(application);
    bootstrap.setConfigurationFactoryFactory((klass, validator, objectMapper, propertyPrefix) -> new SingletonConfigurationFactory());
    bootstrap.addCommand(new ConfiguredCommand<Configuration>("test", "a test command") {

        @Override
        protected void run(final Bootstrap<Configuration> bootstrap, final Namespace namespace, final Configuration configuration) {
            assertThat(namespace.getString("file")).isNotEmpty().isEqualTo("yaml/server.yml");
        }

        @Override
        protected Argument addFileArgument(final Subparser subparser) {
            return super.addFileArgument(subparser).setDefault("yaml/server.yml");
        }
    });
    final JarLocation location = mock(JarLocation.class);
    when(location.toString()).thenReturn("dw-thing.jar");
    when(location.getVersion()).thenReturn(Optional.of("1.0.0"));
    Cli cli = new Cli(location, bootstrap, System.out, System.err);
    cli.run("test");
}
Also used : LoggingFactory(io.dropwizard.logging.LoggingFactory) JarLocation(io.dropwizard.util.JarLocation) Configuration(io.dropwizard.Configuration) Argument(net.sourceforge.argparse4j.inf.Argument) Namespace(net.sourceforge.argparse4j.inf.Namespace) ConfigurationSourceProvider(io.dropwizard.configuration.ConfigurationSourceProvider) ConfigurationFactory(io.dropwizard.configuration.ConfigurationFactory) Subparser(net.sourceforge.argparse4j.inf.Subparser) Bootstrap(io.dropwizard.setup.Bootstrap) Test(org.junit.jupiter.api.Test)

Example 4 with Subparser

use of net.sourceforge.argparse4j.inf.Subparser in project dropwizard by dropwizard.

the class Cli method addCommand.

private void addCommand(Command command) {
    commands.put(command.getName(), command);
    parser.addSubparsers().help("available commands");
    final Subparser subparser = parser.addSubparsers().addParser(command.getName(), false);
    command.configure(subparser);
    addHelp(subparser);
    subparser.description(command.getDescription()).setDefault(COMMAND_NAME_ATTR, command.getName()).defaultHelp(true);
}
Also used : Subparser(net.sourceforge.argparse4j.inf.Subparser)

Example 5 with Subparser

use of net.sourceforge.argparse4j.inf.Subparser in project dropwizard by dropwizard.

the class DbCommand method configure.

@Override
public void configure(Subparser subparser) {
    for (AbstractLiquibaseCommand<T> subcommand : subcommands.values()) {
        final Subparser cmdParser = subparser.addSubparsers().addParser(subcommand.getName()).setDefault(COMMAND_NAME_ATTR, subcommand.getName()).description(subcommand.getDescription());
        subcommand.configure(cmdParser);
    }
}
Also used : Subparser(net.sourceforge.argparse4j.inf.Subparser)

Aggregations

Subparser (net.sourceforge.argparse4j.inf.Subparser)21 ArgumentParser (net.sourceforge.argparse4j.inf.ArgumentParser)10 Before (org.junit.Before)7 Namespace (net.sourceforge.argparse4j.inf.Namespace)5 ImmutableMap (com.google.common.collect.ImmutableMap)3 OptionMetadata (io.airlift.airline.model.OptionMetadata)3 List (java.util.List)3 Argument (net.sourceforge.argparse4j.inf.Argument)3 Configuration (io.dropwizard.Configuration)2 Bootstrap (io.dropwizard.setup.Bootstrap)2 ZoneOffset (java.time.ZoneOffset)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 UriBuilder (javax.ws.rs.core.UriBuilder)2 Subparsers (net.sourceforge.argparse4j.inf.Subparsers)2 StringFormatter.dateString (org.apache.kafka.trogdor.common.StringFormatter.dateString)2 StringFormatter.durationString (org.apache.kafka.trogdor.common.StringFormatter.durationString)2 UptimeResponse (org.apache.kafka.trogdor.rest.UptimeResponse)2 TaskSpec (org.apache.kafka.trogdor.task.TaskSpec)2