Package org.jnode.shell

Examples of org.jnode.shell.CommandLine


        shell.addSyntax("cmd", new OptionSetSyntax(new OptionSyntax("intArg", 'i'),
                new OptionSyntax("fileArg", 'f'), new OptionSyntax("flagArg1", 'x'),
                new OptionSyntax("flagArg2", 'y'), new OptionSyntax("flagArg3", 'z'),
                new OptionSyntax("flagArg4", "boring")));

        CommandLine cl;
        CommandInfo cmdInfo;
        Command cmd;

        cl = new CommandLine(new Token("cmd"), new Token[] {}, null);
        cmdInfo = cl.parseCommandLine(shell);
        cmd = cmdInfo.createCommandInstance();
        Assert.assertEquals(0, cmd.getArgumentBundle().getArgument("fileArg").getValues().length);
        Assert.assertEquals(0, cmd.getArgumentBundle().getArgument("intArg").getValues().length);

        cl =
                new CommandLine(new Token("cmd"), new Token[] {new Token("-f"), new Token("F1")},
                        null);
        cmdInfo = cl.parseCommandLine(shell);
        cmd = cmdInfo.createCommandInstance();
        Assert.assertEquals(1, cmd.getArgumentBundle().getArgument("fileArg").getValues().length);
        Assert.assertEquals(0, cmd.getArgumentBundle().getArgument("intArg").getValues().length);
        Assert.assertEquals("F1", cmd.getArgumentBundle().getArgument("fileArg").getValue()
                .toString());

        cl =
                new CommandLine(new Token("cmd"), new Token[] {new Token("-f"), new Token("F1"),
                    new Token("-x"), new Token("-yz")}, null);
        cmdInfo = cl.parseCommandLine(shell);
        cmd = cmdInfo.createCommandInstance();
        Assert.assertEquals(1, cmd.getArgumentBundle().getArgument("fileArg").getValues().length);
        Assert.assertEquals(0, cmd.getArgumentBundle().getArgument("intArg").getValues().length);
        Assert.assertEquals(1, cmd.getArgumentBundle().getArgument("flagArg1").getValues().length);
        Assert.assertEquals(1, cmd.getArgumentBundle().getArgument("flagArg2").getValues().length);
        Assert.assertEquals(1, cmd.getArgumentBundle().getArgument("flagArg3").getValues().length);

        cl = new CommandLine(new Token("cmd"), new Token[] {new Token("-yz")}, null);
        cmdInfo = cl.parseCommandLine(shell);
        cmd = cmdInfo.createCommandInstance();
        Assert.assertEquals(0, cmd.getArgumentBundle().getArgument("fileArg").getValues().length);
        Assert.assertEquals(0, cmd.getArgumentBundle().getArgument("intArg").getValues().length);
        Assert.assertEquals(0, cmd.getArgumentBundle().getArgument("flagArg1").getValues().length);
        Assert.assertEquals(1, cmd.getArgumentBundle().getArgument("flagArg2").getValues().length);
        Assert.assertEquals(1, cmd.getArgumentBundle().getArgument("flagArg3").getValues().length);

        try {
            cl = new CommandLine(new Token("cmd"), new Token[] {new Token("-xya")}, null);
            cl.parseCommandLine(shell);
            Assert.fail("no exception");
        } catch (CommandSyntaxException ex) {
            // expected
        }

        try {
            cl = new CommandLine(new Token("cmd"), new Token[] {new Token("-")}, null);
            cl.parseCommandLine(shell);
            Assert.fail("no exception");
        } catch (CommandSyntaxException ex) {
            // expected
        }
    }
View Full Code Here


                rc = 0;
            } else {
                // FIXME ... strictly speaking, alias substitution should be performed
                // before "applying the grammatical rules" (i.e. parsing).
                words = context.substituteAliases(words);
                CommandLine command = context.buildCommandLine(words);
                // Assignments and redirections are done in the command's context
                BjorneContext childContext = new BjorneContext(context);
                childContext.performAssignments(assignments);
                holders = childContext.evaluateRedirections(getRedirects());
                CommandIO[] ios = new CommandIO[holders.length];
View Full Code Here

    }
   
    public CommandThread fork(CommandShell shell, BjorneContext context)
        throws ShellException {
        if (words.length > 0) {
            CommandLine command = context.buildCommandLine(words);
            command.setStreams(context.getIOs());
            return shell.invokeAsynchronous(command);
        } else {
            return null;
        }
    }
View Full Code Here

    @Override
    public void complete(CompletionInfo completions, BjorneContext context, CommandShell shell,
            boolean argumentAnticipated)
        throws CompletionException {
        try {
            CommandLine command = context.buildCommandLine(words);
            String commandName = command.getCommandName();
            if (commandName != null && BjorneInterpreter.isBuiltin(commandName)) {
                BjorneBuiltinCommandInfo commandInfo =
                    BjorneInterpreter.BUILTINS.get(commandName).buildCommandInfo(context);
                command.setCommandInfo(commandInfo);
            }
            command.setArgumentAnticipated(argumentAnticipated);
            command.complete(completions, shell);
        } catch (ShellException ex) {
            throw new CompletionException("Shell exception", ex);
        }
    }
View Full Code Here

     */
    public CommandLine buildCommandLine(BjorneToken ... tokens) throws ShellException {
        List<BjorneToken> wordTokens = expandAndSplit(tokens);
        int nosWords = wordTokens.size();
        if (nosWords == 0) {
            return new CommandLine(null, null);
        } else {
            BjorneToken alias = wordTokens.remove(0);
            BjorneToken[] args = wordTokens.toArray(new BjorneToken[nosWords - 1]);
            return new CommandLine(alias, args, null);
        }
    }
View Full Code Here

    @Test
    @SuppressWarnings("deprecation")
    public void testStringConstructors() {
        String[] args = new String[] {"1", "2", "3"};

        CommandLine c1 = new CommandLine(args);
        Assert.assertEquals(null, c1.getCommandName());
        Assert.assertEquals(null, c1.getCommandToken());

        SymbolSource<String> ss = c1.iterator();
        Assert.assertEquals(true, ss.hasNext());
        Assert.assertEquals("1", ss.next());
        Assert.assertEquals(true, ss.hasNext());
        Assert.assertEquals("2", ss.next());
        Assert.assertEquals(true, ss.hasNext());
        Assert.assertEquals("3", ss.next());
        Assert.assertEquals(false, ss.hasNext());

        SymbolSource<Token> ts = c1.tokenIterator();
        Assert.assertEquals(true, ts.hasNext());
        Assert.assertEquals("1", ts.next().text);
        Assert.assertEquals(true, ts.hasNext());
        Assert.assertEquals("2", ts.next().text);
        Assert.assertEquals(true, ts.hasNext());
        Assert.assertEquals("3", ts.next().text);
        Assert.assertEquals(false, ts.hasNext());

        CommandLine c2 = new CommandLine("foo", args);
        Assert.assertEquals("foo", c2.getCommandName());
        Assert.assertEquals("foo", c2.getCommandToken().text);
    }
View Full Code Here

    @SuppressWarnings("deprecation")
    public void testTokenConstructors() {
        Token foo = new Token("foo");
        Token[] args = new Token[] {new Token("1"), new Token("2"), new Token("3")};

        CommandLine c1 = new CommandLine(foo, args, null);
        Assert.assertEquals("foo", c1.getCommandName());
        Assert.assertEquals(foo, c1.getCommandToken());

        SymbolSource<String> ss = c1.iterator();
        Assert.assertEquals(true, ss.hasNext());
        Assert.assertEquals("1", ss.next());
        Assert.assertEquals(true, ss.hasNext());
        Assert.assertEquals("2", ss.next());
        Assert.assertEquals(true, ss.hasNext());
        Assert.assertEquals("3", ss.next());
        Assert.assertEquals(false, ss.hasNext());

        SymbolSource<Token> ts = c1.tokenIterator();
        Assert.assertEquals(true, ts.hasNext());
        Assert.assertEquals(args[0], ts.next());
        Assert.assertEquals(true, ts.hasNext());
        Assert.assertEquals(args[1], ts.next());
        Assert.assertEquals(true, ts.hasNext());
View Full Code Here

    @Test
    public void testParse() throws Exception {
        TestShell shell = new TestShell();
        shell.addAlias("command", "org.jnode.test.shell.syntax.CommandLineTest$TestCommand");
        shell.addSyntax("command", new ArgumentSyntax("arg1"));
        CommandLine cl =
                new CommandLine(new Token("command"), new Token[] {new Token("fish")}, null);
        CommandInfo cmdInfo = cl.parseCommandLine(shell);
        Command cmd = cmdInfo.createCommandInstance();
        Assert.assertEquals("fish", cmd.getArgumentBundle().getArgument("arg1").getValue());
    }
View Full Code Here

    public void testZeroToMany() throws Exception {
        TestShell shell = new TestShell();
        shell.addAlias("cmd", "org.jnode.test.shell.syntax.RepeatSyntaxTest$Test");
        shell.addSyntax("cmd", new RepeatSyntax(new ArgumentSyntax("arg1")));

        CommandLine cl = new CommandLine(new Token("cmd"), new Token[] {}, null);
        CommandInfo cmdInfo = cl.parseCommandLine(shell);
        Command cmd = cmdInfo.createCommandInstance();
        Assert.assertEquals(0, cmd.getArgumentBundle().getArgument("arg1").getValues().length);

        cl = new CommandLine(new Token("cmd"), new Token[] {new Token("F1")}, null);
        cmdInfo = cl.parseCommandLine(shell);
        cmd = cmdInfo.createCommandInstance();
        Assert.assertEquals(1, cmd.getArgumentBundle().getArgument("arg1").getValues().length);

        cl =
                new CommandLine(new Token("cmd"), new Token[] {new Token("F1"), new Token("F1")},
                        null);
        cmdInfo = cl.parseCommandLine(shell);
        cmd = cmdInfo.createCommandInstance();
        Assert.assertEquals(2, cmd.getArgumentBundle().getArgument("arg1").getValues().length);

    }
View Full Code Here

    public void testOneToMany() throws Exception {
        TestShell shell = new TestShell();
        shell.addAlias("cmd", "org.jnode.test.shell.syntax.RepeatSyntaxTest$Test");
        shell.addSyntax("cmd", new RepeatSyntax(new ArgumentSyntax("arg1"), 1, Integer.MAX_VALUE));

        CommandLine cl;
        CommandInfo cmdInfo;
        Command cmd;

        try {
            cl = new CommandLine(new Token("cmd"), new Token[] {}, null);
            cl.parseCommandLine(shell);
            Assert.fail("no exception");
        } catch (CommandSyntaxException ex) {
            // expected
        }

        cl = new CommandLine(new Token("cmd"), new Token[] {new Token("F1")}, null);
        cmdInfo = cl.parseCommandLine(shell);
        cmd = cmdInfo.createCommandInstance();
        Assert.assertEquals(1, cmd.getArgumentBundle().getArgument("arg1").getValues().length);

        cl =
                new CommandLine(new Token("cmd"), new Token[] {new Token("F1"), new Token("F1")},
                        null);
        cmdInfo = cl.parseCommandLine(shell);
        cmd = cmdInfo.createCommandInstance();
        Assert.assertEquals(2, cmd.getArgumentBundle().getArgument("arg1").getValues().length);
    }
View Full Code Here

TOP

Related Classes of org.jnode.shell.CommandLine

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.