public boolean type()

in gogo/jline/src/main/java/org/apache/felix/gogo/jline/Builtin.java [303:464]


    public boolean type(CommandSession session, String[] argv) throws Exception {
        final String[] usage = {"type - show command type",
                "Usage: type [OPTIONS] [name[:]]",
                "  -a --all                 show all matches",
                "  -? --help                show help",
                "  -q --quiet               don't print anything, just return status",
                "  -s --scope=NAME          list all commands in named scope",
                "  -t --types               show full java type names"};

        Process process = Process.Utils.current();
        Options opt = Options.compile(usage).parse(argv);
        List<String> args = opt.args();

        if (opt.isSet("help")) {
            opt.usage(process.err());
            return true;
        }

        boolean all = opt.isSet("all");

        String optScope = null;
        if (opt.isSet("scope")) {
            optScope = opt.get("scope");
        }

        if (args.size() == 1) {
            String arg = args.get(0);
            if (arg.endsWith(":")) {
                optScope = args.remove(0);
            }
        }

        if (optScope != null || (args.isEmpty() && all)) {
            Set<String> snames = new TreeSet<>();

            for (String sname : (getCommands(session))) {
                if ((optScope == null) || sname.startsWith(optScope)) {
                    snames.add(sname);
                }
            }

            for (String sname : snames) {
                process.out().println(sname);
            }

            return true;
        }

        if (args.size() == 0) {
            Map<String, Integer> scopes = new TreeMap<>();

            for (String sname : getCommands(session)) {
                int colon = sname.indexOf(':');
                String scope = sname.substring(0, colon);
                Integer count = scopes.get(scope);
                if (count == null) {
                    count = 0;
                }
                scopes.put(scope, ++count);
            }

            for (Entry<String, Integer> entry : scopes.entrySet()) {
                process.out().println(entry.getKey() + ":" + entry.getValue());
            }

            return true;
        }

        final String name = args.get(0).toLowerCase();

        final int colon = name.indexOf(':');
        final String MAIN = "_main"; // FIXME: must match Reflective.java

        StringBuilder buf = new StringBuilder();
        Set<String> cmds = new LinkedHashSet<>();

        // get all commands
        if ((colon != -1) || (session.get(name) != null)) {
            cmds.add(name);
        } else if (session.get(MAIN) != null) {
            cmds.add(MAIN);
        } else {
            String path = session.get("SCOPE") != null ? session.get("SCOPE").toString()
                    : "*";

            for (String s : path.split(":")) {
                if (s.equals("*")) {
                    for (String sname : getCommands(session)) {
                        if (sname.endsWith(":" + name)) {
                            cmds.add(sname);
                            if (!all) {
                                break;
                            }
                        }
                    }
                } else {
                    String sname = s + ":" + name;
                    if (session.get(sname) != null) {
                        cmds.add(sname);
                        if (!all) {
                            break;
                        }
                    }
                }
            }
        }

        for (String key : cmds) {
            Object target = session.get(key);
            if (target == null) {
                continue;
            }

            CharSequence source = getClosureSource(session, key);

            if (source != null) {
                buf.append(name);
                buf.append(" is function {");
                buf.append(source);
                buf.append("}");
                continue;
            }

            for (Method m : getMethods(session, key)) {
                StringBuilder params = new StringBuilder();

                for (Class<?> type : m.getParameterTypes()) {
                    if (params.length() > 0) {
                        params.append(", ");
                    }
                    params.append(type.getSimpleName());
                }

                String rtype = m.getReturnType().getSimpleName();

                if (buf.length() > 0) {
                    buf.append("\n");
                }

                if (opt.isSet("types")) {
                    String cname = m.getDeclaringClass().getName();
                    buf.append(String.format("%s %s.%s(%s)", rtype, cname, m.getName(),
                            params));
                } else {
                    buf.append(String.format("%s is %s %s(%s)", name, rtype, key, params));
                }
            }
        }

        if (buf.length() > 0) {
            if (!opt.isSet("quiet")) {
                process.out().println(buf);
            }
            return true;
        }

        if (!opt.isSet("quiet")) {
            process.err().println("type: " + name + " not found.");
        }

        return false;
    }