3f153c9f6d532ee71016041566b950e5a9b14a18
[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.add(
24                         new NullParameter("Enable recursive mode.")).addAliases("-r",
25                         "--recursive");
26
27         StringParameter searchForPattern = parser
28                         .add(new StringParameter("String to search for"))
29                         .addAliases("-s", "--search-pattern").setMandatory();
30
31         StringParameter replaceWithPattern = parser.add(
32                         new StringParameter("String to place instead")).addAliases("-p",
33                                         "--replace-pattern");
34
35         DirectoryParameter directoryParameter = parser
36                         .add(new DirectoryParameter("Working directory."))
37                         .addAliases("-w", "--working-directory").mustExist();
38
39         public CommandlineOptions parseCommandlineArguments(final String[] args) {
40
41                 final CommandlineOptions options = new CommandlineOptions();
42
43                 if (!parser.parse(args))
44                         return null;
45
46                 if (recursiveParameter.isParameterSpecified())
47                         options.recursive = true;
48
49                 if (directoryParameter.isParameterSpecified())
50                         options.targetDirectory = directoryParameter.getValue();
51                 else
52                         options.targetDirectory = new File(System.getProperty("user.dir"));
53
54                 if (searchForPattern.isParameterSpecified())
55                         options.searchForPattern = searchForPattern.getValue();
56
57                 if (replaceWithPattern.isParameterSpecified())
58                         options.replaceWithPattern = replaceWithPattern.getValue();
59
60                 return options;
61         }
62 }