in shell/core/src/main/java/org/apache/karaf/shell/impl/console/commands/Procedural.java [169:271]
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;
}