051aaa82668763c646f1d8b8b499630a3baece62
[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.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 import eu.svjatoslav.meviz.encoder.EncodingOptions;
19
20 public class CommandlineHandler {
21
22         Parameter recursiveParameter = new Parameter("Enable recursive mode.",
23                         "-r", "--recursive");
24
25         Parameter testParameter = new Parameter(
26                         "Simulate renaming (no changes will be actually done).", "-t",
27                         "--test");
28
29         Parameter outputPatternParameter = new Parameter(true, true, true,
30                         new StringArgument(), "Output pattern.", "-o", "--output-pattern");
31
32         Parameter inputPatternParameter = new Parameter(true, true, true,
33                         new StringArgument(), "File input pattern.", "-i",
34                         "--input-pattern");
35
36         Parameter workingDirectoryParameter = new Parameter(false, true, true,
37                         new ExistingDirectory(), "Working directory.", "-w",
38                         "--working-directory");
39
40         public Parser initParser() {
41
42                 final Parser parser = new Parser();
43                 parser.addParameter(recursiveParameter);
44                 parser.addParameter(testParameter);
45                 parser.addParameter(outputPatternParameter);
46                 parser.addParameter(inputPatternParameter);
47                 parser.addParameter(workingDirectoryParameter);
48
49                 return parser;
50         }
51
52         /**
53          * @return {@link EncodingOptions} if commandline arguments were
54          *         successfully parsed, or <code>null</code> if parsing error
55          *         occurred.
56          */
57         public RenamingOptions parseCommandlineArguments(final String[] args) {
58
59                 final RenamingOptions options = new RenamingOptions();
60
61                 final Parser parser = initParser();
62                 if (!parser.parse(args))
63                         return null;
64
65                 if (testParameter.isParameterSpecified())
66                         options.testOnly = true;
67
68                 if (recursiveParameter.isParameterSpecified())
69                         options.recursive = true;
70
71                 if (outputPatternParameter.isParameterSpecified())
72                         options.outputPattern = outputPatternParameter
73                                         .getArgumentsAsStrings().get(0);
74
75                 if (workingDirectoryParameter.isParameterSpecified())
76                         options.targetDirectory = workingDirectoryParameter
77                                         .getArgumentsAsFiles().get(0);
78                 else
79                         options.targetDirectory = new File(System.getProperty("user.dir"));
80
81                 if (inputPatternParameter.isParameterSpecified())
82                         options.inputPatterns.addAll(inputPatternParameter
83                                         .getArgumentsAsStrings());
84
85                 return options;
86         }
87 }