Search in sources :

Example 36 with Options

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);
    }
}
Also used : Options(org.jline.builtins.Options)

Example 37 with Options

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));
}
Also used : Options(org.jline.builtins.Options) Function(org.apache.felix.service.command.Function)

Example 38 with Options

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);
        }
    }
}
Also used : Options(org.jline.builtins.Options) Function(org.apache.felix.service.command.Function)

Example 39 with Options

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;
}
Also used : Options(org.jline.builtins.Options) Function(org.apache.felix.service.command.Function)

Example 40 with Options

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;
}
Also used : Options(org.jline.builtins.Options) Function(org.apache.felix.service.command.Function)

Aggregations

Options (org.jline.builtins.Options)41 AttributedString (org.jline.utils.AttributedString)16 Function (org.apache.felix.service.command.Function)15 ArrayList (java.util.ArrayList)14 Process (org.apache.felix.service.command.Process)12 InputStreamReader (java.io.InputStreamReader)10 BufferedReader (java.io.BufferedReader)9 InputStream (java.io.InputStream)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 FilterInputStream (java.io.FilterInputStream)8 PrintStream (java.io.PrintStream)8 Path (java.nio.file.Path)8 CommandSession (org.apache.felix.service.command.CommandSession)8 AttributedStringBuilder (org.jline.utils.AttributedStringBuilder)8 ByteArrayOutputStream (java.io.ByteArrayOutputStream)7 IOException (java.io.IOException)7 HashSet (java.util.HashSet)7 Reader (java.io.Reader)6 TreeMap (java.util.TreeMap)6 Closeable (java.io.Closeable)5