more elegant commandline handling
[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.StringParameters;
16
17 public class CommandlineHandler {
18
19         Parser parser = new Parser();
20
21         StringParameters outputFormatParameter = parser
22                         .add(new StringParameters("Encoding output format."))
23                         .addAliases("-o", "--output-format").setMandatory();
24
25         NullParameter deinterlaceParameter = parser.add(
26                         new NullParameter("Deinterlace video.")).addAliases(
27                         "--deinterlace", "-d");
28
29         NullParameter testParameter = parser.add(
30                         new NullParameter("Simulate file encoding.")).addAliases("-t",
31                                         "--test");
32
33         NullParameter recursiveParameter = parser.add(
34                         new NullParameter("Enable recursive mode.")).addAliases("-r",
35                         "--recursive");
36
37         NullParameter forPortableParameter = parser.add(
38                         new NullParameter("Encode for portable player.")).addAliases("-p",
39                                         "--portable");
40
41         StringParameters inputPatternParameter = parser
42                         .add(new StringParameters("File input pattern."))
43                         .addAliases("-i", "--input-pattern").setMandatory();
44
45         DirectoryParameter workingDirectoryParameter = parser
46                         .add(new DirectoryParameter("Working directory."))
47                         .addAliases("-w", "--working-directory").mustExist();
48
49         BitrateParameter videoBitrateParameter = parser.add(
50                         new BitrateParameter("Video bitrate")).addAliases("-v",
51                         "--video-bitrate");
52
53         BitrateParameter audioBitrateParameter = parser.add(
54                         new BitrateParameter("Audio bitrate")).addAliases("-a",
55                                         "--audio-bitrate");
56
57         /**
58          * @return {@link EncodingOptions} if commandline arguments were
59          *         successfully parsed, or <code>null</code> if parsing error
60          *         occurred.
61          */
62         public EncodingOptions parseCommandlineArguments(final String[] args) {
63
64                 final EncodingOptions options = new EncodingOptions();
65
66                 parser.parse(args);
67
68                 options.recursive = recursiveParameter.isParameterSpecified();
69
70                 options.deinterlace = deinterlaceParameter.isParameterSpecified();
71
72                 options.testOnly = testParameter.isParameterSpecified();
73
74                 if (outputFormatParameter.isParameterSpecified())
75                         options.outputFormats.addAll(outputFormatParameter.getValue());
76
77                 if (workingDirectoryParameter.isParameterSpecified())
78                         options.workingDirectory = workingDirectoryParameter.getValue();
79
80                 if (inputPatternParameter.isParameterSpecified())
81                         options.inputPatterns.addAll(inputPatternParameter.getValue());
82
83                 options.forPortablePlayer = forPortableParameter.isParameterSpecified();
84
85                 if (videoBitrateParameter.isParameterSpecified())
86                         try {
87                                 options.setVideoBitrate(videoBitrateParameter.getValue());
88                         } catch (final Exception e) {
89                                 System.out
90                                                 .println("Invalid video bitrate. Valid values are: LOW, MEDIUM, HIGH.");
91                                 return null;
92                         }
93
94                 return options;
95         }
96 }