From: Svjatoslav Agejenko Date: Fri, 19 Dec 2014 20:54:10 +0000 (+0200) Subject: more elegant commandline handling X-Git-Tag: meviz-1.0~40 X-Git-Url: http://www2.svjatoslav.eu/gitweb/?a=commitdiff_plain;h=bafe09c26e6ebb5fadf408ecd46bea79cd79e8f1;p=meviz.git more elegant commandline handling --- diff --git a/src/main/java/eu/svjatoslav/meviz/bomremove/CommandlineHandler.java b/src/main/java/eu/svjatoslav/meviz/bomremove/CommandlineHandler.java index 1bc4f3c..fdfc3c9 100755 --- a/src/main/java/eu/svjatoslav/meviz/bomremove/CommandlineHandler.java +++ b/src/main/java/eu/svjatoslav/meviz/bomremove/CommandlineHandler.java @@ -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; diff --git a/src/main/java/eu/svjatoslav/meviz/bomremove/Main.java b/src/main/java/eu/svjatoslav/meviz/bomremove/Main.java index 9eed788..201c723 100755 --- a/src/main/java/eu/svjatoslav/meviz/bomremove/Main.java +++ b/src/main/java/eu/svjatoslav/meviz/bomremove/Main.java @@ -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) diff --git a/src/main/java/eu/svjatoslav/meviz/encoder/BitrateParameter.java b/src/main/java/eu/svjatoslav/meviz/encoder/BitrateParameter.java index 6e3ade7..18c3a2c 100755 --- a/src/main/java/eu/svjatoslav/meviz/encoder/BitrateParameter.java +++ b/src/main/java/eu/svjatoslav/meviz/encoder/BitrateParameter.java @@ -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 { 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 { diff --git a/src/main/java/eu/svjatoslav/meviz/encoder/CommandlineHandler.java b/src/main/java/eu/svjatoslav/meviz/encoder/CommandlineHandler.java index 102ca7a..ec7d9cd 100755 --- a/src/main/java/eu/svjatoslav/meviz/encoder/CommandlineHandler.java +++ b/src/main/java/eu/svjatoslav/meviz/encoder/CommandlineHandler.java @@ -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."); diff --git a/src/main/java/eu/svjatoslav/meviz/encoder/EncodingOptions.java b/src/main/java/eu/svjatoslav/meviz/encoder/EncodingOptions.java index 9e7f2b4..6c666a1 100755 --- a/src/main/java/eu/svjatoslav/meviz/encoder/EncodingOptions.java +++ b/src/main/java/eu/svjatoslav/meviz/encoder/EncodingOptions.java @@ -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 inputPatterns = new ArrayList(); + 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; + } + } diff --git a/src/main/java/eu/svjatoslav/meviz/encoder/converters/AvconvVideo.java b/src/main/java/eu/svjatoslav/meviz/encoder/converters/AvconvVideo.java index db6ab4d..2cda2d8 100644 --- a/src/main/java/eu/svjatoslav/meviz/encoder/converters/AvconvVideo.java +++ b/src/main/java/eu/svjatoslav/meviz/encoder/converters/AvconvVideo.java @@ -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 getTargetFileExtensions() { - return toList("mkv", "mts", "mp4", "avi", "mpg", "mpeg", "vob"); + return toList("mkv", "mts", "mp4", "avi", "mpg", "mpeg", "vob", "m4v"); } @Override diff --git a/src/main/java/eu/svjatoslav/meviz/grabmemcard/Main.java b/src/main/java/eu/svjatoslav/meviz/grabmemcard/Main.java index 101cc3f..ca78c4a 100755 --- a/src/main/java/eu/svjatoslav/meviz/grabmemcard/Main.java +++ b/src/main/java/eu/svjatoslav/meviz/grabmemcard/Main.java @@ -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); } diff --git a/src/main/java/eu/svjatoslav/meviz/htmlindexer/CommandlineHandler.java b/src/main/java/eu/svjatoslav/meviz/htmlindexer/CommandlineHandler.java index 95aacf0..5f27ee0 100755 --- a/src/main/java/eu/svjatoslav/meviz/htmlindexer/CommandlineHandler.java +++ b/src/main/java/eu/svjatoslav/meviz/htmlindexer/CommandlineHandler.java @@ -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()) diff --git a/src/main/java/eu/svjatoslav/meviz/renamer/CommandlineHandler.java b/src/main/java/eu/svjatoslav/meviz/renamer/CommandlineHandler.java index e5ebda3..817d5e5 100755 --- a/src/main/java/eu/svjatoslav/meviz/renamer/CommandlineHandler.java +++ b/src/main/java/eu/svjatoslav/meviz/renamer/CommandlineHandler.java @@ -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(); /** diff --git a/src/main/java/eu/svjatoslav/meviz/replace/CommandlineHandler.java b/src/main/java/eu/svjatoslav/meviz/replace/CommandlineHandler.java index 898b391..3f153c9 100755 --- a/src/main/java/eu/svjatoslav/meviz/replace/CommandlineHandler.java +++ b/src/main/java/eu/svjatoslav/meviz/replace/CommandlineHandler.java @@ -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) { diff --git a/src/main/java/eu/svjatoslav/meviz/textsplitter/CommandlineHandler.java b/src/main/java/eu/svjatoslav/meviz/textsplitter/CommandlineHandler.java index ef2a27a..713e10f 100755 --- a/src/main/java/eu/svjatoslav/meviz/textsplitter/CommandlineHandler.java +++ b/src/main/java/eu/svjatoslav/meviz/textsplitter/CommandlineHandler.java @@ -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) { diff --git a/src/main/java/eu/svjatoslav/meviz/texttruncate/CommandlineHandler.java b/src/main/java/eu/svjatoslav/meviz/texttruncate/CommandlineHandler.java index cad7c22..e77e822 100755 --- a/src/main/java/eu/svjatoslav/meviz/texttruncate/CommandlineHandler.java +++ b/src/main/java/eu/svjatoslav/meviz/texttruncate/CommandlineHandler.java @@ -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) {