84bff070ea47aac003dd692daaa2de711450a1a6
[meviz.git] / src / main / java / eu / svjatoslav / meviz / htmlindexer / 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.htmlindexer;
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.StringArgument;
17 import eu.svjatoslav.meviz.encoder.EncodingOptions;
18
19 public class CommandlineHandler {
20
21         Parameter galleryNameParameter = new Parameter(false, true, false,
22                         new StringArgument(), "Gallery title. (default is: "
23                                         + Constants.DEFAULT_GALLERY_TITLE + ").", "-t",
24                         "--gallery-title");
25
26         Parameter workingDirectoryParameter = new Parameter(false, true, true,
27                         new StringArgument(), "Working directory.", "-w",
28                         "--working-directory");
29
30         public Parser initParser() {
31
32                 final Parser parser = new Parser();
33                 parser.addParameter(galleryNameParameter);
34                 parser.addParameter(workingDirectoryParameter);
35
36                 return parser;
37         }
38
39         /**
40          * @return {@link EncodingOptions} if commandline arguments were
41          *         successfully parsed, or <code>null</code> if parsing error
42          *         occurred.
43          */
44         public IndexingOptions parseCommandlineArguments(final String[] args) {
45
46                 final IndexingOptions options = new IndexingOptions();
47
48                 final Parser parser = initParser();
49                 if (!parser.parse(args))
50                         return null;
51
52                 if (galleryNameParameter.isParameterSpecified())
53                         options.galleryTitle = galleryNameParameter.getArgumentsAsStrings()
54                                         .get(0);
55
56                 if (workingDirectoryParameter.isParameterSpecified())
57                         options.workingDirectory = workingDirectoryParameter
58                                         .getArgumentsAsFiles().get(0);
59                 else
60                         options.workingDirectory = new File(System.getProperty("user.dir"));
61
62                 return options;
63         }
64 }