use of org.jline.builtins.Options in project felix by apache.
the class Telnet method telnetd.
public void telnetd(CommandSession session, String[] argv) throws IOException {
final String[] usage = { "telnetd - start simple telnet server", "Usage: telnetd [-i ip] [-p port] start | stop | status", " -i --ip=INTERFACE listen interface (default=127.0.0.1)", " -p --port=PORT listen port (default=" + defaultPort + ")", " -? --help show help" };
Options opt = Options.compile(usage).parse(argv);
List<String> args = opt.args();
if (opt.isSet("help") || args.isEmpty()) {
opt.usage(System.err);
return;
}
String command = args.get(0);
if ("start".equals(command)) {
if (portListener != null) {
throw new IllegalStateException("telnetd is already running on port " + port);
}
ip = opt.get("ip");
port = opt.getNumber("port");
start(session);
status();
} else if ("stop".equals(command)) {
if (portListener == null) {
throw new IllegalStateException("telnetd is not running.");
}
stop();
} else if ("status".equals(command)) {
status();
} else {
throw opt.usageError("bad command: " + command);
}
}
use of org.jline.builtins.Options in project karaf by apache.
the class Procedural method doNot.
protected Boolean doNot(CommandSession session, Process process, Object[] argv) throws Exception {
String[] usage = { "not - return the opposite condition", "Usage: not { condition }", " -? --help Show help" };
Options opt = parseOptions(session, usage, argv);
List<Function> functions = getFunctions(opt);
if (functions == null || functions.size() != 1) {
process.err().println("usage: not { condition }");
process.error(2);
return null;
}
return !isTrue(session, functions.get(0));
}
use of org.jline.builtins.Options in project karaf by apache.
the class Procedural method doTry.
protected Object doTry(CommandSession session, Process process, Object[] argv) throws Exception {
String[] usage = { "try - try / catch / finally construct", "Usage: try { try-action } [ [catch] { catch-action } [ [finally] { finally-action } ] ]", " -? --help Show help" };
Options opt = parseOptions(session, usage, argv);
Function tryAction = null;
Function catchFunction = null;
Function finallyFunction = null;
int step = 0;
boolean error = false;
for (Object obj : opt.argObjects()) {
if (tryAction == null) {
if (obj instanceof Function) {
tryAction = (Function) obj;
} else {
error = true;
break;
}
step = 1;
} else if ("catch".equals(obj)) {
if (step != 1) {
error = true;
break;
}
step = 2;
} else if ("finally".equals(obj)) {
if (step != 1 && step != 3) {
error = true;
break;
}
step = 4;
} else if (step == 1 || step == 2) {
if (obj instanceof Function) {
catchFunction = (Function) obj;
} else {
error = true;
break;
}
step = 3;
} else if (step == 3 || step == 4) {
if (obj instanceof Function) {
finallyFunction = (Function) obj;
} else {
error = true;
break;
}
step = 5;
} else {
error = true;
break;
}
}
error |= tryAction == null;
error |= catchFunction == null && finallyFunction == null;
if (error) {
process.err().println("usage: try { try-action } [ [catch] { catch-action } [ [finally] { finally-action } ] ]");
process.error(2);
return null;
}
try {
return tryAction.execute(session, null);
} catch (BreakException b) {
throw b;
} catch (Exception e) {
session.put("exception", e);
if (catchFunction != null) {
catchFunction.execute(session, null);
}
return null;
} finally {
if (finallyFunction != null) {
finallyFunction.execute(session, null);
}
}
}
use of org.jline.builtins.Options in project karaf by apache.
the class Procedural method doWhile.
protected Object doWhile(CommandSession session, Process process, Object[] argv) throws Exception {
String[] usage = { "while - while loop", "Usage: while { condition } [do] { action }", " -? --help Show help" };
Options opt = parseOptions(session, usage, argv);
Function condition = null;
Function action = null;
int step = 0;
boolean error = false;
for (Object obj : opt.argObjects()) {
if (condition == null) {
if (obj instanceof Function) {
condition = (Function) obj;
} else {
error = true;
break;
}
step = 1;
} else if ("do".equals(obj)) {
if (step != 1) {
error = true;
break;
}
step = 2;
} else if (step == 1 || step == 2) {
if (obj instanceof Function) {
action = (Function) obj;
} else {
error = true;
break;
}
step = 3;
} else {
error = true;
break;
}
}
error |= condition == null;
error |= action == null;
if (error) {
process.err().println("usage: while { condition } [do] { action }");
process.error(2);
return null;
}
while (isTrue(session, condition)) {
try {
action.execute(session, null);
} catch (BreakException b) {
break;
} catch (ContinueException c) {
continue;
}
}
return null;
}
use of org.jline.builtins.Options in project karaf by apache.
the class Procedural method doIf.
protected Object doIf(CommandSession session, Process process, Object[] argv) throws Exception {
String[] usage = { "if - if / then / else construct", "Usage: if {condition} [then] {if-action} [elif {cond} [then] {elif-action}]... [else] {else-action}", " -? --help Show help" };
Options opt = parseOptions(session, usage, argv);
List<Function> conditions = new ArrayList<>();
List<Function> actions = new ArrayList<>();
Function elseFunction = null;
int step = 0;
boolean error = false;
for (Object obj : opt.argObjects()) {
switch(step) {
case 0:
if (obj instanceof Function) {
conditions.add((Function) obj);
} else {
error = true;
}
step = 1;
break;
case 1:
if ("then".equals(obj)) {
step = 2;
break;
}
case 2:
if (obj instanceof Function) {
actions.add((Function) obj);
step = 3;
} else {
error = true;
}
break;
case 3:
if ("elif".equals(obj)) {
step = 4;
} else if ("else".equals(obj)) {
step = 7;
} else if (obj instanceof Function) {
elseFunction = (Function) obj;
step = 8;
} else {
error = true;
}
break;
case 4:
if (obj instanceof Function) {
conditions.add((Function) obj);
} else {
error = true;
}
step = 5;
break;
case 5:
if ("then".equals(obj)) {
step = 6;
break;
}
case 6:
if (obj instanceof Function) {
actions.add((Function) obj);
step = 3;
} else {
error = true;
}
break;
case 7:
if (obj instanceof Function) {
elseFunction = (Function) obj;
step = 8;
} else {
error = true;
}
break;
case 8:
error = true;
break;
}
if (error) {
break;
}
}
error |= conditions.isEmpty();
error |= conditions.size() != actions.size();
if (error) {
process.err().println("usage: if {condition} [then] {if-action} [elif {elif-action}]... [else] {else-action}");
process.error(2);
return null;
}
for (int i = 0, length = conditions.size(); i < length; ++i) {
if (isTrue(session, conditions.get(i))) {
return actions.get(i).execute(session, null);
}
}
if (elseFunction != null) {
return elseFunction.execute(session, null);
}
return null;
}
Aggregations