005a828994fb35f769226f8917b42081eb73f63d
[meviz.git] / src / main / java / eu / svjatoslav / meviz / texttruncate / 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.texttruncate;
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 inputPatternParameter = parser.createStringParameter(
27                         "File input pattern.").addAliases("-i", "--input-pattern");
28
29         DirectoryParameter workingDirectoryParameter = parser
30                         .createDirectoryParameter("Working directory.").addAliases("-w",
31                                         "--working-directory");
32
33         public TextTruncatingOptions parseCommandlineArguments(final String[] args) {
34
35                 final TextTruncatingOptions options = new TextTruncatingOptions();
36
37                 if (!parser.parse(args))
38                         return null;
39
40                 if (recursiveParameter.isParameterSpecified())
41                         options.recursive = true;
42
43                 if (workingDirectoryParameter.isParameterSpecified())
44                         options.targetDirectory = workingDirectoryParameter
45                                         .getArgumentsAsFiles().get(0);
46                 else
47                         options.targetDirectory = new File(System.getProperty("user.dir"));
48
49                 if (inputPatternParameter.isParameterSpecified())
50                         options.inputPatterns.addAll(inputPatternParameter
51                                         .getArgumentsAsStrings());
52
53                 return options;
54         }
55 }