added possibility to use multiple patterns from command-line
[meviz.git] / src / main / java / eu / svjatoslav / meviz / bomremove / 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.bomremove;
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 inputPatternParameter = parser.createStringParameters(
27                         "File input pattern.").addAliases("-i", "--input-pattern");
28
29         DirectoryParameter workingDirectoryParameter = parser
30                         .createDirectoryParameter("Working directory.")
31                         .addAliases("-w", "--working-directory").mustExist();
32
33         public Parser initParser() {
34
35                 final Parser parser = new Parser();
36                 parser.addParameter(recursiveParameter);
37                 parser.addParameter(inputPatternParameter);
38                 parser.addParameter(workingDirectoryParameter);
39
40                 return parser;
41         }
42
43         public BomStrippingOptions parseCommandlineArguments(final String[] args) {
44
45                 final BomStrippingOptions options = new BomStrippingOptions();
46
47                 final Parser parser = initParser();
48                 if (!parser.parse(args))
49                         return null;
50
51                 if (recursiveParameter.isParameterSpecified())
52                         options.recursive = true;
53
54                 if (workingDirectoryParameter.isParameterSpecified())
55                         options.targetDirectory = workingDirectoryParameter.getValue();
56                 else
57                         options.targetDirectory = new File(System.getProperty("user.dir"));
58
59                 // TODO: add support for multiple input patterns
60                 if (inputPatternParameter.isParameterSpecified())
61                         options.inputPatterns.addAll(inputPatternParameter.getValue());
62
63                 return options;
64         }
65 }