ef2a27ae5b2b72119d180220e2cfa04a530163d5
[meviz.git] / src / main / java / eu / svjatoslav / meviz / textsplitter / 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.textsplitter;
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.StringParameters;
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         StringParameters fileInputPatternsParameter = parser
27                         .createStringParameters("File input pattern.")
28                         .addAliases("-i", "--input-pattern").setMandatory();
29
30         StringParameters splitPatternsParameter = parser
31                         .createStringParameters("File split regular expression.")
32                         .addAliases("-s", "--split-pattern").setMandatory();
33
34         DirectoryParameter workingDirectoryParameter = parser
35                         .createDirectoryParameter("Working directory.").addAliases("-w",
36                                         "--working-directory");
37
38         public TextSplittingOptions parseCommandlineArguments(final String[] args) {
39
40                 final TextSplittingOptions options = new TextSplittingOptions();
41
42                 if (!parser.parse(args))
43                         return null;
44
45                 if (recursiveParameter.isParameterSpecified())
46                         options.recursive = true;
47
48                 if (workingDirectoryParameter.isParameterSpecified())
49                         options.targetDirectory = workingDirectoryParameter.getValue();
50                 else
51                         options.targetDirectory = new File(System.getProperty("user.dir"));
52
53                 if (fileInputPatternsParameter.isParameterSpecified())
54                         options.fileInputPatterns.addAll(fileInputPatternsParameter
55                                         .getValue());
56
57                 if (splitPatternsParameter.isParameterSpecified())
58                         options.textSplitPatterns.addAll(splitPatternsParameter.getValue());
59
60                 return options;
61         }
62 }