2 * Meviz - Various tools collection to work with multimedia.
3 * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of version 2 of the GNU General Public License
7 * as published by the Free Software Foundation.
10 package eu.svjatoslav.meviz.replace;
14 import eu.svjatoslav.commons.commandline.parameterparser.Parameter;
15 import eu.svjatoslav.commons.commandline.parameterparser.Parser;
16 import eu.svjatoslav.commons.commandline.parameterparser.arguments.ExistingDirectory;
17 import eu.svjatoslav.commons.commandline.parameterparser.arguments.StringArgument;
19 public class CommandlineHandler {
21 Parameter recursiveParameter = new Parameter("Enable recursive mode.",
24 Parameter searchForPattern = new Parameter(true, true, false,
25 new StringArgument(), "String to search for", "-s",
28 Parameter replaceWithPattern = new Parameter(true, true, false,
29 new StringArgument(), "String to place instead", "-p",
32 Parameter workingDirectoryParameter = new Parameter(false, true, false,
33 new ExistingDirectory(), "Working directory.", "-w",
34 "--working-directory");
36 public Parser initParser() {
38 final Parser parser = new Parser();
39 parser.addParameter(recursiveParameter);
40 parser.addParameter(searchForPattern);
41 parser.addParameter(replaceWithPattern);
42 parser.addParameter(workingDirectoryParameter);
47 public CommandlineOptions parseCommandlineArguments(final String[] args) {
49 final CommandlineOptions options = new CommandlineOptions();
51 final Parser parser = initParser();
52 if (!parser.parse(args))
55 if (recursiveParameter.isParameterSpecified())
56 options.recursive = true;
58 if (workingDirectoryParameter.isParameterSpecified())
59 options.targetDirectory = workingDirectoryParameter
60 .getArgumentsAsFiles().get(0);
62 options.targetDirectory = new File(System.getProperty("user.dir"));
64 if (searchForPattern.isParameterSpecified())
65 options.searchForPattern = searchForPattern.getArgumentAsString();
67 if (replaceWithPattern.isParameterSpecified())
68 options.replaceWithPattern = replaceWithPattern
69 .getArgumentAsString();