5f27ee068821fa61db8e506115c226b8f92e57d6
[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.Parser;
15 import eu.svjatoslav.commons.commandline.parameterparser.parameter.DirectoryParameter;
16 import eu.svjatoslav.commons.commandline.parameterparser.parameter.NullParameter;
17 import eu.svjatoslav.commons.commandline.parameterparser.parameter.StringParameter;
18 import eu.svjatoslav.meviz.encoder.EncodingOptions;
19
20 public class CommandlineHandler {
21
22         Parser parser = new Parser();
23
24         StringParameter galleryNameParameter = parser.add(
25                         new StringParameter("Gallery title. (default is: "
26                                         + Constants.DEFAULT_GALLERY_TITLE + ").")).addAliases("-t",
27                         "--gallery-title");
28
29         DirectoryParameter workingDirectoryParameter = parser
30                         .add(new DirectoryParameter("Working directory."))
31                         .addAliases("-w", "--working-directory").mustExist();
32
33         NullParameter removeIndex = parser.add(
34                         new NullParameter("Remove generated index")).addAliases("-r",
35                         "--remove");
36
37         public String getGalleryTitle() {
38                 if (galleryNameParameter.isParameterSpecified())
39                         return galleryNameParameter.getValue();
40                 return Constants.DEFAULT_GALLERY_TITLE;
41         }
42
43         public File getWorkingDirectory() {
44                 if (workingDirectoryParameter.isParameterSpecified())
45                         return workingDirectoryParameter.getValue();
46                 else
47                         return new File(System.getProperty("user.dir"));
48         }
49
50         /**
51          * @return {@link EncodingOptions} if commandline arguments were
52          *         successfully parsed, or <code>null</code> if parsing error
53          *         occurred.
54          */
55         public boolean parseCommandlineArguments(final String[] args) {
56                 final boolean parsingSucceeded = parser.parse(args);
57
58                 if (!parsingSucceeded)
59                         parser.showHelp();
60
61                 return parsingSucceeded;
62         }
63 }