Search in sources :

Example 16 with Options

use of org.jline.builtins.Options in project felix 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 17 with Options

use of org.jline.builtins.Options in project karaf by apache.

the class Procedural method doUntil.

protected Object doUntil(CommandSession session, Process process, Object[] argv) throws Exception {
    String[] usage = { "until -  until loop", "Usage: until { 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: until { condition } [do] { action }");
        process.error(2);
        return null;
    }
    while (!isTrue(session, condition)) {
        try {
            action.execute(session, null);
        } catch (BreakException e) {
            break;
        } catch (ContinueException c) {
            continue;
        }
    }
    return null;
}
Also used : Options(org.jline.builtins.Options) Function(org.apache.felix.service.command.Function)

Example 18 with Options

use of org.jline.builtins.Options in project karaf by apache.

the class Procedural method parseOptions.

protected Options parseOptions(CommandSession session, String[] usage, Object[] argv) throws HelpException, OptionException {
    try {
        Options opt = Options.compile(usage, s -> get(session, s)).parse(argv, true);
        if (opt.isSet("help")) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            opt.usage(new PrintStream(baos));
            throw new HelpException(baos.toString());
        }
        return opt;
    } catch (IllegalArgumentException e) {
        throw new OptionException(e.getMessage(), e);
    }
}
Also used : Process(org.apache.felix.service.command.Process) PrintStream(java.io.PrintStream) java.util(java.util) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Function(org.apache.felix.service.command.Function) CommandSession(org.apache.felix.service.command.CommandSession) Options(org.jline.builtins.Options) Options(org.jline.builtins.Options) PrintStream(java.io.PrintStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 19 with Options

use of org.jline.builtins.Options in project karaf by apache.

the class Procedural method doEach.

protected List<Object> doEach(CommandSession session, Process process, Object[] argv) throws Exception {
    String[] usage = { "each -  loop over the elements", "Usage: each [-r] elements [do] { closure }", "         elements              an array to iterate on", "         closure               a closure to call", "  -? --help                    Show help", "  -r --result                  Return a list containing each iteration result" };
    Options opt = parseOptions(session, usage, argv);
    Collection<Object> elements = getElements(opt);
    if (opt.argObjects().size() > 0 && "do".equals(opt.argObjects().get(0))) {
        opt.argObjects().remove(0);
    }
    List<Function> functions = getFunctions(opt);
    if (elements == null || functions == null || functions.size() != 1) {
        process.err().println("usage: each elements [do] { closure }");
        process.err().println("       elements: an array to iterate on");
        process.err().println("       closure: a function or closure to call");
        process.error(2);
        return null;
    }
    List<Object> args = new ArrayList<>();
    List<Object> results = new ArrayList<>();
    args.add(null);
    for (Object x : elements) {
        checkInterrupt();
        args.set(0, x);
        try {
            results.add(functions.get(0).execute(session, args));
        } catch (BreakException b) {
            break;
        } catch (ContinueException c) {
            continue;
        }
    }
    return opt.isSet("result") ? results : null;
}
Also used : Options(org.jline.builtins.Options) Function(org.apache.felix.service.command.Function)

Example 20 with Options

use of org.jline.builtins.Options in project felix by apache.

the class Builtin method tac.

/*
     * the following methods depend on the internals of the runtime implementation.
     * ideally, they should be available via some API.
     */
public Object tac(CommandSession session, String[] argv) throws IOException {
    final String[] usage = { "tac - capture stdin as String or List and optionally write to file.", "Usage: tac [-al] [FILE]", "  -a --append              append to FILE", "  -l --list                return List<String>", "  -? --help                show help" };
    Process process = Process.Utils.current();
    Options opt = Options.compile(usage).parse(argv);
    if (opt.isSet("help")) {
        opt.usage(process.err());
        return null;
    }
    List<String> args = opt.args();
    BufferedWriter fw = null;
    if (args.size() == 1) {
        Path path = session.currentDir().resolve(args.get(0));
        Set<OpenOption> options = new HashSet<>();
        options.add(StandardOpenOption.WRITE);
        options.add(StandardOpenOption.CREATE);
        if (opt.isSet("append")) {
            options.add(StandardOpenOption.APPEND);
        } else {
            options.add(StandardOpenOption.TRUNCATE_EXISTING);
        }
        fw = Files.newBufferedWriter(path, StandardCharsets.UTF_8, options.toArray(new OpenOption[options.size()]));
    }
    StringWriter sw = new StringWriter();
    BufferedReader rdr = new BufferedReader(new InputStreamReader(process.in()));
    ArrayList<String> list = null;
    if (opt.isSet("list")) {
        list = new ArrayList<String>();
    }
    boolean first = true;
    String s;
    while ((s = rdr.readLine()) != null) {
        if (list != null) {
            list.add(s);
        } else {
            if (!first) {
                sw.write(' ');
            }
            first = false;
            sw.write(s);
        }
        if (fw != null) {
            fw.write(s);
            fw.newLine();
        }
    }
    if (fw != null) {
        fw.close();
    }
    return list != null ? list : sw.toString();
}
Also used : Path(java.nio.file.Path) Options(org.jline.builtins.Options) InputStreamReader(java.io.InputStreamReader) Process(org.apache.felix.service.command.Process) BufferedWriter(java.io.BufferedWriter) OpenOption(java.nio.file.OpenOption) StandardOpenOption(java.nio.file.StandardOpenOption) StringWriter(java.io.StringWriter) BufferedReader(java.io.BufferedReader) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

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