CommandTree { public String[] joinArgsForKnownCommands(String[] args) { CommandTree tree = this; CommandTree lastTree = null; CommandKey key = null; int commandArgLength = 0; for (int i = 0; i < args.length; i++) { String lowerName = args[i].toLowerCase(); key = tree.keyMap.get(lowerName); tree = tree.treeMap.get(lowerName); if (tree == null) { break; } commandArgLength++; lastTree = tree; } String name = null; if (key != null) { if (tree == null || tree.name == null || !tree.name.equals(key.getName())) { commandArgLength++; } name = key.getName(); } else if (lastTree != null) { name = lastTree.name; } if (name != null) { String[] newArgs = new String[args.length - (commandArgLength - 1)]; newArgs[0] = name; if (args.length > 1) { System.arraycopy(args, commandArgLength, newArgs, 1, args.length - commandArgLength); } return newArgs; } else { return args; } } CommandTree(); private  CommandTree(@NotNull CommandTree parent, @NotNull String name); void registerKeysForAlias(@NotNull String alias); String[] joinArgsForKnownCommands(String[] args); CommandTree getTreeAt(String path); @Override String toString(); Set<String> getSubCommandSet();  }
@Test public void test2ArgCommandWith2Args() throws Exception { String[] args = commandTree.joinArgsForKnownCommands(new String[] {"two", "args"}); assertArrayEquals(new String[] {"two args"}, args); }
@Test public void test2ArgCommandWith3Args() throws Exception { String[] args = commandTree.joinArgsForKnownCommands(new String[] {"two", "args", "one"}); assertArrayEquals(new String[] {"two args", "one"}, args); }
@Test public void test2ArgCommandWith4Args() throws Exception { String[] args = commandTree.joinArgsForKnownCommands(new String[] {"two", "args", "one", "two"}); assertArrayEquals(new String[] {"two args", "one", "two"}, args); }
@Test public void test1ArgParentCommandWith1Args() throws Exception { String[] args = commandTree.joinArgsForKnownCommands(new String[] {"parent"}); assertArrayEquals(new String[] {"parent"}, args); }
@Test public void test1ArgParentCommandWith2Args() throws Exception { String[] args = commandTree.joinArgsForKnownCommands(new String[] {"parent", "one"}); assertArrayEquals(new String[]{"parent", "one"}, args); }
@Test public void test1ArgParentCommandWith3Args() throws Exception { String[] args = commandTree.joinArgsForKnownCommands(new String[] {"parent", "one", "two"}); assertArrayEquals(new String[] {"parent", "one", "two"}, args); }
@Test public void test2ArgChildCommandWith2Args() throws Exception { String[] args = commandTree.joinArgsForKnownCommands(new String[] {"parent", "child"}); assertArrayEquals(new String[] {"parent child"}, args); }
@Test public void test2ArgChildCommandWith3Args() throws Exception { String[] args = commandTree.joinArgsForKnownCommands(new String[] {"parent", "child", "one"}); assertArrayEquals(new String[] {"parent child", "one"}, args); }
@Test public void test2ArgChildCommandWith4Args() throws Exception { String[] args = commandTree.joinArgsForKnownCommands(new String[] {"parent", "child", "one", "two"}); assertArrayEquals(new String[] {"parent child", "one", "two"}, args); }
@Test public void test3ArgChildCommandWith3Args() throws Exception { String[] args = commandTree.joinArgsForKnownCommands(new String[] {"parent", "child", "baby"}); assertArrayEquals(new String[] {"parent child baby"}, args); }
@Test public void test3ArgChildCommandWith4Args() throws Exception { String[] args = commandTree.joinArgsForKnownCommands(new String[] {"parent", "child", "baby", "one"}); assertArrayEquals(new String[] {"parent child baby", "one"}, args); }
@Test public void test3ArgChildCommandWith5Args() throws Exception { String[] args = commandTree.joinArgsForKnownCommands(new String[] {"parent", "child", "baby", "one", "two"}); assertArrayEquals(new String[] {"parent child baby", "one", "two"}, args); }
@Test public void test2ArgChildUnusedParentCommandWith1Arg() throws Exception { String[] args = commandTree.joinArgsForKnownCommands(new String[] {"unused"}); assertArrayEquals(new String[] {"unused"}, args); }
@Test public void test2ArgChildUnusedParentCommandWith2ArgFirst() throws Exception { String[] args = commandTree.joinArgsForKnownCommands(new String[] {"unused", "first"}); assertArrayEquals(new String[] {"unused first"}, args); }
@Test public void test2ArgChildUnusedParentCommandWith2ArgSecond() throws Exception { String[] args = commandTree.joinArgsForKnownCommands(new String[] {"unused", "second"}); assertArrayEquals(new String[] {"unused second"}, args); }
@Test public void test1ArgCommandWith1Arg() throws Exception { String[] args = commandTree.joinArgsForKnownCommands(new String[] {"test"}); assertArrayEquals(new String[] {"test"}, args); }
@Test public void test1ArgCommandWith2Args() throws Exception { String[] args = commandTree.joinArgsForKnownCommands(new String[] {"test", "one"}); assertArrayEquals(new String[] {"test", "one"}, args); }
@Test public void test1ArgCommandWith3Args() throws Exception { String[] args = commandTree.joinArgsForKnownCommands(new String[] {"test", "one", "two"}); assertArrayEquals(new String[] {"test", "one", "two"}, args); }