Package org.apache.felix.gogo.options

Examples of org.apache.felix.gogo.options.Option


        "Usage: runTests [OPTION]... [TESTS]...",
        "  -? --help                show help",
        "  -d --directory=DIR       Write test results to specified directory",
        "  -q --quiet               Do not output test results to console"};
       
        Option opts = Options.compile( usage ).parse( args );
       
        boolean quiet = opts.isSet( "quiet" );
        Object d = opts.isSet( "directory" ) ? opts.getObject( "directory" ) : null;
        File dir = null;
        if (d != null)
        {
            if ( d instanceof File ) {
                dir = ( File ) d;
            }
            else {
                dir = new File(d.toString());               
            }
            dir.mkdirs();
            if (!quiet) {
                System.out.println("Writing results to " + dir.getAbsolutePath());
                System.out.flush();
            }
        }
       
        List<Object> tests = opts.argObjects();
       
        return runTests(tests, quiet, dir);
    }
View Full Code Here


                "  -s --noshutdown          don't shutdown framework when script completes",
                "  -x --xtrace              echo commands before execution",
                "  -? --help                show help",
                "If no script-file, an interactive shell is started, type $D to exit." };

        Option opt = Options.compile(usage).setOptionsFirst(true).parse(argv);
        List<String> args = opt.args();

        boolean login = opt.isSet("login");
        boolean interactive = !opt.isSet("nointeractive");

        if (opt.isSet("help"))
        {
            opt.usage();
            if (login && !opt.isSet("noshutdown"))
            {
                shutdown();
            }
            return null;
        }

        if (opt.isSet("command") && args.isEmpty())
        {
            throw opt.usageError("option --command requires argument(s)");
        }

        CommandSession newSession = (login ? session : processor.createSession(
            session.getKeyboard(), session.getConsole(), System.err));

        if (opt.isSet("xtrace"))
        {
            newSession.put("echo", true);
        }

        if (login && interactive)
        {
            URI uri = baseURI.resolve("etc/gosh_profile");
            if (!new File(uri).exists())
            {
                URL url = getClass().getResource("/ext/gosh_profile");
                if (url == null) {
                    url = getClass().getResource("/gosh_profile");
                }
                uri = (url == null) ? null : url.toURI();
            }
            if (uri != null)
            {
                source(session, uri.toString());
            }
        }

        // export variables starting with upper-case to newSession
        for (String key : getVariables(session))
        {
            if (key.matches("[.]?[A-Z].*"))
            {
                newSession.put(key, session.get(key));
            }
        }

        Object result = null;

        if (args.isEmpty())
        {
            if (interactive)
            {
                result = console(newSession);
            }
        }
        else
        {
            CharSequence program;

            if (opt.isSet("command"))
            {
                StringBuilder buf = new StringBuilder();
                for (String arg : args)
                {
                    if (buf.length() > 0)
                    {
                        buf.append(' ');
                    }
                    buf.append(arg);
                }
                program = buf;
            }
            else
            {
                URI script = cwd(session).resolve(args.remove(0));

                // set script arguments
                newSession.put("0", script);
                newSession.put("args", args);

                for (int i = 0; i < args.size(); ++i)
                {
                    newSession.put(String.valueOf(i + 1), args.get(i));
                }

                program = readScript(script);
            }

            result = newSession.execute(program);
        }

        if (login && interactive && !opt.isSet("noshutdown"))
        {
            System.out.println("gosh: stopping framework");
            shutdown();
        }

View Full Code Here

                "  -a --all                 show all variables, including those starting with .",
                "  -x                       set xtrace option",
                "  +x                       unset xtrace option",
                "If PREFIX given, then only show variable(s) starting with PREFIX" };

        Option opt = Options.compile(usage).parse(argv);

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

        List<String> args = opt.args();
        String prefix = (args.isEmpty() ? "" : args.get(0));

        if (opt.isSet("x"))
        {
            session.put("echo", true);
        }
        else if ("+x".equals(prefix))
        {
            session.put("echo", null);
        }
        else
        {
            boolean all = opt.isSet("all");
            for (String key : new TreeSet<String>(Shell.getVariables(session)))
            {
                if (!key.startsWith(prefix))
                    continue;
View Full Code Here

                "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" };

        Option opt = Options.compile(usage).parse(argv);

        if (opt.isSet("help"))
        {
            opt.usage();
            return null;
        }

        List<String> args = opt.args();
        BufferedWriter fw = null;

        if (args.size() == 1)
        {
            String path = args.get(0);
            File file = new File(Shell.cwd(session).resolve(path));
            fw = new BufferedWriter(new FileWriter(file, opt.isSet("append")));
        }

        StringWriter sw = new StringWriter();
        BufferedReader rdr = new BufferedReader(new InputStreamReader(System.in));

        ArrayList<String> list = null;

        if (opt.isSet("list"))
        {
            list = new ArrayList<String>();
        }

        boolean first = true;
View Full Code Here

                "  -? --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" };

        Option opt = Options.compile(usage).parse(argv);
        List<String> args = opt.args();

        if (opt.isSet("help"))
        {
            opt.usage();
            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<String>();

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

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

            return true;
        }

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

            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())
            {
                System.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<String>();

        // 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"))
            {
                System.out.println(buf);
            }
            return true;
        }

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

        return false;
View Full Code Here

                "  -i --ignore-case         ignore case distinctions",
                "  -n --line-number         prefix each line with line number within its input file",
                "  -q --quiet, --silent     suppress all normal output",
                "  -v --invert-match        select non-matching lines" };

        Option opt = Options.compile(usage).parse(argv);

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

        List<String> args = opt.args();

        if (args.size() == 0)
        {
            throw opt.usageError("no pattern supplied.");
        }

        String regex = args.remove(0);
        if (opt.isSet("ignore-case"))
        {
            regex = "(?i)" + regex;
        }

        if (args.isEmpty())
        {
            args.add(null);
        }

        StringBuilder buf = new StringBuilder();

        if (args.size() > 1)
        {
            buf.append("%1$s:");
        }

        if (opt.isSet("line-number"))
        {
            buf.append("%2$s:");
        }

        buf.append("%3$s");
        String format = buf.toString();

        Pattern pattern = Pattern.compile(regex);
        boolean status = true;
        boolean match = false;

        for (String arg : args)
        {
            InputStream in = null;

            try
            {
                URI cwd = Shell.cwd(session);
                in = (arg == null) ? System.in : cwd.resolve(arg).toURL().openStream();
               
                BufferedReader rdr = new BufferedReader(new InputStreamReader(in));
                int line = 0;
                String s;
                while ((s = rdr.readLine()) != null)
                {
                    line++;
                    Matcher matcher = pattern.matcher(s);
                    if (!(matcher.find() ^ !opt.isSet("invert-match")))
                    {
                        match = true;
                        if (opt.isSet("quiet"))
                            break;

                        System.out.println(String.format(format, arg, line, s));
                    }
                }

                if (match && opt.isSet("quiet"))
                {
                    break;
                }
            }
            catch (IOException e)
View Full Code Here

                "  -s --noshutdown          don't shutdown framework when script completes",
                "  -x --xtrace              echo commands before execution",
                "  -? --help                show help",
                "If no script-file, an interactive shell is started, type $D to exit." };

        Option opt = Options.compile(usage).setOptionsFirst(true).parse(argv);
        List<String> args = opt.args();

        boolean login = opt.isSet("login");
        boolean interactive = !opt.isSet("nointeractive");

        if (opt.isSet("help"))
        {
            opt.usage();
            if (login && !opt.isSet("noshutdown"))
            {
                shutdown();
            }
            return null;
        }

        if (opt.isSet("command") && args.isEmpty())
        {
            throw opt.usageError("option --command requires argument(s)");
        }

        CommandSession newSession = (login ? session : processor.createSession(
            session.getKeyboard(), session.getConsole(), System.err));

        if (opt.isSet("xtrace"))
        {
            newSession.put("echo", true);
        }

        if (login && interactive)
        {
            URI uri = baseURI.resolve("etc/gosh_profile");
            if (!new File(uri).exists())
            {
                uri = getClass().getResource("/gosh_profile").toURI();
            }
            if (uri != null)
            {
                source(session, uri.toString());
            }
        }

        // export variables starting with upper-case to newSession
        for (String key : getVariables(session))
        {
            if (key.matches("[.]?[A-Z].*"))
            {
                newSession.put(key, session.get(key));
            }
        }

        Object result = null;

        if (args.isEmpty())
        {
            if (interactive)
            {
                result = console(newSession);
            }
        }
        else
        {
            CharSequence program;

            if (opt.isSet("command"))
            {
                StringBuilder buf = new StringBuilder();
                for (String arg : args)
                {
                    if (buf.length() > 0)
                    {
                        buf.append(' ');
                    }
                    buf.append(arg);
                }
                program = buf;
            }
            else
            {
                URI script = cwd(session).resolve(args.remove(0));

                // set script arguments
                newSession.put("0", script);
                newSession.put("args", args);

                for (int i = 0; i < args.size(); ++i)
                {
                    newSession.put(String.valueOf(i + 1), args.get(i));
                }

                program = readScript(script);
            }

            result = newSession.execute(program);
        }

        if (login && interactive && !opt.isSet("noshutdown"))
        {
            System.out.println("gosh: stopping framework");
            shutdown();
        }

View Full Code Here

                "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" };

        Option opt = Options.compile(usage).parse(argv);
        List<String> args = opt.args();

        if (opt.isSet("help") || args.isEmpty())
        {
            opt.usage();
            return;
        }

        String command = args.get(0);

        if ("start".equals(command))
        {
            if (server != null)
            {
                throw new IllegalStateException("telnetd is already running on port "
                    + port);
            }
            ip = opt.get("ip");
            port = opt.getNumber("port");
            start();
            status();
        }
        else if ("stop".equals(command))
        {
            if (server == null)
            {
                throw new IllegalStateException("telnetd is not running.");
            }
            stop();
        }
        else if ("status".equals(command))
        {
            status();
        }
        else
        {
            throw opt.usageError("bad command: " + command);
        }
    }
View Full Code Here

TOP

Related Classes of org.apache.felix.gogo.options.Option

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.