2 * Meviz - Various tools collection to work with multimedia.
3 * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
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.
10 package eu.svjatoslav.meviz.renamer;
14 import eu.svjatoslav.commons.commandline.parameterparser.Parameter;
15 import eu.svjatoslav.commons.commandline.parameterparser.Parser;
16 import eu.svjatoslav.commons.commandline.parameterparser.arguments.ExistingDirectory;
17 import eu.svjatoslav.commons.commandline.parameterparser.arguments.StringArgument;
18 import eu.svjatoslav.meviz.encoder.EncodingOptions;
20 public class CommandlineHandler {
22 Parameter recursiveParameter = new Parameter("Enable recursive mode.",
25 Parameter testParameter = new Parameter(
26 "Simulate renaming (no changes will be actually done).", "-t",
29 Parameter outputPatternParameter = new Parameter(true, true, true,
30 new StringArgument(), "Output pattern.", "-o", "--output-pattern");
32 Parameter inputPatternParameter = new Parameter(true, true, true,
33 new StringArgument(), "File input pattern.", "-i",
36 Parameter workingDirectoryParameter = new Parameter(false, true, true,
37 new ExistingDirectory(), "Working directory.", "-w",
38 "--working-directory");
40 public Parser initParser() {
42 final Parser parser = new Parser();
43 parser.addParameter(recursiveParameter);
44 parser.addParameter(testParameter);
45 parser.addParameter(outputPatternParameter);
46 parser.addParameter(inputPatternParameter);
47 parser.addParameter(workingDirectoryParameter);
53 * @return {@link EncodingOptions} if commandline arguments were
54 * successfully parsed, or <code>null</code> if parsing error
57 public RenamingOptions parseCommandlineArguments(final String[] args) {
59 final RenamingOptions options = new RenamingOptions();
61 final Parser parser = initParser();
62 if (!parser.parse(args))
65 if (testParameter.isParameterSpecified())
66 options.testOnly = true;
68 if (recursiveParameter.isParameterSpecified())
69 options.recursive = true;
71 if (outputPatternParameter.isParameterSpecified())
72 options.outputPattern = outputPatternParameter
73 .getArgumentsAsStrings().get(0);
75 if (workingDirectoryParameter.isParameterSpecified())
76 options.targetDirectory = workingDirectoryParameter
77 .getArgumentsAsFiles().get(0);
79 options.targetDirectory = new File(System.getProperty("user.dir"));
81 if (inputPatternParameter.isParameterSpecified())
82 options.inputPatterns.addAll(inputPatternParameter
83 .getArgumentsAsStrings());