Find extends FsCommand { @Override protected void processOptions(LinkedList<String> args) throws IOException { CommandFormat cf = new CommandFormat(1, Integer.MAX_VALUE, OPTION_FOLLOW_LINK, OPTION_FOLLOW_ARG_LINK, null); cf.parse(args); if (cf.getOpt(OPTION_FOLLOW_LINK)) { getOptions().setFollowLink(true); } else if (cf.getOpt(OPTION_FOLLOW_ARG_LINK)) { getOptions().setFollowArgLink(true); } LinkedList<String> expressionArgs = new LinkedList<String>(); Iterator<String> it = args.iterator(); boolean isPath = true; while (it.hasNext()) { String arg = it.next(); if (isPath) { if (arg.startsWith("-")) { isPath = false; } } if (!isPath) { expressionArgs.add(arg); it.remove(); } } if (args.isEmpty()) { args.add(Path.CUR_DIR); } Expression expression = parseExpression(expressionArgs); if (!expression.isAction()) { Expression and = getExpression(And.class); Deque<Expression> children = new LinkedList<Expression>(); children.add(getExpression(Print.class)); children.add(expression); and.addChildren(children); expression = and; } setRootExpression(expression); } Find(); static void registerCommands(CommandFactory factory); static final String NAME; static final String USAGE; static final String DESCRIPTION; }
@Test public void processOptionsExpression() throws IOException { Find find = new Find(); find.setConf(conf); String paths = "path1 path2 path3"; String args = "-L -H " + paths + " -print -name test"; LinkedList<String> argsList = getArgs(args); find.processOptions(argsList); LinkedList<String> pathList = getArgs(paths); assertEquals(pathList, argsList); }
@Test public void processOptionsUnknown() throws IOException { Find find = new Find(); find.setConf(conf); String args = "path -unknown"; try { find.processOptions(getArgs(args)); fail("Unknown expression not caught"); } catch (IOException e) { } }
@Test public void processOptionsKnownUnknown() throws IOException { Find find = new Find(); find.setConf(conf); String args = "path -print -unknown -print"; try { find.processOptions(getArgs(args)); fail("Unknown expression not caught"); } catch (IOException e) { } }
@Test public void processOptionsNoPath() throws IOException { Find find = new Find(); find.setConf(conf); String args = "-print"; LinkedList<String> argsList = getArgs(args); find.processOptions(argsList); assertEquals(Collections.singletonList(Path.CUR_DIR), argsList); }