d391bcac9f6fbdef6205d2ac127ab5b0414c8e48
[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.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 inputPatternParameter = new Parameter(true, true, true,
25                         new StringArgument(), "File input pattern.", "-i",
26                         "--input-pattern");
27
28         Parameter workingDirectoryParameter = new Parameter(false, true, true,
29                         new ExistingDirectory(), "Working directory.", "-w",
30                         "--working-directory");
31
32         public Parser initParser() {
33
34                 final Parser parser = new Parser();
35                 parser.addParameter(recursiveParameter);
36                 parser.addParameter(inputPatternParameter);
37                 parser.addParameter(workingDirectoryParameter);
38
39                 return parser;
40         }
41
42         public TextTruncatingOptions parseCommandlineArguments(final String[] args) {
43
44                 final TextTruncatingOptions options = new TextTruncatingOptions();
45
46                 final Parser parser = initParser();
47                 if (!parser.parse(args))
48                         return null;
49
50                 if (recursiveParameter.isParameterSpecified())
51                         options.recursive = true;
52
53                 if (workingDirectoryParameter.isParameterSpecified())
54                         options.targetDirectory = workingDirectoryParameter
55                                         .getArgumentsAsFiles().get(0);
56                 else
57                         options.targetDirectory = new File(System.getProperty("user.dir"));
58
59                 if (inputPatternParameter.isParameterSpecified())
60                         options.inputPatterns.addAll(inputPatternParameter
61                                         .getArgumentsAsStrings());
62
63                 return options;
64         }
65 }