Search in sources :

Example 1 with Option

use of dmg.util.command.Option in project dcache by dCache.

the class AnnotatedCommandScannerTest method shouldEnforceRequiredOptions.

@Test(expected = CommandSyntaxException.class)
public void shouldEnforceRequiredOptions() throws Exception {
    class SUT {

        @Command(name = "test")
        class TestCommand implements Callable<String> {

            @Option(name = "foo", required = true)
            int bar;

            @Override
            public String call() {
                assertThat(bar, is(2));
                return null;
            }
        }
    }
    Map<List<String>, ? extends CommandExecutor> commands = _scanner.scan(new SUT());
    commands.get(asList("test")).execute(new Args(""));
}
Also used : Args(org.dcache.util.Args) Option(dmg.util.command.Option) List(java.util.List) Arrays.asList(java.util.Arrays.asList) Test(org.junit.Test)

Example 2 with Option

use of dmg.util.command.Option in project dcache by dCache.

the class AnnotatedCommandScannerTest method shouldAllowOptionalOptions.

@Test
public void shouldAllowOptionalOptions() throws Exception {
    class SUT {

        @Command(name = "test")
        class TestCommand implements Callable<String> {

            @Option(name = "foo")
            int bar;

            @Override
            public String call() {
                assertThat(bar, is(0));
                return null;
            }
        }
    }
    Map<List<String>, ? extends CommandExecutor> commands = _scanner.scan(new SUT());
    commands.get(asList("test")).execute(new Args(""));
}
Also used : Args(org.dcache.util.Args) Option(dmg.util.command.Option) List(java.util.List) Arrays.asList(java.util.Arrays.asList) Test(org.junit.Test)

Example 3 with Option

use of dmg.util.command.Option in project dcache by dCache.

the class AnnotatedCommandExecutor method createFieldHandlers.

private List<Handler> createFieldHandlers(Command command, Class<? extends Callable<?>> clazz) {
    Set<String> optionNames = new HashSet<>();
    boolean allowAnyOption = false;
    List<Handler> handlers = Lists.newArrayList();
    for (Class<?> c = clazz; c != null; c = c.getSuperclass()) {
        for (Field field : c.getDeclaredFields()) {
            Option option = field.getAnnotation(Option.class);
            if (option != null) {
                handlers.add(createFieldHandler(field, option));
                optionNames.add(option.name());
            }
            Argument argument = field.getAnnotation(Argument.class);
            if (argument != null) {
                handlers.add(createFieldHandler(field, argument));
            }
            CommandLine commandLine = field.getAnnotation(CommandLine.class);
            if (commandLine != null) {
                handlers.add(createFieldHandler(command, field, commandLine));
                allowAnyOption |= commandLine.allowAnyOption();
            }
        }
    }
    int maxArgs = handlers.isEmpty() ? 0 : Ordering.natural().max(transform(handlers, GET_MAX_ARGS));
    if (maxArgs < Integer.MAX_VALUE) {
        handlers.add(new MaxArgumentsHandler(maxArgs));
    }
    if (!allowAnyOption) {
        handlers.add(new CheckOptionsKnownHandler(optionNames));
    }
    return handlers;
}
Also used : Argument(dmg.util.command.Argument) Field(java.lang.reflect.Field) CommandLine(dmg.util.command.CommandLine) Option(dmg.util.command.Option) HashSet(java.util.HashSet)

Example 4 with Option

use of dmg.util.command.Option in project dcache by dCache.

the class AnnotatedCommandScannerTest method shouldOptionAnnotationForOptions.

@Test
public void shouldOptionAnnotationForOptions() throws Exception {
    class SUT {

        @Command(name = "test")
        class TestCommand implements Callable<String> {

            @Option(name = "foo")
            boolean bar;

            @Override
            public String call() {
                assertThat(bar, is(true));
                return null;
            }
        }
    }
    Map<List<String>, ? extends CommandExecutor> commands = _scanner.scan(new SUT());
    commands.get(asList("test")).execute(new Args("-foo"));
}
Also used : Args(org.dcache.util.Args) Option(dmg.util.command.Option) List(java.util.List) Arrays.asList(java.util.Arrays.asList) Test(org.junit.Test)

Example 5 with Option

use of dmg.util.command.Option in project dcache by dCache.

the class AnnotatedCommandScannerTest method shouldAllowOptionArguments.

@Test
public void shouldAllowOptionArguments() throws Exception {
    class SUT {

        @Command(name = "test")
        class TestCommand implements Callable<String> {

            @Option(name = "foo")
            int bar;

            @Override
            public String call() {
                assertThat(bar, is(2));
                return null;
            }
        }
    }
    Map<List<String>, ? extends CommandExecutor> commands = _scanner.scan(new SUT());
    commands.get(asList("test")).execute(new Args("-foo=2"));
}
Also used : Args(org.dcache.util.Args) Option(dmg.util.command.Option) List(java.util.List) Arrays.asList(java.util.Arrays.asList) Test(org.junit.Test)

Aggregations

Option (dmg.util.command.Option)5 Arrays.asList (java.util.Arrays.asList)4 List (java.util.List)4 Args (org.dcache.util.Args)4 Test (org.junit.Test)4 Argument (dmg.util.command.Argument)1 CommandLine (dmg.util.command.CommandLine)1 Field (java.lang.reflect.Field)1 HashSet (java.util.HashSet)1