+/*
+ * Meviz - Various tools collection to work with multimedia.
+ * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of version 2 of the GNU General Public License
+ * as published by the Free Software Foundation.
+ */
+
+package eu.svjatoslav.meviz.replace;
+
+import java.io.File;
+
+import eu.svjatoslav.commons.commandline.parameterparser.Parameter;
+import eu.svjatoslav.commons.commandline.parameterparser.Parser;
+import eu.svjatoslav.commons.commandline.parameterparser.arguments.ExistingDirectory;
+import eu.svjatoslav.commons.commandline.parameterparser.arguments.StringArgument;
+
+public class CommandlineHandler {
+
+ Parameter recursiveParameter = new Parameter("Enable recursive mode.",
+ "-r", "--recursive");
+
+ Parameter searchForPattern = new Parameter(true, true, false,
+ new StringArgument(), "String to search for", "-s",
+ "--search-pattern");
+
+ Parameter replaceWithPattern = new Parameter(true, true, false,
+ new StringArgument(), "String to place instead", "-p",
+ "--replace-pattern");
+
+ Parameter workingDirectoryParameter = new Parameter(false, true, false,
+ new ExistingDirectory(), "Working directory.", "-w",
+ "--working-directory");
+
+ public Parser initParser() {
+
+ final Parser parser = new Parser();
+ parser.addParameter(recursiveParameter);
+ parser.addParameter(searchForPattern);
+ parser.addParameter(replaceWithPattern);
+ parser.addParameter(workingDirectoryParameter);
+
+ return parser;
+ }
+
+ public CommandlineOptions parseCommandlineArguments(final String[] args) {
+
+ final CommandlineOptions options = new CommandlineOptions();
+
+ final Parser parser = initParser();
+ if (!parser.parse(args))
+ return null;
+
+ if (recursiveParameter.isParameterSpecified())
+ options.recursive = true;
+
+ if (workingDirectoryParameter.isParameterSpecified())
+ options.targetDirectory = workingDirectoryParameter
+ .getArgumentsAsFiles().get(0);
+ else
+ options.targetDirectory = new File(System.getProperty("user.dir"));
+
+ if (searchForPattern.isParameterSpecified())
+ options.searchForPattern = searchForPattern.getArgumentAsString();
+
+ if (replaceWithPattern.isParameterSpecified())
+ options.replaceWithPattern = replaceWithPattern
+ .getArgumentAsString();
+
+ return options;
+ }
+}