use of org.dcache.util.Args in project dcache by dCache.
the class ShellApplication method execute.
/**
* Executes a single command with the output being printed to the console.
*/
public void execute(Args args) throws Throwable {
if (args.argc() == 0) {
return;
}
String out;
try {
if (isAnsiSupported && args.argc() > 0) {
if (args.argv(0).equals("help")) {
args.shift();
args = new Args("help -format=" + HelpFormat.ANSI + " " + args.toString());
}
}
try {
out = Objects.toString(commandInterpreter.command(args), null);
} catch (CommandThrowableException e) {
throw e.getCause();
}
} catch (CommandSyntaxException e) {
Ansi sb = Ansi.ansi();
sb.fg(RED).a("Syntax error: " + e.getMessage() + "\n").reset();
String help = e.getHelpText();
if (help != null) {
sb.a(help);
}
out = sb.toString();
} catch (CommandExitException e) {
throw e;
} catch (CommandPanicException e) {
Ansi sb = Ansi.ansi();
sb.fg(RED).a("Bug detected! ").reset().a("Please email the following details to <support@dcache.org>:\n");
Throwable t = e.getCause() == null ? e : e.getCause();
StringWriter sw = new StringWriter();
t.printStackTrace(new PrintWriter(sw));
out = sb.a(sw.toString()).toString();
} catch (Exception e) {
out = Ansi.ansi().fg(RED).a(e.getMessage()).reset().toString();
}
if (!isNullOrEmpty(out)) {
console.print(out);
if (out.charAt(out.length() - 1) != '\n') {
console.println();
}
}
console.flush();
}
use of org.dcache.util.Args in project dcache by dCache.
the class ShellApplication method console.
/**
* Start an interactive session. The user is supplied a prompt and their input is executed as a
* command. This repeats until they indicate that they wish to exit the session.
*/
public void console() throws Throwable {
onInteractiveStart();
try {
while (true) {
String prompt = Ansi.ansi().bold().a(getPrompt()).boldOff().toString();
String str = console.readLine(prompt);
if (str == null) {
console.println();
break;
}
execute(new Args(str));
}
} catch (CommandExitException ignored) {
}
}
use of org.dcache.util.Args in project dcache by dCache.
the class AnnotatedCommandScannerTest method shouldUseNegativeArgumentArgumentIndexToCountFromTheEnd.
@Test
public void shouldUseNegativeArgumentArgumentIndexToCountFromTheEnd() throws Exception {
class SUT {
@Command(name = "test")
class TestCommand implements Callable<String> {
@Argument(index = -2)
int argument;
@Override
public String call() {
assertThat(argument, is(1));
return null;
}
}
}
Map<List<String>, ? extends CommandExecutor> commands = _scanner.scan(new SUT());
commands.get(asList("test")).execute(new Args("1 2"));
}
use of org.dcache.util.Args 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 org.dcache.util.Args in project dcache by dCache.
the class AnnotatedCommandScannerTest method shouldAcceptCommonTypes.
@Test
public void shouldAcceptCommonTypes() throws Exception {
class SUT {
@Command(name = "test")
class TestCommand implements Callable<String> {
@Argument(index = 0)
Integer arg0;
@Argument(index = 1)
int arg1;
@Argument(index = 2)
Short arg2;
@Argument(index = 3)
short arg3;
@Argument(index = 4)
Long arg4;
@Argument(index = 5)
long arg5;
@Argument(index = 6)
Byte arg6;
@Argument(index = 7)
byte arg7;
@Argument(index = 8)
Float arg8;
@Argument(index = 9)
float arg9;
@Argument(index = 10)
Double arg10;
@Argument(index = 11)
double arg11;
@Argument(index = 12)
String arg12;
@Argument(index = 13)
Character arg13;
@Argument(index = 14)
char arg14;
@Argument(index = 15)
AnEnum arg15;
@Argument(index = 16)
File arg16;
@Argument(index = 17)
Time arg17;
@Argument(index = 18)
long[] arg18;
@Override
public String call() {
assertThat(arg0, is(0));
assertThat(arg1, is(1));
assertThat(arg2, is((short) 2));
assertThat(arg3, is((short) 3));
assertThat(arg4, is(4L));
assertThat(arg5, is(5L));
assertThat(arg6, is((byte) 6));
assertThat(arg7, is((byte) 7));
assertThat(arg8, is((float) 8.0));
assertThat(arg9, is((float) 9.0));
assertThat(arg10, is(10.0));
assertThat(arg11, is(11.0));
assertThat(arg12, is("12"));
assertThat(arg13, is('a'));
assertThat(arg14, is('b'));
assertThat(arg15, is(AnEnum.BAR));
assertThat(arg16, is(new File("/my/file")));
assertThat(arg17, is(Time.valueOf("12:34:56")));
assertArrayEquals(arg18, new long[] { 100, 101 });
return null;
}
}
}
Map<List<String>, ? extends CommandExecutor> commands = _scanner.scan(new SUT());
commands.get(asList("test")).execute(new Args("0 1 2 3 4 5 6 7 8.0 9 10 11.00 12 a b BAR /my/file 12:34:56 100 101"));
}
Aggregations