Simplification (A2)

A single command at a time and no arity gives opportunity to simplify
the argument parsing code.

Change-Id: Iff7bab78ede7448a662092f221c590a9eedae2a5
This commit is contained in:
Jean Chalard 2012-08-03 17:24:18 +09:00
parent 77c8c73837
commit 82cd20ee98

View file

@ -16,7 +16,6 @@
package com.android.inputmethod.latin.dicttool; package com.android.inputmethod.latin.dicttool;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.HashMap; import java.util.HashMap;
@ -61,22 +60,18 @@ public class Dicttool {
return sCommands.containsKey(commandName); return sCommands.containsKey(commandName);
} }
private Command getCommand(final ArrayList<String> arguments) { private Command getCommand(final String[] arguments) {
final String firstArgument = arguments.get(0); final String commandName = arguments[0];
final String commandName; if (!isCommand(commandName)) {
if (isCommand(firstArgument)) { throw new RuntimeException("Unknown command : " + commandName);
commandName = firstArgument;
arguments.remove(0);
} else {
throw new RuntimeException("Unknown command : " + firstArgument);
} }
final Command command = getCommandInstance(commandName); final Command command = getCommandInstance(commandName);
final String[] argsArray = arguments.toArray(new String[arguments.size()]); final String[] argsArray = Arrays.copyOfRange(arguments, 1, arguments.length);
command.setArgs(argsArray); command.setArgs(argsArray);
return command; return command;
} }
private void execute(final ArrayList<String> arguments) { private void execute(final String[] arguments) {
final Command command = getCommand(arguments); final Command command = getCommand(arguments);
try { try {
command.run(); command.run();
@ -87,15 +82,11 @@ public class Dicttool {
} }
} }
public static void main(final String[] args) { public static void main(final String[] arguments) {
if (0 == args.length) { if (0 == arguments.length) {
help(); help();
return; return;
} }
if (!isCommand(args[0])) throw new RuntimeException("Unknown command : " + args[0]);
final ArrayList<String> arguments = new ArrayList<String>(args.length);
arguments.addAll(Arrays.asList(args));
new Dicttool().execute(arguments); new Dicttool().execute(arguments);
} }
} }