updated commandline handler
[meviz.git] / src / main / java / eu / svjatoslav / meviz / encoder / 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.encoder;
11
12 import eu.svjatoslav.commons.commandline.parameterparser.Parser;
13 import eu.svjatoslav.commons.commandline.parameterparser.parameter.DirectoryParameter;
14 import eu.svjatoslav.commons.commandline.parameterparser.parameter.NullParameter;
15 import eu.svjatoslav.commons.commandline.parameterparser.parameter.StringParameter;
16
17 public class CommandlineHandler {
18
19         Parser parser = new Parser();
20
21         StringParameter outputFormatParameter = parser
22                         .createStringParameter("Encoding output format.")
23                         .addAliases("-o", "--output-format").setMandatory();
24
25         NullParameter terminalParameter = parser.createNullParameter(
26                         "Enable popup terminal.").addAliases("--terminal");
27
28         NullParameter deinterlaceParameter = parser.createNullParameter(
29                         "Deinterlace video.").addAliases("--deinterlace", "-d");
30
31         NullParameter testParameter = parser.createNullParameter(
32                         "Simulate file encoding.").addAliases("-t", "--test");
33
34         NullParameter recursiveParameter = parser.createNullParameter(
35                         "Enable recursive mode.").addAliases("-r", "--recursive");
36
37         StringParameter inputPatternParameter = parser
38                         .createStringParameter("File input pattern.")
39                         .addAliases("-i", "--input-pattern").setMandatory();
40
41         DirectoryParameter workingDirectoryParameter = parser
42                         .createDirectoryParameter("Working directory.")
43                         .addAliases("-w", "--working-directory").mustExist();
44
45         BitrateParameter videoBitrateParameter = new BitrateParameter(
46                         "Video bitrate");
47
48         public CommandlineHandler() {
49                 videoBitrateParameter.addAliases("-v", "--video-bitrate");
50                 parser.addParameter(videoBitrateParameter);
51         }
52
53         /**
54          * @return {@link EncodingOptions} if commandline arguments were
55          *         successfully parsed, or <code>null</code> if parsing error
56          *         occurred.
57          */
58         public EncodingOptions parseCommandlineArguments(final String[] args) {
59
60                 final EncodingOptions options = new EncodingOptions();
61
62                 parser.parse(args);
63
64                 if (recursiveParameter.isParameterSpecified())
65                         options.recursive = true;
66
67                 if (deinterlaceParameter.isParameterSpecified())
68                         options.deinterlace = true;
69
70                 if (terminalParameter.isParameterSpecified())
71                         options.terminal = true;
72
73                 if (testParameter.isParameterSpecified())
74                         options.testOnly = true;
75
76                 if (outputFormatParameter.isParameterSpecified())
77                         options.outputFormats.addAll(outputFormatParameter
78                                         .getArgumentsAsStrings());
79
80                 if (workingDirectoryParameter.isParameterSpecified())
81                         options.workingDirectory = workingDirectoryParameter
82                                         .getArgumentsAsFiles().get(0);
83
84                 if (inputPatternParameter.isParameterSpecified())
85                         options.inputPatterns.addAll(inputPatternParameter
86                                         .getArgumentsAsStrings());
87
88                 if (videoBitrateParameter.isParameterSpecified())
89                         try {
90                                 options.videoBitrate = videoBitrateParameter.getValue();
91                         } catch (final Exception e) {
92                                 System.out
93                                                 .println("Invalid video bitrate. Valid values are: LOW, MEDIUM, HIGH.");
94                                 return null;
95                         }
96
97                 return options;
98         }
99 }