private static ShellCommand sed(String filePath) { ShellCommand shellCommand = new ShellCommand(); shellCommand.setCommand("sed"); shellCommand.setOption(String.format("-n '%s' %s", getSedRegx(), getFile(filePath))); return shellCommand; }
private boolean doesCommandExist(String commandName, int arity) { for (ShellCommand cmd : commandTable) { if (cmd.canBeDenotedBy(commandName) && cmd.getArity() == arity) { return true; } } return false; }
private static ShellCommand awk() { ShellCommand shellCommand = new ShellCommand(); shellCommand.setCommand("awk"); shellCommand.setOption("'{print $9}'"); return shellCommand; }
private static ShellCommand grep() { ShellCommand shellCommand = new ShellCommand(); shellCommand.setCommand("grep"); shellCommand.setOption("-E '^(4|5)'"); return shellCommand; }
private static ShellCommand wc() { ShellCommand shellCommand = new ShellCommand(); shellCommand.setCommand("wc"); shellCommand.setOption("-l"); return shellCommand; }
public List<ShellCommand> commandsByName(String discriminator) { List<ShellCommand> collectedTable = new ArrayList<ShellCommand>(); // collection for (ShellCommand cs : commandTable) { if (cs.canBeDenotedBy(discriminator)) { collectedTable.add(cs); } } return collectedTable; }
protected void executeCommand(String query) throws ShellException { /*StringTokenizer tokenizer = new StringTokenizer(query); int argNum = tokenizer.countTokens(); if (argNum == 0) { return; // empty query } String commandName = tokenizer.nextToken(); String[] commandArgs = new String[argNum - 1]; for(int i = 0; i < argNum - 1; ++i) { commandArgs[i] = tokenizer.nextToken(); } ShellCommand currentCommand = mapCommand.get(commandName); if (currentCommand == null) { throw new ShellException(commandName, "No such command."); } currentCommand.execute(commandArgs);*/ String commandName = null; String[] arguments = new String[0]; try { String[] commandAndArgument = query.trim().split("\\s", 2); commandName = ""; arguments = new String[0]; if (commandAndArgument.length > 0) { commandName = commandAndArgument[0]; } if (!(commandName.length() > 0)) { return; // empty command } if (commandAndArgument.length > 1) { arguments = shellParseArguments(commandAndArgument[1]); } } catch (Exception e) { e.printStackTrace(); } ShellCommand currentCommand = mapCommand.get(commandName); if (currentCommand == null) { throw new ShellException(commandName, "No such command"); } justBeforeExecutingAction(commandName); currentCommand.execute(arguments); }
public ShellCommand lookupCommand(String discriminator, List<Token> tokens) throws CLIException { List<ShellCommand> collectedTable = commandsByName(discriminator); // reduction List<ShellCommand> reducedTable = new ArrayList<ShellCommand>(); for (ShellCommand cs : collectedTable) { if (cs.getMethod().getParameterTypes().length == tokens.size() - 1 || (cs.getMethod().isVarArgs() && (cs.getMethod().getParameterTypes().length <= tokens.size() - 1))) { reducedTable.add(cs); } } // selection if (collectedTable.size() == 0) { throw CLIException.createCommandNotFound(discriminator); } else if (reducedTable.size() == 0) { throw CLIException.createCommandNotFoundForArgNum(discriminator, tokens.size() - 1); } else if (reducedTable.size() > 1) { throw CLIException.createAmbiguousCommandExc(discriminator, tokens.size() - 1); } else { return reducedTable.get(0); } }
/** @see org.overlord.sramp.common.shell.ShellCommandReader#read() */ @Override public final ShellCommand read() throws Exception { String line = readLine(); if (line == null) { return null; } Arguments arguments = new Arguments(line); if (arguments.isEmpty()) { return new NoOpCommand(); } // The first argument is the qualified command name. QName commandName = arguments.removeCommandName(); // Create the command. ShellCommand command = factory.createCommand(commandName); command.setContext(this.context); command.setArguments(arguments); command.setOutput(getCommandOutput()); return command; }
public void addMethod(Method method, Object handler, String prefix) { Command annotation = method.getAnnotation(Command.class); assert method != null; String name; String autoAbbrev = null; if (annotation != null && annotation.name() != null && !annotation.name().equals("")) { name = annotation.name(); } else { CommandNamer.NamingInfo autoNames = namer.nameCommand(method); name = autoNames.commandName; for (String abbr : autoNames.possibleAbbreviations) { if (!doesCommandExist(prefix + abbr, method.getParameterTypes().length)) { autoAbbrev = abbr; break; } } } ShellCommand command = new ShellCommand(handler, method, prefix, name); if (annotation != null && annotation.abbrev() != null && !annotation.abbrev().equals("")) { command.setAbbreviation(annotation.abbrev()); } else { command.setAbbreviation(autoAbbrev); } if (annotation != null && annotation.description() != null && !annotation.description().equals("")) { command.setDescription(annotation.description()); } if (annotation != null && annotation.header() != null && !annotation.header().equals("")) { command.setHeader(annotation.header()); } commandTable.add(command); }
protected void addToCommandList(ShellCommand[] commandList) { // mapCommand.clear(); for (ShellCommand command : commandList) { mapCommand.put(command.getName(), command); } }