added possibility to use multiple patterns from command-line
[meviz.git] / src / main / java / eu / svjatoslav / meviz / renamer / 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.renamer;
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 import eu.svjatoslav.commons.commandline.parameterparser.parameter.StringParameters;
19 import eu.svjatoslav.meviz.encoder.EncodingOptions;
20
21 public class CommandlineHandler {
22
23         Parser parser = new Parser();
24
25         NullParameter recursiveParameter = parser.createNullParameter(
26                         "Enable recursive mode.").addAliases("-r", "--recursive");
27
28         NullParameter testParameter = parser.createNullParameter(
29                         "Simulate renaming (no changes will be actually done).")
30                         .addAliases("-t", "--test");
31
32         StringParameter outputPatternParameter = parser.createStringParameter(
33                         "Output pattern.").addAliases("-o", "--output-pattern");
34
35         StringParameters inputPatternParameter = parser.createStringParameters(
36                         "File input pattern.").addAliases("-i", "--input-pattern");
37
38         DirectoryParameter workingDirectoryParameter = parser
39                         .createDirectoryParameter("Working directory.")
40                         .addAliases("-w", "--working-directory").mustExist();
41
42         /**
43          * @return {@link EncodingOptions} if commandline arguments were
44          *         successfully parsed, or <code>null</code> if parsing error
45          *         occurred.
46          */
47         public RenamingOptions parseCommandlineArguments(final String[] args) {
48
49                 final RenamingOptions options = new RenamingOptions();
50
51                 if (!parser.parse(args))
52                         return null;
53
54                 if (testParameter.isParameterSpecified())
55                         options.testOnly = true;
56
57                 if (recursiveParameter.isParameterSpecified())
58                         options.recursive = true;
59
60                 if (outputPatternParameter.isParameterSpecified())
61                         options.outputPattern = outputPatternParameter.getValue();
62
63                 if (workingDirectoryParameter.isParameterSpecified())
64                         options.targetDirectory = workingDirectoryParameter.getValue();
65                 else
66                         options.targetDirectory = new File(System.getProperty("user.dir"));
67
68                 // TODO: add multiple input patterns support
69                 if (inputPatternParameter.isParameterSpecified())
70                         options.inputPatterns.addAll(inputPatternParameter.getValue());
71
72                 return options;
73         }
74 }