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