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(""));
}
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(""));
}
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;
}
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"));
}
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"));
}
Aggregations