From: Hiroshi Miura Date: Mon, 9 Oct 2023 01:29:38 +0000 (+0900) Subject: feat: migrate CLI library to pococli X-Git-Tag: v0.14.0~2^2 X-Git-Url: http://git.osdn.net/view?a=commitdiff_plain;h=053b6ea7eaed88635e2a80086257bbbad6b067aa;p=dictzip-java%2Fdictzip-java.git feat: migrate CLI library to pococli Signed-off-by: Hiroshi Miura --- diff --git a/dictzip-cli/build.gradle b/dictzip-cli/build.gradle index df2c619..f04ddc5 100644 --- a/dictzip-cli/build.gradle +++ b/dictzip-cli/build.gradle @@ -31,9 +31,9 @@ repositories { dependencies { implementation project(':dictzip-lib') implementation(libs.jetbrains.annotations) + implementation(libs.picocli) testImplementation(libs.commons.io) testImplementation(libs.junit.jupiter) - implementation 'gnu.getopt:java-getopt:1.0.13' testImplementation project(':northside-io') } @@ -48,6 +48,10 @@ java { } } +compileJava { + options.compilerArgs += ["-Aproject=${project.group}/${project.name}"] +} + tasks.register('mandoc', Copy) { from "doc/dictzip.1.in" into layout.buildDirectory.file("docs/man") diff --git a/dictzip-cli/src/main/java/org/dict/zip/cli/AppConsts.java b/dictzip-cli/src/main/java/org/dict/zip/cli/AppConsts.java index 3bc32cb..43a70a0 100644 --- a/dictzip-cli/src/main/java/org/dict/zip/cli/AppConsts.java +++ b/dictzip-cli/src/main/java/org/dict/zip/cli/AppConsts.java @@ -17,23 +17,31 @@ import java.util.ResourceBundle; * @author Hiroshi Miura */ public final class AppConsts { - static final String VERSION = ResourceBundle.getBundle("org/dict/zip/Version") + private static final String VERSION = ResourceBundle.getBundle("org/dict/zip/Version") .getString("version"); - static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle + private static final ResourceBundle RESOURCE_BUNDLE = ResourceBundle .getBundle("org/dict/zip/cli/Bundle", Locale.getDefault()); - static final String NAME = RESOURCE_BUNDLE.getString("application.name"); - static final String BRANDING = ""; - static final String YEAR = "2016-2022"; - static final String AUTHORS = "Hiroshi Miura"; + private static final String NAME = RESOURCE_BUNDLE.getString("application.name"); + private static final String BRANDING = ""; + private static final String YEAR = "2016-2023"; + private static final String AUTHORS = "Hiroshi Miura"; - private static String getString(final String key) { + static String getString(final String key) { return RESOURCE_BUNDLE.getString(key); } static String getNameAndVersion() { - return MessageFormat.format(getString("app-version-template-pretty"), - getApplicationName(), VERSION); + return MessageFormat.format(getString("app-version-template-pretty"), getApplicationName(), VERSION); + } + + static String getVersion() { + return VERSION; + } + + + static String getCopyright() { + return MessageFormat.format(getString("help.copyright.template"), AppConsts.YEAR, AppConsts.AUTHORS); } static String getApplicationName() { diff --git a/dictzip-cli/src/main/java/org/dict/zip/cli/CommandLine.java b/dictzip-cli/src/main/java/org/dict/zip/cli/CommandLine.java deleted file mode 100644 index 5c4e1b6..0000000 --- a/dictzip-cli/src/main/java/org/dict/zip/cli/CommandLine.java +++ /dev/null @@ -1,196 +0,0 @@ -/* - * DictZip library. - * - * Copyright (C) 2016-2022 Hiroshi Miura - * - * SPDX-License-Identifier: GNU General Public License v2.0 or later - */ - -package org.dict.zip.cli; - -import gnu.getopt.Getopt; -import gnu.getopt.LongOpt; -import org.dict.zip.DictZipHeader.CompressionLevel; - -import java.text.MessageFormat; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; - - -/** - * Parse and keep command options and arguments. - * @author Hiroshi Miura - */ -public class CommandLine { - /** - * Command line options. - */ - protected final Options options = new Options(); - - /** - * Target files for zip or unzip. - */ - protected final List targetFiles = new ArrayList<>(); - - private static String getString(final String key) { - return AppConsts.RESOURCE_BUNDLE.getString(key); - } - - protected List getTargetFiles() { - return targetFiles; - } - - private static final int OPTS_LEN = 13; - /** - * Parse command line and set preferences. - * - * @param argv command line argument - * @return exit code, if success return 0, otherwise return exit code. - */ - @SuppressWarnings("checkstyle:avoidinlineconditionals") - protected int parse(final String[] argv) { - int c; - String arg; - - LongOpt[] longOpts = new LongOpt[OPTS_LEN]; - StringBuffer startVal = new StringBuffer(); - StringBuffer sizeVal = new StringBuffer(); - longOpts[0] = new LongOpt("stdout", LongOpt.NO_ARGUMENT, null, 'c'); - longOpts[1] = new LongOpt("decompress", LongOpt.NO_ARGUMENT, null, 'd'); - longOpts[2] = new LongOpt("force", LongOpt.NO_ARGUMENT, null, 'f'); - longOpts[3] = new LongOpt("help", LongOpt.NO_ARGUMENT, null, 'h'); - longOpts[4] = new LongOpt("keep", LongOpt.NO_ARGUMENT, null, 'k'); - longOpts[5] = new LongOpt("list", LongOpt.NO_ARGUMENT, null, 'l'); - longOpts[6] = new LongOpt("license", LongOpt.NO_ARGUMENT, null, 'L'); - longOpts[7] = new LongOpt("test", LongOpt.NO_ARGUMENT, null, 't'); - longOpts[8] = new LongOpt("version", LongOpt.NO_ARGUMENT, null, 'v'); - longOpts[9] = new LongOpt("start", LongOpt.REQUIRED_ARGUMENT, startVal, 's'); - longOpts[10] = new LongOpt("size", LongOpt.REQUIRED_ARGUMENT, sizeVal, 'e'); - longOpts[11] = new LongOpt("fast", LongOpt.NO_ARGUMENT, null, '1'); - longOpts[12] = new LongOpt("best", LongOpt.NO_ARGUMENT, null, '9'); - assert (longOpts.length == OPTS_LEN); - Getopt g = new Getopt("dictzip", argv, "cdfhklLe:E:s:S:tvVD:p:P:169", longOpts); - g.setOpterr(false); // We'll do our own error handling - // - while ((c = g.getopt()) != -1) { - switch (c) { - case 0: - break; - case 1: - System.out.println("I see you have return in order set and that " - + "a non-option argv element was just found " - + "with the value '" + g.getOptarg() + "'"); - break; - - case 2: - arg = g.getOptarg(); - System.out.println("I know this, but pretend I didn't"); - System.out.println("We picked option " - + longOpts[g.getLongind()].getName() - + " with value " - + ((arg != null) ? arg : "null")); - break; - case '1': - options.setLevel(CompressionLevel.BEST_SPEED); - break; - case '6': - options.setLevel(CompressionLevel.DEFAULT_COMPRESSION); - break; - case '9': - options.setLevel(CompressionLevel.BEST_COMPRESSION); - break; - case 'c': - options.setStdout(true); - break; - case 'd': - options.setDecompress(true); - break; - case 'f': - options.setForce(true); - break; - case 'h': - System.out.println(AppConsts.getNameAndVersion()); - showCopyright(); - System.out.println(); - showHelp(); - return 1; - case 'k': - options.setKeep(true); - break; - case 'l': - options.setList(true); - break; - case 'L': - System.out.println(AppConsts.getNameAndVersion()); - showCopyright(); - System.out.println(); - System.out.println(getString("help.license")); - return 1; - case 'e': - arg = g.getOptarg(); - try { - options.setSize(parseNumber(arg.trim())); - } catch (NumberFormatException nfe) { - System.err.println(getString("commandline.error.num_format")); - showHelp(); - return 2; - } - break; - case 's': - arg = g.getOptarg(); - try { - options.setStart(parseNumber(arg.trim())); - } catch (NumberFormatException nfe) { - System.err.println(getString("commandline.error.num_format")); - showHelp(); - return 2; - } - break; - case 't': - options.setTest(true); - break; - case 'v': - System.out.println(AppConsts.getNameAndVersion()); - showCopyright(); - return 1; - case ':': - System.err.println("Doh! You need an argument for option " - + (char) g.getOptopt()); - showHelp(); - return 2; - case '?': - System.out.println("The option '" + (char) g.getOptopt() - + "' is not valid"); - showHelp(); - return 2; - default: - System.out.println("getopt() returned " + c); - break; - } - } - // - targetFiles.addAll(Arrays.asList(argv).subList(g.getOptind(), argv.length)); - return 0; - } - - private static void showCopyright() { - System.out.println(MessageFormat.format(getString("help.copyright.template"), - AppConsts.YEAR, AppConsts.AUTHORS)); - } - private static void showHelp() { - System.out.println(MessageFormat.format(getString("help.message"), - AppConsts.NAME)); - } - - private static Integer parseNumber(final String arg) throws NumberFormatException { - if (arg.startsWith("0x")) { - System.err.println(arg.substring(2)); - return Integer.parseInt(arg.substring(2), 16); - } else if (arg.startsWith("0")) { - return Integer.parseInt(arg, 8); - } else { - return Integer.parseInt(arg); - } - } -} diff --git a/dictzip-cli/src/main/java/org/dict/zip/cli/Main.java b/dictzip-cli/src/main/java/org/dict/zip/cli/Main.java index 3d5ebb5..eab1a7d 100644 --- a/dictzip-cli/src/main/java/org/dict/zip/cli/Main.java +++ b/dictzip-cli/src/main/java/org/dict/zip/cli/Main.java @@ -10,24 +10,17 @@ package org.dict.zip.cli; import org.dict.zip.DictZipFiles; import org.dict.zip.DictZipHeader.CompressionLevel; +import picocli.CommandLine; import java.io.File; -import java.util.Locale; -import java.util.ResourceBundle; import java.io.IOException; +import java.text.MessageFormat; /** * dictzip/dictunzip main class. * @author Hiroshi Miura */ public final class Main { - /** - * The localized strings are kept in a separate file. - */ - private static ResourceBundle messages = ResourceBundle.getBundle( - "org/dict/zip/cli/Bundle", Locale.getDefault()); - - private static CommandLine commandLine = new CommandLine(); /** * main method. @@ -35,22 +28,28 @@ public final class Main { * @param argv command line argument */ public static void main(final String[] argv) { - int res = commandLine.parse(argv); - // If error in command line, exit with code - if (res > 1) { - System.exit(res); - } else if (res == 1) { - // normal exit. + Options options = new Options(); + new CommandLine(options).parseArgs(argv); + + if (options.isHelpRequested()) { + System.out.println(AppConsts.getNameAndVersion()); + showCopyright(); + System.out.println(); + showHelp(); System.exit(0); } - for (String fName: commandLine.getTargetFiles()) { + if (options.isVersion()) { + System.out.println(AppConsts.getVersion()); + System.exit(0); + } + for (String fName: options.getTargetFiles()) { try { DictData dict; - if (commandLine.options.isList()) { - commandLine.options.setKeep(true); + if (options.isList()) { + options.setKeep(true); dict = new DictData(fName, null); dict.printHeader(); - } else if (commandLine.options.isTest()) { + } else if (options.isTest()) { boolean result = false; try { result = DictZipFiles.checkDictZipFile(fName); @@ -61,30 +60,37 @@ public final class Main { if (result) { System.exit(0); } else { - System.err.println(messages.getString("main.test.error")); + System.err.println(getString("main.test.error")); System.exit(1); } - } else if (commandLine.options.isDecompress()) { + } else if (options.isDecompress()) { String extractFile = DictZipUtils.uncompressedFileName(fName); - long start = commandLine.options.getStart(); - int size = commandLine.options.getSize(); + long start = options.getStart(); + int size = options.getSize(); dict = new DictData(extractFile, fName); dict.doUnzip(start, size); } else { // compression. String zippedFile = DictZipUtils.compressedFileName(fName); - CompressionLevel level = commandLine.options.getLevel(); + CompressionLevel level; + if (options.isBest()) { + level = CompressionLevel.BEST_COMPRESSION; + } else if (options.isFast()) { + level = CompressionLevel.BEST_SPEED; + } else { + level = CompressionLevel.DEFAULT_COMPRESSION; + } dict = new DictData(fName, zippedFile); dict.doZip(level); } - if (!commandLine.options.isKeep()) { + if (!options.isKeep()) { File targetFile = new File(fName); if (!targetFile.delete()) { - System.err.println(messages.getString("main.delete.error")); + System.err.println(getString("main.delete.error")); System.exit(2); } } } catch (IOException ex) { - System.err.println(messages.getString("main.io.error")); + System.err.println(getString("main.io.error")); System.err.println(ex.getLocalizedMessage()); System.exit(1); } @@ -95,4 +101,16 @@ public final class Main { private Main() { } + private static String getString(final String key) { + return AppConsts.getString(key); + } + + private static void showCopyright() { + System.out.println(AppConsts.getCopyright()); + } + private static void showHelp() { + System.out.println(MessageFormat.format(getString("help.message"), + AppConsts.getApplicationName())); + } + } diff --git a/dictzip-cli/src/main/java/org/dict/zip/cli/Options.java b/dictzip-cli/src/main/java/org/dict/zip/cli/Options.java index 0b9d75d..415deaa 100644 --- a/dictzip-cli/src/main/java/org/dict/zip/cli/Options.java +++ b/dictzip-cli/src/main/java/org/dict/zip/cli/Options.java @@ -8,7 +8,10 @@ package org.dict.zip.cli; -import org.dict.zip.DictZipHeader.CompressionLevel; +import picocli.CommandLine; + +import java.util.Collections; +import java.util.List; /** * @@ -16,18 +19,58 @@ import org.dict.zip.DictZipHeader.CompressionLevel; */ public class Options { + /** + * Command line options. + */ + @CommandLine.Option(names = {"-c", "--stdout"}, description = "return result in stdout.") + private boolean stdout = false; + + @CommandLine.Option(names = {"-d", "--decompress"}, description = "decompression mode") private boolean decompress = false; + + @CommandLine.Option(names = {"-f", "--force"}, description = "force overwrite.") private boolean force = false; + + @CommandLine.Option(names = {"-k", "--keep"}, description = "keep source archive") private boolean keep = false; + + @CommandLine.Option(names = {"-l", "--list"}, description = "list archive files") private boolean list = false; - private boolean stdout = false; + + @CommandLine.Option(names = {"-t", "--test"}, description = "test archive") private boolean test = false; + + @CommandLine.Option(names = {"-L", "--license"}, description = "show license information") + private boolean license; + + @CommandLine.Option(names = {"-v", "--verbose"}, description = "verbose") private boolean verbose = false; + private boolean debugVerbose = false; + + @CommandLine.Option(names = {"-s", "--start"}, description = "start position") private long start = 0; + + @CommandLine.Option(names = {"-e", "--size"}, description = "extract size") private int size = 0; - private CompressionLevel level = CompressionLevel.DEFAULT_COMPRESSION; + @CommandLine.Option(names = {"-h", "--help"}, usageHelp = true, description = "show help") + private boolean helpRequested = false; + + @CommandLine.Option(names = {"-1", "--fast"}, description = "fast compression level") + private boolean fast; + + @CommandLine.Option(names = {"-6", "--moderate"}, description = "moderate compression level") + private boolean moderate; + + @CommandLine.Option(names = {"-9", "--best"}, description = "best compression level") + private boolean best; + + @CommandLine.Option(names = {"-V", "--version"}, description = "show version") + private boolean version; + + @CommandLine.Parameters + private List targetFiles; /** * Whether -d option is spedified. @@ -38,14 +81,6 @@ public class Options { } /** - * Set -d option. - * @param decompress true if -d option is specified. - */ - void setDecompress(final boolean decompress) { - this.decompress = decompress; - } - - /** * Whether -f/--force option is specified. * @return true if -f option is specified. */ @@ -54,14 +89,6 @@ public class Options { } /** - * Set -f/--force option. - * @param force true if -f option is specified. - */ - void setForce(final boolean force) { - this.force = force; - } - - /** * Whether -k/--keep is specified or not. * @return true if -k is specified */ @@ -86,14 +113,6 @@ public class Options { } /** - * Set -l option. - * @param list true if -l option is specified. - */ - void setList(final boolean list) { - this.list = list; - } - - /** * Whther -c option is specified. * @return true if -l option is specified. */ @@ -102,14 +121,6 @@ public class Options { } /** - * Set -c option. - * @param stdout true when -c option specified. - */ - void setStdout(final boolean stdout) { - this.stdout = stdout; - } - - /** * Whether -t/--test option specified. * @return true if -t specified. */ @@ -118,14 +129,6 @@ public class Options { } /** - * Set -t option. - * @param test true if -t specified. - */ - void setTest(final boolean test) { - this.test = test; - } - - /** * Whether -v/--verbose option specified. * @return true if -v specified. */ @@ -134,14 +137,6 @@ public class Options { } /** - * Set -v option. - * @param verbose true if -v option specified. - */ - void setVerbose(final boolean verbose) { - this.verbose = verbose; - } - - /** * Whether -Dverbose/--debug=verbose specified. * @return true if -Dverbose specified. */ @@ -150,14 +145,6 @@ public class Options { } /** - * Set -Dverbose/--debug=verbose specified. - * @param debugVerbose true if specified. - */ - void setDebugVerbose(final boolean debugVerbose) { - this.debugVerbose = debugVerbose; - } - - /** * Get value of -s/--start option. * @return Value of -s/--start option. If not specified, return 0. */ @@ -166,14 +153,6 @@ public class Options { } /** - * Set value of start option. - * @param start value of start option. - */ - void setStart(final long start) { - this.start = start; - } - - /** * Get value of -S/--size option. * @return value of size option. */ @@ -181,19 +160,31 @@ public class Options { return size; } - /** - * Set value of size option. - * @param size value of size option. - */ - void setSize(final int size) { - this.size = size; + public boolean isLicense() { + return license; } - CompressionLevel getLevel() { - return level; + public boolean isHelpRequested() { + return helpRequested; } - void setLevel(final CompressionLevel level) { - this.level = level; + public boolean isFast() { + return fast; } + + public boolean isBest() { + return best; + } + + public boolean isVersion() { + return version; + } + + List getTargetFiles() { + if (targetFiles == null) { + targetFiles = Collections.emptyList(); + } + return targetFiles; + } + } diff --git a/dictzip-cli/src/test/java/org/dict/zip/cli/CommandLineTest.java b/dictzip-cli/src/test/java/org/dict/zip/cli/CommandLineTest.java deleted file mode 100644 index 85b8d1b..0000000 --- a/dictzip-cli/src/test/java/org/dict/zip/cli/CommandLineTest.java +++ /dev/null @@ -1,366 +0,0 @@ -package org.dict.zip.cli; - -import org.dict.zip.DictZipHeader; -import org.junit.jupiter.api.Test; - -import java.util.ArrayList; - -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; - - -/** - * Created by miurahr on 16/04/09. - */ -public class CommandLineTest { - /** - * Test Parser help. - */ - @Test - public void testParseHelp() { - final String[] argv = new String[1]; - argv[0] = "-h"; - CommandLine commandLine = new CommandLine(); - assertEquals(commandLine.parse(argv), 1); - } - - /** - * Test parser helpLong. - */ - @Test - public void testParseHelpLong() { - final String[] argv = new String[1]; - argv[0] = "--help"; - CommandLine commandLine = new CommandLine(); - assertEquals(commandLine.parse(argv), 1); - } - - /** - * Test parser version. - */ - @Test - public void testParseVersion() { - final String[] argv = new String[1]; - argv[0] = "-v"; - CommandLine commandLine = new CommandLine(); - assertEquals(commandLine.parse(argv), 1); - } - - /** - * Test parser VersionLong. - */ - @Test - public void testParseVersionLong() { - final String[] argv = new String[1]; - argv[0] = "--version"; - CommandLine commandLine = new CommandLine(); - assertEquals(commandLine.parse(argv), 1); - } - - /** - * Test parser License. - */ - @Test - public void testParseLicense() { - final String[] argv = new String[1]; - argv[0] = "-L"; - CommandLine commandLine = new CommandLine(); - assertEquals(commandLine.parse(argv), 1); - } - - /** - * Test parser License long. - */ - @Test - public void testParseLicenseLong() { - final String[] argv = new String[1]; - argv[0] = "--license"; - CommandLine commandLine = new CommandLine(); - assertEquals(commandLine.parse(argv), 1); - } - - /** - * Test parser Keep. - */ - @Test - public void testParseKeep() { - final String[] argv = new String[1]; - argv[0] = "-k"; - CommandLine commandLine = new CommandLine(); - assertEquals(commandLine.parse(argv), 0); - assertTrue(commandLine.options.isKeep()); - } - - /** - * Test parser KeepLong. - */ - @Test - public void testParseKeepLong() { - final String[] argv = new String[1]; - argv[0] = "--keep"; - CommandLine commandLine = new CommandLine(); - assertEquals(commandLine.parse(argv), 0); - assertTrue(commandLine.options.isKeep()); - } - - /** - * Test parser stdout. - */ - @Test - public void testParseStdout() { - final String[] argv = new String[1]; - argv[0] = "-c"; - CommandLine commandLine = new CommandLine(); - assertEquals(commandLine.parse(argv), 0); - assertTrue(commandLine.options.isStdout()); - } - - /** - * Test parser stdout long. - */ - @Test - public void testParseStdoutLong() { - final String[] argv = new String[1]; - argv[0] = "--stdout"; - CommandLine commandLine = new CommandLine(); - assertEquals(commandLine.parse(argv), 0); - assertTrue(commandLine.options.isStdout()); - } - - /** - * Test parser force. - */ - @Test - public void testParseForce() { - final String[] argv = new String[1]; - argv[0] = "-f"; - CommandLine commandLine = new CommandLine(); - assertEquals(commandLine.parse(argv), 0); - assertTrue(commandLine.options.isForce()); - } - - /** - * Test parser force long. - */ - @Test - public void testParseForceLong() { - final String[] argv = new String[1]; - argv[0] = "--force"; - CommandLine commandLine = new CommandLine(); - assertEquals(commandLine.parse(argv), 0); - assertTrue(commandLine.options.isForce()); - } - - /** - * Test parser decompresss. - */ - @Test - public void testParseDecompress() { - final String[] argv = new String[1]; - argv[0] = "-d"; - CommandLine commandLine = new CommandLine(); - assertEquals(commandLine.parse(argv), 0); - assertTrue(commandLine.options.isDecompress()); - } - - /** - * Test parser decompress long. - */ - @Test - public void testParseDecompressLong() { - final String[] argv = new String[1]; - argv[0] = "--decompress"; - CommandLine commandLine = new CommandLine(); - assertEquals(commandLine.parse(argv), 0); - assertTrue(commandLine.options.isDecompress()); - } - - /** - * Test parser List. - */ - @Test - public void testParseList() { - final String[] argv = new String[1]; - argv[0] = "-l"; - CommandLine commandLine = new CommandLine(); - assertEquals(commandLine.parse(argv), 0); - assertTrue(commandLine.options.isList()); - } - - /** - * Test parser list long version. - */ - @Test - public void testParseListLong() { - final String[] argv = new String[1]; - argv[0] = "--list"; - CommandLine commandLine = new CommandLine(); - assertEquals(commandLine.parse(argv), 0); - assertTrue(commandLine.options.isList()); - } - - /** - * test parsre leve fast. - */ - @Test - public void testParseLevelFast() { - final String[] argv = new String[1]; - argv[0] = "-1"; - CommandLine commandLine = new CommandLine(); - assertEquals(commandLine.parse(argv), 0); - assertEquals(commandLine.options.getLevel(), DictZipHeader.CompressionLevel.BEST_SPEED); - } - - /** - * Test parser level fast long version. - */ - @Test - public void testParseLevelFastLong() { - final String[] argv = new String[1]; - argv[0] = "--fast"; - CommandLine commandLine = new CommandLine(); - assertEquals(commandLine.parse(argv), 0); - assertEquals(commandLine.options.getLevel(), DictZipHeader.CompressionLevel.BEST_SPEED); - } - - /** - * Test parser Level best. - */ - @Test - public void testParseLevelBest() { - final String[] argv = new String[1]; - argv[0] = "-9"; - CommandLine commandLine = new CommandLine(); - assertEquals(commandLine.parse(argv), 0); - assertEquals(commandLine.options.getLevel(), DictZipHeader.CompressionLevel.BEST_COMPRESSION); - } - - /** - * Test parser Level best long version. - */ - @Test - public void testParseLevelBestLong() { - final String[] argv = new String[1]; - argv[0] = "--best"; - CommandLine commandLine = new CommandLine(); - assertEquals(commandLine.parse(argv), 0); - assertEquals(commandLine.options.getLevel(), DictZipHeader.CompressionLevel.BEST_COMPRESSION); - } - - /** - * test parser level default. - */ - @Test - public void testParseLevelDefault() { - final String[] argv = new String[1]; - argv[0] = "-6"; - CommandLine commandLine = new CommandLine(); - assertEquals(commandLine.parse(argv), 0); - assertEquals(commandLine.options.getLevel(), DictZipHeader.CompressionLevel.DEFAULT_COMPRESSION); - } - - /** - * test parser target. - */ - @Test - public void testParseTarget() { - final String[] argv = new String[1]; - argv[0] = "target_filename"; - CommandLine commandLine = new CommandLine(); - assertEquals(commandLine.parse(argv), 0); - assertTrue(commandLine.getTargetFiles().equals(new ArrayList() { - { - add("target_filename"); - } - } - )); - } - - /** - * Test parser number hex. - * @throws NumberFormatException when giving non-number argument. - */ - @Test - public void testParseNumberHex() throws NumberFormatException { - final String[] argv = new String[1]; - argv[0] = "-s 0x123A"; - CommandLine commandLine = new CommandLine(); - assertEquals(commandLine.parse(argv), 0); - assertEquals(commandLine.options.getStart(), 0x123A); - } - - /** - * Test parser Number octet. - * @throws NumberFormatException when giving non-number argument. - */ - @Test - public void testParseNumberOct() throws NumberFormatException { - final String[] argv = new String[1]; - argv[0] = "-s 01237"; - CommandLine commandLine = new CommandLine(); - assertEquals(commandLine.parse(argv), 0); - assertEquals(commandLine.options.getStart(), 671); - } - - /** - * Test parser number decimal. - * @throws NumberFormatException when giving non-number argument. - */ - @Test - public void testParseNumberDec() throws NumberFormatException { - final String[] argv = new String[1]; - argv[0] = "-s 1239"; - CommandLine commandLine = new CommandLine(); - assertEquals(commandLine.parse(argv), 0); - assertEquals(commandLine.options.getStart(), 1239); - } - - /** - * Test parser size. - * @throws NumberFormatException when giving non-number argument. - */ - @Test - public void testParseSize() throws NumberFormatException { - final String[] argv = new String[1]; - argv[0] = "-e 1239"; - CommandLine commandLine = new CommandLine(); - assertEquals(commandLine.parse(argv), 0); - assertEquals(commandLine.options.getSize(), 1239); - } - - /** - * Test parser start bad number. - * @throws NumberFormatException when giving bad number. - */ - @Test - public void testParseStartBadnum() throws NumberFormatException { - final String[] argv = new String[1]; - argv[0] = "-s 123A"; - CommandLine commandLine = new CommandLine(); - assertEquals(commandLine.parse(argv), 2); - } - - /** - * Test parser start badnum octet. - * @throws NumberFormatException when giving bad number. - */ - @Test - public void testParseStartBadnumOct() throws NumberFormatException { - final String[] argv = new String[1]; - argv[0] = "-s 0Q23A"; - CommandLine commandLine = new CommandLine(); - assertEquals(commandLine.parse(argv), 2); - } - - /** - * Test parser start badnum hex. - * @throws NumberFormatException when giving bad number. - */ - @Test - public void testParseStartBadnumHex() throws NumberFormatException { - final String[] argv = new String[1]; - argv[0] = "-s 0xQ23A"; - CommandLine commandLine = new CommandLine(); - assertEquals(commandLine.parse(argv), 2); - } -} diff --git a/gradle/libs.versions.toml b/gradle/libs.versions.toml index 262c194..f052ed3 100644 --- a/gradle/libs.versions.toml +++ b/gradle/libs.versions.toml @@ -16,6 +16,7 @@ guava = "31.0.1-jre" annotations = "23.0.0" trie4j = "0.9.8" stardict4j = "0.5.1" +picocli = "4.7.5" protobuf = "3.21.7" dictzip = "0.13.0" jacoco = "0.8.6" @@ -46,6 +47,7 @@ stardict4j = {group = "tokyo.northside", name = "stardict4j", version.ref = "sta protobuf = {group = "com.google.protobuf", name = "protobuf-java", version.ref = "protobuf"} dictzip = {group = "io.github.dictzip", name = "dictzip", version.ref = "dictzip"} testng = {group = "org.testng", name = "testng", version.ref = "testng"} +picocli = {group = "info.picocli", name = "picocli", version.ref = "picocli"} [plugins] spotbugs = {id = "com.github.spotbugs", version = "5.1.3"}