898b391787d069ba8c43b7eb426d05b4f2e60133
[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.Parser;
15 import eu.svjatoslav.commons.commandline.parameterparser.parameter.DirectoryParameter;
16 import eu.svjatoslav.commons.commandline.parameterparser.parameter.NullParameter;
17 import eu.svjatoslav.commons.commandline.parameterparser.parameter.StringParameter;
18
19 public class CommandlineHandler {
20
21         Parser parser = new Parser();
22
23         NullParameter recursiveParameter = parser.createNullParameter(
24                         "Enable recursive mode.").addAliases("-r", "--recursive");
25
26         StringParameter searchForPattern = parser
27                         .createStringParameter("String to search for")
28                         .addAliases("-s", "--search-pattern").setMandatory();
29
30         StringParameter replaceWithPattern = parser.createStringParameter(
31                         "String to place instead").addAliases("-p", "--replace-pattern");
32
33         DirectoryParameter directoryParameter = parser
34                         .createDirectoryParameter("Working directory.")
35                         .addAliases("-w", "--working-directory").mustExist();
36
37         public CommandlineOptions parseCommandlineArguments(final String[] args) {
38
39                 final CommandlineOptions options = new CommandlineOptions();
40
41                 if (!parser.parse(args))
42                         return null;
43
44                 if (recursiveParameter.isParameterSpecified())
45                         options.recursive = true;
46
47                 if (directoryParameter.isParameterSpecified())
48                         options.targetDirectory = directoryParameter.getValue();
49                 else
50                         options.targetDirectory = new File(System.getProperty("user.dir"));
51
52                 if (searchForPattern.isParameterSpecified())
53                         options.searchForPattern = searchForPattern.getValue();
54
55                 if (replaceWithPattern.isParameterSpecified())
56                         options.replaceWithPattern = replaceWithPattern.getValue();
57
58                 return options;
59         }
60 }