Remove arity from dicttool commands (A1)

This unnecessarily complicates and limits commands.
It's simpler to just have one command by invocation.

Groundwork for
Bug: 6429606

Change-Id: I24cf267a9cbc937a5ba53942b29e28e7095d2fd2
main
Jean Chalard 2012-08-02 23:15:17 +09:00
parent 77e8e81ad9
commit 77c8c73837
3 changed files with 20 additions and 40 deletions

View File

@ -55,11 +55,10 @@ public class Compress {
return "compress <filename>: Compresses a file using gzip compression"; return "compress <filename>: Compresses a file using gzip compression";
} }
public int getArity() {
return 1;
}
public void run() throws IOException { public void run() throws IOException {
if (mArgs.length < 1) {
throw new RuntimeException("Not enough arguments for command " + COMMAND);
}
final String inFilename = mArgs[0]; final String inFilename = mArgs[0];
final String outFilename = inFilename + SUFFIX; final String outFilename = inFilename + SUFFIX;
final FileInputStream input = new FileInputStream(new File(inFilename)); final FileInputStream input = new FileInputStream(new File(inFilename));
@ -79,11 +78,10 @@ public class Compress {
return "uncompress <filename>: Uncompresses a file compressed with gzip compression"; return "uncompress <filename>: Uncompresses a file compressed with gzip compression";
} }
public int getArity() {
return 1;
}
public void run() throws IOException { public void run() throws IOException {
if (mArgs.length < 1) {
throw new RuntimeException("Not enough arguments for command " + COMMAND);
}
final String inFilename = mArgs[0]; final String inFilename = mArgs[0];
final String outFilename = inFilename + SUFFIX; final String outFilename = inFilename + SUFFIX;
final FileInputStream input = new FileInputStream(new File(inFilename)); final FileInputStream input = new FileInputStream(new File(inFilename));

View File

@ -27,7 +27,6 @@ public class Dicttool {
public void setArgs(String[] args) throws IllegalArgumentException { public void setArgs(String[] args) throws IllegalArgumentException {
mArgs = args; mArgs = args;
} }
abstract public int getArity();
abstract public String getHelp(); abstract public String getHelp();
abstract public void run() throws Exception; abstract public void run() throws Exception;
} }
@ -62,47 +61,29 @@ public class Dicttool {
return sCommands.containsKey(commandName); return sCommands.containsKey(commandName);
} }
private String mPreviousCommand = null; // local to the getNextCommand function private Command getCommand(final ArrayList<String> arguments) {
private Command getNextCommand(final ArrayList<String> arguments) {
final String firstArgument = arguments.get(0); final String firstArgument = arguments.get(0);
final String commandName; final String commandName;
if (isCommand(firstArgument)) { if (isCommand(firstArgument)) {
commandName = firstArgument; commandName = firstArgument;
arguments.remove(0); arguments.remove(0);
} else if (isCommand(mPreviousCommand)) {
commandName = mPreviousCommand;
} else { } else {
throw new RuntimeException("Unknown command : " + firstArgument); throw new RuntimeException("Unknown command : " + firstArgument);
} }
final Command command = getCommandInstance(commandName); final Command command = getCommandInstance(commandName);
final int arity = command.getArity(); final String[] argsArray = arguments.toArray(new String[arguments.size()]);
if (arguments.size() < arity) {
throw new RuntimeException("Not enough arguments to command " + commandName);
}
final String[] argsArray = new String[arity];
arguments.subList(0, arity).toArray(argsArray);
for (int i = 0; i < arity; ++i) {
// For some reason, ArrayList#removeRange is protected
arguments.remove(0);
}
command.setArgs(argsArray); command.setArgs(argsArray);
mPreviousCommand = commandName;
return command; return command;
} }
private void execute(final ArrayList<String> arguments) { private void execute(final ArrayList<String> arguments) {
ArrayList<Command> commandsToExecute = new ArrayList<Command>(); final Command command = getCommand(arguments);
while (!arguments.isEmpty()) { try {
commandsToExecute.add(getNextCommand(arguments)); command.run();
} } catch (Exception e) {
for (final Command command : commandsToExecute) { System.out.println("Exception while processing command "
try { + command.getClass().getSimpleName() + " : " + e);
command.run(); return;
} catch (Exception e) {
System.out.println("Exception while processing command "
+ command.getClass().getSimpleName() + " : " + e);
return;
}
} }
} }

View File

@ -17,6 +17,8 @@
package com.android.inputmethod.latin.dicttool; package com.android.inputmethod.latin.dicttool;
public class Info extends Dicttool.Command { public class Info extends Dicttool.Command {
public static final String COMMAND = "info";
public Info() { public Info() {
} }
@ -24,12 +26,11 @@ public class Info extends Dicttool.Command {
return "info <filename>: prints various information about a dictionary file"; return "info <filename>: prints various information about a dictionary file";
} }
public int getArity() {
return 1;
}
public void run() { public void run() {
// TODO: implement this // TODO: implement this
if (mArgs.length < 1) {
throw new RuntimeException("Not enough arguments for command " + COMMAND);
}
System.out.println("Not implemented yet"); System.out.println("Not implemented yet");
} }
} }