more elegant commandline handling
authorSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Fri, 19 Dec 2014 20:54:10 +0000 (22:54 +0200)
committerSvjatoslav Agejenko <svjatoslav@svjatoslav.eu>
Fri, 19 Dec 2014 20:54:10 +0000 (22:54 +0200)
12 files changed:
src/main/java/eu/svjatoslav/meviz/bomremove/CommandlineHandler.java
src/main/java/eu/svjatoslav/meviz/bomremove/Main.java
src/main/java/eu/svjatoslav/meviz/encoder/BitrateParameter.java
src/main/java/eu/svjatoslav/meviz/encoder/CommandlineHandler.java
src/main/java/eu/svjatoslav/meviz/encoder/EncodingOptions.java
src/main/java/eu/svjatoslav/meviz/encoder/converters/AvconvVideo.java
src/main/java/eu/svjatoslav/meviz/grabmemcard/Main.java
src/main/java/eu/svjatoslav/meviz/htmlindexer/CommandlineHandler.java
src/main/java/eu/svjatoslav/meviz/renamer/CommandlineHandler.java
src/main/java/eu/svjatoslav/meviz/replace/CommandlineHandler.java
src/main/java/eu/svjatoslav/meviz/textsplitter/CommandlineHandler.java
src/main/java/eu/svjatoslav/meviz/texttruncate/CommandlineHandler.java

index 1bc4f3c..fdfc3c9 100755 (executable)
@@ -20,31 +20,22 @@ public class CommandlineHandler {
 
        Parser parser = new Parser();
 
-       NullParameter recursiveParameter = parser.createNullParameter(
-                       "Enable recursive mode.").addAliases("-r", "--recursive");
+       NullParameter recursiveParameter = parser.add(
+                       new NullParameter("Enable recursive mode.")).addAliases("-r",
+                       "--recursive");
 
-       StringParameters inputPatternParameter = parser.createStringParameters(
-                       "File input pattern.").addAliases("-i", "--input-pattern");
+       StringParameters inputPatternParameter = parser.add(
+                       new StringParameters("File input pattern.")).addAliases("-i",
+                                       "--input-pattern");
 
        DirectoryParameter workingDirectoryParameter = parser
-                       .createDirectoryParameter("Working directory.")
+                       .add(new DirectoryParameter("Working directory."))
                        .addAliases("-w", "--working-directory").mustExist();
 
-       public Parser initParser() {
-
-               final Parser parser = new Parser();
-               parser.addParameter(recursiveParameter);
-               parser.addParameter(inputPatternParameter);
-               parser.addParameter(workingDirectoryParameter);
-
-               return parser;
-       }
-
        public BomStrippingOptions parseCommandlineArguments(final String[] args) {
 
                final BomStrippingOptions options = new BomStrippingOptions();
 
-               final Parser parser = initParser();
                if (!parser.parse(args))
                        return null;
 
index 9eed788..201c723 100755 (executable)
@@ -114,7 +114,7 @@ public class Main implements Module {
 
        @Override
        public void showCommandlineHelp() {
-               commandlineHandler.initParser().showHelp();
+               commandlineHandler.parser.showHelp();
        }
 
        public void stripFileFromHeader(final File file)
index 6e3ade7..18c3a2c 100755 (executable)
@@ -12,7 +12,7 @@ package eu.svjatoslav.meviz.encoder;
 import eu.svjatoslav.commons.commandline.parameterparser.ArgumentCount;
 import eu.svjatoslav.commons.commandline.parameterparser.Parameter;
 
-public class BitrateParameter extends Parameter {
+public class BitrateParameter extends Parameter<BitrateParameter> {
 
        static public enum bitrate {
                LOW, MEDIUM, HIGH, COPY
@@ -22,12 +22,6 @@ public class BitrateParameter extends Parameter {
                super(description, ArgumentCount.SINGLE);
        }
 
-       @Override
-       public BitrateParameter addAliases(final String... aliasArray) {
-               addAliasesProtected(aliasArray);
-               return this;
-       }
-
        @Override
        public String describeFormat() {
                return "Target bitrate [LOW / MEDIUM / HIGH / COPY]";
@@ -38,12 +32,6 @@ public class BitrateParameter extends Parameter {
                return bitrate.valueOf(arguments.get(0).toUpperCase());
        }
 
-       @Override
-       public BitrateParameter setMandatory() {
-               setMandatoryProtected();
-               return this;
-       }
-
        @Override
        public boolean validate(final String value) {
                try {
index 102ca7a..ec7d9cd 100755 (executable)
@@ -19,36 +19,40 @@ public class CommandlineHandler {
        Parser parser = new Parser();
 
        StringParameters outputFormatParameter = parser
-                       .createStringParameters("Encoding output format.")
+                       .add(new StringParameters("Encoding output format."))
                        .addAliases("-o", "--output-format").setMandatory();
 
-       NullParameter deinterlaceParameter = parser.createNullParameter(
-                       "Deinterlace video.").addAliases("--deinterlace", "-d");
+       NullParameter deinterlaceParameter = parser.add(
+                       new NullParameter("Deinterlace video.")).addAliases(
+                       "--deinterlace", "-d");
 
-       NullParameter testParameter = parser.createNullParameter(
-                       "Simulate file encoding.").addAliases("-t", "--test");
+       NullParameter testParameter = parser.add(
+                       new NullParameter("Simulate file encoding.")).addAliases("-t",
+                                       "--test");
 
-       NullParameter recursiveParameter = parser.createNullParameter(
-                       "Enable recursive mode.").addAliases("-r", "--recursive");
+       NullParameter recursiveParameter = parser.add(
+                       new NullParameter("Enable recursive mode.")).addAliases("-r",
+                       "--recursive");
 
-       NullParameter forPortableParameter = parser.createNullParameter(
-                       "Encode for portable player.").addAliases("-p", "--portable");
+       NullParameter forPortableParameter = parser.add(
+                       new NullParameter("Encode for portable player.")).addAliases("-p",
+                                       "--portable");
 
        StringParameters inputPatternParameter = parser
-                       .createStringParameters("File input pattern.")
+                       .add(new StringParameters("File input pattern."))
                        .addAliases("-i", "--input-pattern").setMandatory();
 
        DirectoryParameter workingDirectoryParameter = parser
-                       .createDirectoryParameter("Working directory.")
+                       .add(new DirectoryParameter("Working directory."))
                        .addAliases("-w", "--working-directory").mustExist();
 
-       BitrateParameter videoBitrateParameter = new BitrateParameter(
-                       "Video bitrate");
+       BitrateParameter videoBitrateParameter = parser.add(
+                       new BitrateParameter("Video bitrate")).addAliases("-v",
+                       "--video-bitrate");
 
-       public CommandlineHandler() {
-               videoBitrateParameter.addAliases("-v", "--video-bitrate");
-               parser.addParameter(videoBitrateParameter);
-       }
+       BitrateParameter audioBitrateParameter = parser.add(
+                       new BitrateParameter("Audio bitrate")).addAliases("-a",
+                                       "--audio-bitrate");
 
        /**
         * @return {@link EncodingOptions} if commandline arguments were
@@ -80,7 +84,7 @@ public class CommandlineHandler {
 
                if (videoBitrateParameter.isParameterSpecified())
                        try {
-                               options.videoBitrate = videoBitrateParameter.getValue();
+                               options.setVideoBitrate(videoBitrateParameter.getValue());
                        } catch (final Exception e) {
                                System.out
                                                .println("Invalid video bitrate. Valid values are: LOW, MEDIUM, HIGH.");
index 9e7f2b4..6c666a1 100755 (executable)
@@ -1,7 +1,7 @@
 /*
  * Meviz - Various tools collection to work with multimedia.
  * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- * 
+ *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of version 2 of the GNU General Public License
  * as published by the Free Software Foundation.
@@ -15,7 +15,9 @@ import java.util.List;
 
 public class EncodingOptions {
 
-       public BitrateParameter.bitrate videoBitrate = BitrateParameter.bitrate.MEDIUM;
+       private BitrateParameter.bitrate videoBitrate = BitrateParameter.bitrate.MEDIUM;
+
+       private BitrateParameter.bitrate audioBitrate = BitrateParameter.bitrate.MEDIUM;
 
        // public Bitrate audioBitrate;
 
@@ -33,4 +35,20 @@ public class EncodingOptions {
 
        List<String> inputPatterns = new ArrayList<String>();
 
+       public BitrateParameter.bitrate getAudioBitrate() {
+               return audioBitrate;
+       }
+
+       public BitrateParameter.bitrate getVideoBitrate() {
+               return videoBitrate;
+       }
+
+       public void setAudioBitrate(final BitrateParameter.bitrate audioBitrate) {
+               this.audioBitrate = audioBitrate;
+       }
+
+       public void setVideoBitrate(final BitrateParameter.bitrate videoBitrate) {
+               this.videoBitrate = videoBitrate;
+       }
+
 }
index db6ab4d..2cda2d8 100644 (file)
@@ -42,7 +42,7 @@ public class AvconvVideo extends AbstractConverter {
                int videoBitrate = -1;
                int audioBitrate = -1;
 
-               switch (options.videoBitrate) {
+               switch (options.getVideoBitrate()) {
                case LOW:
                        videoBitrate = 1000;
                        audioBitrate = 128;
@@ -62,7 +62,7 @@ public class AvconvVideo extends AbstractConverter {
                        break;
 
                default:
-                       throw new RuntimeException("Video bitrate: " + options.videoBitrate
+                       throw new RuntimeException("Video bitrate: " + options.getVideoBitrate()
                                        + " is not supported.");
                }
 
@@ -72,7 +72,7 @@ public class AvconvVideo extends AbstractConverter {
                String videoCodec = "libx264";
                String audioCodec = "libmp3lame";
 
-               if (options.videoBitrate == bitrate.COPY) {
+               if (options.getVideoBitrate() == bitrate.COPY) {
                        videoCodec = "copy";
                        audioCodec = "copy";
                }
@@ -88,8 +88,8 @@ public class AvconvVideo extends AbstractConverter {
                                codecParams, videoCodec, audioCodec);
 
                return "avconv -i \"" + inputFile.getAbsolutePath() + "\" "
-               + codecParams.toString() + "\"" + targetFile.getAbsolutePath()
-               + "\"";
+                               + codecParams.toString() + "\"" + targetFile.getAbsolutePath()
+                               + "\"";
        }
 
        @Override
@@ -99,7 +99,7 @@ public class AvconvVideo extends AbstractConverter {
 
        @Override
        public List<String> getTargetFileExtensions() {
-               return toList("mkv", "mts", "mp4", "avi", "mpg", "mpeg", "vob");
+               return toList("mkv", "mts", "mp4", "avi", "mpg", "mpeg", "vob", "m4v");
        }
 
        @Override
index 101cc3f..ca78c4a 100755 (executable)
@@ -116,7 +116,7 @@ public class Main implements Module {
                }
 
                final EncodingOptions options = new EncodingOptions();
-               options.videoBitrate = BitrateParameter.bitrate.COPY;
+               options.setVideoBitrate(BitrateParameter.bitrate.COPY);
 
                encodingPlan.execute(options);
        }
index 95aacf0..5f27ee0 100755 (executable)
@@ -1,7 +1,7 @@
 /*
  * Meviz - Various tools collection to work with multimedia.
  * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- * 
+ *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of version 2 of the GNU General Public License
  * as published by the Free Software Foundation.
@@ -21,16 +21,18 @@ public class CommandlineHandler {
 
        Parser parser = new Parser();
 
-       StringParameter galleryNameParameter = parser.createStringParameter(
-                       "Gallery title. (default is: " + Constants.DEFAULT_GALLERY_TITLE
-                                       + ").").addAliases("-t", "--gallery-title");
+       StringParameter galleryNameParameter = parser.add(
+                       new StringParameter("Gallery title. (default is: "
+                                       + Constants.DEFAULT_GALLERY_TITLE + ").")).addAliases("-t",
+                       "--gallery-title");
 
        DirectoryParameter workingDirectoryParameter = parser
-                       .createDirectoryParameter("Working directory.")
+                       .add(new DirectoryParameter("Working directory."))
                        .addAliases("-w", "--working-directory").mustExist();
 
-       NullParameter removeIndex = parser.createNullParameter(
-                       "Remove generated index").addAliases("-r", "--remove");
+       NullParameter removeIndex = parser.add(
+                       new NullParameter("Remove generated index")).addAliases("-r",
+                       "--remove");
 
        public String getGalleryTitle() {
                if (galleryNameParameter.isParameterSpecified())
index e5ebda3..817d5e5 100755 (executable)
@@ -22,21 +22,25 @@ public class CommandlineHandler {
 
        Parser parser = new Parser();
 
-       NullParameter recursiveParameter = parser.createNullParameter(
-                       "Enable recursive mode.").addAliases("-r", "--recursive");
+       NullParameter recursiveParameter = parser.add(
+                       new NullParameter("Enable recursive mode.")).addAliases("-r",
+                       "--recursive");
 
-       NullParameter testParameter = parser.createNullParameter(
-                       "Simulate renaming (no changes will be actually done).")
+       NullParameter testParameter = parser.add(
+                       new NullParameter(
+                                       "Simulate renaming (no changes will be actually done)."))
                        .addAliases("-t", "--test");
 
-       StringParameter outputPatternParameter = parser.createStringParameter(
-                       "Output pattern.").addAliases("-o", "--output-pattern");
+       StringParameter outputPatternParameter = parser.add(
+                       new StringParameter("Output pattern.")).addAliases("-o",
+                       "--output-pattern");
 
-       StringParameters inputPatternParameter = parser.createStringParameters(
-                       "File input pattern.").addAliases("-i", "--input-pattern");
+       StringParameters inputPatternParameter = parser.add(
+                       new StringParameters("File input pattern.")).addAliases("-i",
+                       "--input-pattern");
 
        DirectoryParameter workingDirectoryParameter = parser
-                       .createDirectoryParameter("Working directory.")
+                       .add(new DirectoryParameter("Working directory."))
                        .addAliases("-w", "--working-directory").mustExist();
 
        /**
index 898b391..3f153c9 100755 (executable)
@@ -1,7 +1,7 @@
 /*
  * Meviz - Various tools collection to work with multimedia.
  * Copyright (C) 2012, Svjatoslav Agejenko, svjatoslav@svjatoslav.eu
- * 
+ *
  * This program is free software; you can redistribute it and/or
  * modify it under the terms of version 2 of the GNU General Public License
  * as published by the Free Software Foundation.
@@ -20,18 +20,20 @@ public class CommandlineHandler {
 
        Parser parser = new Parser();
 
-       NullParameter recursiveParameter = parser.createNullParameter(
-                       "Enable recursive mode.").addAliases("-r", "--recursive");
+       NullParameter recursiveParameter = parser.add(
+                       new NullParameter("Enable recursive mode.")).addAliases("-r",
+                       "--recursive");
 
        StringParameter searchForPattern = parser
-                       .createStringParameter("String to search for")
+                       .add(new StringParameter("String to search for"))
                        .addAliases("-s", "--search-pattern").setMandatory();
 
-       StringParameter replaceWithPattern = parser.createStringParameter(
-                       "String to place instead").addAliases("-p", "--replace-pattern");
+       StringParameter replaceWithPattern = parser.add(
+                       new StringParameter("String to place instead")).addAliases("-p",
+                                       "--replace-pattern");
 
        DirectoryParameter directoryParameter = parser
-                       .createDirectoryParameter("Working directory.")
+                       .add(new DirectoryParameter("Working directory."))
                        .addAliases("-w", "--working-directory").mustExist();
 
        public CommandlineOptions parseCommandlineArguments(final String[] args) {
index ef2a27a..713e10f 100755 (executable)
@@ -20,20 +20,21 @@ public class CommandlineHandler {
 
        Parser parser = new Parser();
 
-       NullParameter recursiveParameter = parser.createNullParameter(
-                       "Enable recursive mode.").addAliases("-r", "--recursive");
+       NullParameter recursiveParameter = parser.add(
+                       new NullParameter("Enable recursive mode.")).addAliases("-r",
+                       "--recursive");
 
-       StringParameters fileInputPatternsParameter = parser
-                       .createStringParameters("File input pattern.")
-                       .addAliases("-i", "--input-pattern").setMandatory();
+       StringParameters fileInputPatternsParameter = parser.add(
+                       new StringParameters("File input pattern.").addAliases("-i",
+                                       "--input-pattern")).setMandatory();
 
        StringParameters splitPatternsParameter = parser
-                       .createStringParameters("File split regular expression.")
+                       .add(new StringParameters("File split regular expression."))
                        .addAliases("-s", "--split-pattern").setMandatory();
 
-       DirectoryParameter workingDirectoryParameter = parser
-                       .createDirectoryParameter("Working directory.").addAliases("-w",
-                                       "--working-directory");
+       DirectoryParameter workingDirectoryParameter = parser.add(
+                       new DirectoryParameter("Working directory.")).addAliases("-w",
+                       "--working-directory");
 
        public TextSplittingOptions parseCommandlineArguments(final String[] args) {
 
index cad7c22..e77e822 100755 (executable)
@@ -20,15 +20,17 @@ public class CommandlineHandler {
 
        Parser parser = new Parser();
 
-       NullParameter recursiveParameter = parser.createNullParameter(
-                       "Enable recursive mode.").addAliases("-r", "--recursive");
+       NullParameter recursiveParameter = parser.add(
+                       new NullParameter("Enable recursive mode.")).addAliases("-r",
+                       "--recursive");
 
-       StringParameters inputPatternParameter = parser.createStringParameters(
-                       "File input pattern.").addAliases("-i", "--input-pattern");
+       StringParameters inputPatternParameter = parser.add(
+                       new StringParameters("File input pattern.")).addAliases("-i",
+                                       "--input-pattern");
 
-       DirectoryParameter workingDirectoryParameter = parser
-                       .createDirectoryParameter("Working directory.").addAliases("-w",
-                                       "--working-directory");
+       DirectoryParameter workingDirectoryParameter = parser.add(
+                       new DirectoryParameter("Working directory.")).addAliases("-w",
+                       "--working-directory");
 
        public TextTruncatingOptions parseCommandlineArguments(final String[] args) {