@Test @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()); Assert.assertEquals(args[2], ts.next()); Assert.assertEquals(false, ts.hasNext()); }
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; } }
@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()); }
@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); }
@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); } }
@Override public void execute() throws NameNotFoundException, ShellException, HelpException { // The above exceptions are either bugs or configuration errors and should be allowed // to propagate so that the shell can diagnose them appropriately. String alias; CommandLine commandLine = getCommandLine(); PrintWriter out = getOutput().getPrintWriter(); PrintWriter err = getError().getPrintWriter(); if (argAlias.isSet()) { alias = argAlias.getValue(); } else if (commandLine.getCommandName() != null) { alias = commandLine.getCommandName(); } else { alias = "help"; } CommandShell shell = null; try { shell = (CommandShell) ShellUtils.getShellManager().getCurrentShell(); CommandInfo cmdInfo = shell.getCommandInfo(alias); Help cmdHelp = HelpFactory.getHelpFactory().getHelp(alias, cmdInfo); if (cmdHelp == null) { err.format(err_no_help, alias); exit(1); } cmdHelp.help(out); otherAliases(shell.getAliasManager(), alias, cmdInfo.getCommandClass().getName(), out); } catch (HelpException ex) { err.format(err_help_ex, alias, ex.getLocalizedMessage()); throw ex; } catch (ShellException ex) { err.println(ex.getMessage()); throw ex; } catch (SecurityException ex) { err.format(err_sec_ex, alias, ex.getLocalizedMessage()); throw ex; } }