4a8f3369aad67f04ab3c5320ada66f24a03a5dbc
[meviz.git] / src / main / java / eu / svjatoslav / meviz / replace / CommandlineHandler.java
1 /*
2  * Meviz - Various tools collection to work with multimedia.
3  * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
4  * 
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.
8  */
9
10 package eu.svjatoslav.meviz.replace;
11
12 import java.io.File;
13
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;
18
19 public class CommandlineHandler {
20
21         Parameter recursiveParameter = new Parameter("Enable recursive mode.",
22                         "-r", "--recursive");
23
24         Parameter searchForPattern = new Parameter(true, true, false,
25                         new StringArgument(), "String to search for", "-s",
26                         "--search-pattern");
27
28         Parameter replaceWithPattern = new Parameter(true, true, false,
29                         new StringArgument(), "String to place instead", "-p",
30                         "--replace-pattern");
31
32         Parameter workingDirectoryParameter = new Parameter(false, true, false,
33                         new ExistingDirectory(), "Working directory.", "-w",
34                         "--working-directory");
35
36         public Parser initParser() {
37
38                 final Parser parser = new Parser();
39                 parser.addParameter(recursiveParameter);
40                 parser.addParameter(searchForPattern);
41                 parser.addParameter(replaceWithPattern);
42                 parser.addParameter(workingDirectoryParameter);
43
44                 return parser;
45         }
46
47         public CommandlineOptions parseCommandlineArguments(final String[] args) {
48
49                 final CommandlineOptions options = new CommandlineOptions();
50
51                 final Parser parser = initParser();
52                 if (!parser.parse(args))
53                         return null;
54
55                 if (recursiveParameter.isParameterSpecified())
56                         options.recursive = true;
57
58                 if (workingDirectoryParameter.isParameterSpecified())
59                         options.targetDirectory = workingDirectoryParameter
60                         .getArgumentsAsFiles().get(0);
61                 else
62                         options.targetDirectory = new File(System.getProperty("user.dir"));
63
64                 if (searchForPattern.isParameterSpecified())
65                         options.searchForPattern = searchForPattern.getArgumentAsString();
66
67                 if (replaceWithPattern.isParameterSpecified())
68                         options.replaceWithPattern = replaceWithPattern
69                         .getArgumentAsString();
70
71                 return options;
72         }
73 }