OSDN Git Service

CLI: command line refactoring
authorHiroshi Miura <miurahr@linux.com>
Thu, 28 Apr 2016 13:20:35 +0000 (22:20 +0900)
committerHiroshi Miura <miurahr@linux.com>
Thu, 28 Apr 2016 15:04:45 +0000 (00:04 +0900)
- Drop --debug/-D --verbose/-V
- Support hex and octet for -s/-e options.
- Clean up unused variables.

Signed-off-by: Hiroshi Miura <miurahr@linux.com>
CHANGELOG.md
dictzip-cli/doc/dictzip.1.in
dictzip-cli/src/main/java/org/dict/zip/cli/CommandLine.java
dictzip-cli/src/main/java/org/dict/zip/cli/Main.java
dictzip-cli/src/main/resources/org/dict/zip/cli/Bundle.properties
dictzip-cli/src/main/resources/org/dict/zip/cli/Bundle_ja.properties
dictzip-cli/src/test/java/org/dict/zip/cli/CommandLineTest.java

index 4a27206..6e962fe 100644 (file)
@@ -9,12 +9,15 @@ All notable changes to this project will be documented in this file.
 - DictZipHeader.setHeaderCRC() argument become final.
 - DictZipFileUtils class become final.
 - CLI: drop -S -E <base64> arugment.
+- CLI: support -s/-e hex(0xAAAA) and octet(0777) numbers.
 - CLI: update man page according to option changes.
+- Manpage description.
 
 ### Fixed
 - Generate Maven POM with a proper groupId.
 - DictZipInputStream: copy buffer with offset by System.copyarray().
 - CLI: Enable -t --test functionarity.
+- CLI: fix --version not working.
 
 ## [0.7.0] - 2016-4-25
 ### Add
index 01e69a0..5343455 100644 (file)
@@ -137,8 +137,7 @@ Write output on standard output; keep original files unchanged.  This is
 only available when decompressing (because parts of the header must be
 updated after a write when compressing).
 .TP
-.BR \-f " or " \-\-force
-Force compression or decompression even if the output file already exists.
+.BR \-f " or " \-\-force Force compression or decompression even if the output file already exists.
 .TP
 .BR \-h " or " \-\-help
 Display help.
@@ -171,19 +170,16 @@ license and quit.
 .BR \-t " or " \-\-test
 Check the compressed file integrity.
 .TP
-.BR \-v " or " \-\-verbose
-Verbose. Display extra information during compression.
-.TP
-.BR \-V " or " \-\-version
+.BR \-v " or " \-\-version
 Version. Display the version number and compilation options then quit.
 .TP
 .BI \-s " start\fR or " \-\-start " start"
-Specify the offer to start decompression, using decimal numbers.  The
-default is at the beginning of the file.
+Specify the offer to start decompression, using decimal, hex or octet
+numbers.  The default is at the beginning of the file.
 .TP
 .BI \-e " size\fR or " \-\-size " size"
-Specify the size of the portion of the file to decompress, using decimal
-numbers.  The default is the whole file.
+Specify the size of the portion of the file to decompress, using decimal,
+hex or octet numbers.  The default is the whole file.
 .SH CREDITS
 .B dictzip
 was written by Rik Faith (faith@cs.unc.edu) and is distributed under the
index d9a8bb8..9a7fb58 100644 (file)
@@ -51,7 +51,7 @@ public class CommandLine {
         return targetFiles;
     }
 
-    private static final int OPTS_LEN = 19;
+    private static final int OPTS_LEN = 13;
     /**
      * Parse command line and set preferences.
      *
@@ -63,11 +63,8 @@ public class CommandLine {
         String arg;
 
         LongOpt[] longOpts = new LongOpt[OPTS_LEN];
-        StringBuffer debugLevelVal = new StringBuffer();
         StringBuffer startVal = new StringBuffer();
         StringBuffer sizeVal = new StringBuffer();
-        StringBuffer preFilterName = new StringBuffer();
-        StringBuffer postFilterName = 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');
@@ -76,15 +73,11 @@ public class CommandLine {
         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("verbose", LongOpt.NO_ARGUMENT, null, 'v');
-        longOpts[9] = new LongOpt("version", LongOpt.NO_ARGUMENT, null, 'V');
-        longOpts[10] = new LongOpt("debug", LongOpt.REQUIRED_ARGUMENT, debugLevelVal, 'D');
-        longOpts[11] = new LongOpt("start", LongOpt.REQUIRED_ARGUMENT, startVal, 's');
-        longOpts[12] = new LongOpt("size", LongOpt.REQUIRED_ARGUMENT, sizeVal, 'e');
-        longOpts[15] = new LongOpt("pre", LongOpt.REQUIRED_ARGUMENT, preFilterName, 'p');
-        longOpts[16] = new LongOpt("post", LongOpt.REQUIRED_ARGUMENT, postFilterName, 'P');
-        longOpts[17] = new LongOpt("fast", LongOpt.NO_ARGUMENT, null, '1');
-        longOpts[18] = new LongOpt("best", LongOpt.NO_ARGUMENT, null, '9');
+        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
@@ -127,12 +120,10 @@ public class CommandLine {
                     break;
                 case 'h':
                     System.out.println(AppConsts.getNameAndVersion());
-                    System.out.println(MessageFormat.format(getString("help.copyright.template"),
-                            AppConsts.YEAR, AppConsts.AUTHORS));
+                    showCopyright();
                     System.out.println();
-                    System.out.println(MessageFormat.format(getString("help.message"),
-                            AppConsts.NAME));
-                    break;
+                    showHelp();
+                    return 1;
                 case 'k':
                     options.setKeep(true);
                     break;
@@ -141,36 +132,47 @@ public class CommandLine {
                     break;
                 case 'L':
                     System.out.println(AppConsts.getNameAndVersion());
-                    System.out.println(MessageFormat.format(getString("help.copyright.template"),
-                            AppConsts.YEAR, AppConsts.AUTHORS));
+                    showCopyright();
                     System.out.println();
                     System.out.println(getString("help.license"));
-                    break;
+                    return 1;
                 case 'e':
                     arg = g.getOptarg();
-                    options.setSize(Integer.getInteger(arg));
+                    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();
-                    options.setStart(Integer.getInteger(arg));
+                    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());
-                    System.out.println(MessageFormat.format(getString("help.copyright.template"),
-                            AppConsts.YEAR, AppConsts.AUTHORS));
-                    System.out.println();
-                    break;
+                    showCopyright();
+                    return 1;
                 case ':':
-                    System.out.println("Doh! You need an argument for option "
+                    System.err.println("Doh! You need an argument for option "
                             + (char) g.getOptopt());
-                    break;
+                    showHelp();
+                    return 2;
                 case '?':
                     System.out.println("The option '" + (char) g.getOptopt()
                             + "' is not valid");
-                    return 1;
+                    showHelp();
+                    return 2;
                 default:
                     System.out.println("getopt() returned " + c);
                     break;
@@ -180,4 +182,24 @@ public class CommandLine {
         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);
+        }
+    }
 }
index ec3e08c..9daef7e 100644 (file)
@@ -51,8 +51,11 @@ public final class Main {
     public static void main(final String[] argv) {
         int res = commandLine.parse(argv);
         // If error in command line, exit with code
-        if (res != 0) {
+        if (res > 1) {
             System.exit(res);
+        } else if (res == 1) {
+            // normal exit.
+            System.exit(0);
         }
         for (String fName: commandLine.getTargetFiles()) {
             try {
index d7fb22f..2170269 100644 (file)
@@ -54,8 +54,6 @@ Usage: {0} [options] name\n\
 -v --version         display version number\n\
 -s --start <offset>  starting offset for decompression (decimal)\n\
 -e --size <offset>   size for decompression (decimal)\n\
--S --Start <offset>  starting offset for decompression (base64)\n\
--E --Size <offset>   size for decompression (base64)\n\
 -# --best --fast     Regulate the speed of compression
 
 help.license=\n\
@@ -80,3 +78,5 @@ main.delete.error=File delete failed.
 dictzip.header.title=type\tcrc     \tdate    \ttime  \tchunks\tsize\tcompr.\tuncompr.\tratio\tname
 
 main.test.error=File integrity error.
+
+commandline.error.num_format=Number format of argument is invalid.
index bc05228..161b96b 100644 (file)
@@ -54,8 +54,6 @@ Usage: {0} [options] name\n\
 -v --version         display version number\n\
 -s --start <offset>  starting offset for decompression (decimal)\n\
 -e --size <offset>   size for decompression (decimal)\n\
--S --Start <offset>  starting offset for decompression (base64)\n\
--E --Size <offset>   size for decompression (base64)\n\
 -# --best --fast     Regulate the speed of compression
 
 help.license=\n\
@@ -78,3 +76,5 @@ main.io.error=File I/O error.
 dictzip.header.title=type\tcrc     \tdate    \ttime \tchunks\tsize\tcompr.\tuncompr.\tratio\tname
 
 main.test.error=File integrity error.
+
+commandline.error.num_format=引数の数字が不正です。
index 4826464..5012562 100644 (file)
@@ -16,7 +16,7 @@ public class CommandLineTest {
         final String[] argv = new String[1];
         argv[0] = "-h";
         CommandLine commandLine = new CommandLine();
-        commandLine.parse(argv);
+        assertEquals(commandLine.parse(argv), 1);
     }
 
     @Test
@@ -24,7 +24,7 @@ public class CommandLineTest {
         final String[] argv = new String[1];
         argv[0] = "--help";
         CommandLine commandLine = new CommandLine();
-        commandLine.parse(argv);
+        assertEquals(commandLine.parse(argv), 1);
     }
 
     @Test
@@ -32,7 +32,7 @@ public class CommandLineTest {
         final String[] argv = new String[1];
         argv[0] = "-v";
         CommandLine commandLine = new CommandLine();
-        commandLine.parse(argv);
+        assertEquals(commandLine.parse(argv), 1);
     }
 
     @Test
@@ -40,7 +40,7 @@ public class CommandLineTest {
         final String[] argv = new String[1];
         argv[0] = "--version";
         CommandLine commandLine = new CommandLine();
-        commandLine.parse(argv);
+        assertEquals(commandLine.parse(argv), 1);
     }
 
     @Test
@@ -48,7 +48,7 @@ public class CommandLineTest {
         final String[] argv = new String[1];
         argv[0] = "-L";
         CommandLine commandLine = new CommandLine();
-        commandLine.parse(argv);
+        assertEquals(commandLine.parse(argv), 1);
     }
 
     @Test
@@ -56,7 +56,7 @@ public class CommandLineTest {
         final String[] argv = new String[1];
         argv[0] = "--license";
         CommandLine commandLine = new CommandLine();
-        commandLine.parse(argv);
+        assertEquals(commandLine.parse(argv), 1);
     }
 
     @Test
@@ -64,7 +64,7 @@ public class CommandLineTest {
         final String[] argv = new String[1];
         argv[0] = "-k";
         CommandLine commandLine = new CommandLine();
-        commandLine.parse(argv);
+        assertEquals(commandLine.parse(argv), 0);
         assertTrue(commandLine.options.isKeep());
     }
 
@@ -73,7 +73,7 @@ public class CommandLineTest {
         final String[] argv = new String[1];
         argv[0] = "--keep";
         CommandLine commandLine = new CommandLine();
-        commandLine.parse(argv);
+        assertEquals(commandLine.parse(argv), 0);
         assertTrue(commandLine.options.isKeep());
     }
 
@@ -82,7 +82,7 @@ public class CommandLineTest {
         final String[] argv = new String[1];
         argv[0] = "-c";
         CommandLine commandLine = new CommandLine();
-        commandLine.parse(argv);
+        assertEquals(commandLine.parse(argv), 0);
         assertTrue(commandLine.options.isStdout());
     }
 
@@ -91,7 +91,7 @@ public class CommandLineTest {
         final String[] argv = new String[1];
         argv[0] = "--stdout";
         CommandLine commandLine = new CommandLine();
-        commandLine.parse(argv);
+        assertEquals(commandLine.parse(argv), 0);
         assertTrue(commandLine.options.isStdout());
     }
 
@@ -100,7 +100,7 @@ public class CommandLineTest {
         final String[] argv = new String[1];
         argv[0] = "-f";
         CommandLine commandLine = new CommandLine();
-        commandLine.parse(argv);
+        assertEquals(commandLine.parse(argv), 0);
         assertTrue(commandLine.options.isForce());
     }
 
@@ -109,7 +109,7 @@ public class CommandLineTest {
         final String[] argv = new String[1];
         argv[0] = "--force";
         CommandLine commandLine = new CommandLine();
-        commandLine.parse(argv);
+        assertEquals(commandLine.parse(argv), 0);
         assertTrue(commandLine.options.isForce());
     }
 
@@ -118,7 +118,7 @@ public class CommandLineTest {
         final String[] argv = new String[1];
         argv[0] = "-d";
         CommandLine commandLine = new CommandLine();
-        commandLine.parse(argv);
+        assertEquals(commandLine.parse(argv), 0);
         assertTrue(commandLine.options.isDecompress());
     }
 
@@ -127,7 +127,7 @@ public class CommandLineTest {
         final String[] argv = new String[1];
         argv[0] = "--decompress";
         CommandLine commandLine = new CommandLine();
-        commandLine.parse(argv);
+        assertEquals(commandLine.parse(argv), 0);
         assertTrue(commandLine.options.isDecompress());
     }
 
@@ -136,7 +136,7 @@ public class CommandLineTest {
         final String[] argv = new String[1];
         argv[0] = "-l";
         CommandLine commandLine = new CommandLine();
-        commandLine.parse(argv);
+        assertEquals(commandLine.parse(argv), 0);
         assertTrue(commandLine.options.isList());
     }
 
@@ -145,7 +145,7 @@ public class CommandLineTest {
         final String[] argv = new String[1];
         argv[0] = "--list";
         CommandLine commandLine = new CommandLine();
-        commandLine.parse(argv);
+        assertEquals(commandLine.parse(argv), 0);
         assertTrue(commandLine.options.isList());
     }
 
@@ -154,7 +154,7 @@ public class CommandLineTest {
         final String[] argv = new String[1];
         argv[0] = "-1";
         CommandLine commandLine = new CommandLine();
-        commandLine.parse(argv);
+        assertEquals(commandLine.parse(argv), 0);
         assertEquals(commandLine.options.getLevel(), DictZipHeader.CompressionLevel.BEST_SPEED);
     }
 
@@ -163,7 +163,7 @@ public class CommandLineTest {
         final String[] argv = new String[1];
         argv[0] = "--fast";
         CommandLine commandLine = new CommandLine();
-        commandLine.parse(argv);
+        assertEquals(commandLine.parse(argv), 0);
         assertEquals(commandLine.options.getLevel(), DictZipHeader.CompressionLevel.BEST_SPEED);
     }
 
@@ -172,7 +172,7 @@ public class CommandLineTest {
         final String[] argv = new String[1];
         argv[0] = "-9";
         CommandLine commandLine = new CommandLine();
-        commandLine.parse(argv);
+        assertEquals(commandLine.parse(argv), 0);
         assertEquals(commandLine.options.getLevel(), DictZipHeader.CompressionLevel.BEST_COMPRESSION);
     }
 
@@ -181,7 +181,7 @@ public class CommandLineTest {
         final String[] argv = new String[1];
         argv[0] = "--best";
         CommandLine commandLine = new CommandLine();
-        commandLine.parse(argv);
+        assertEquals(commandLine.parse(argv), 0);
         assertEquals(commandLine.options.getLevel(), DictZipHeader.CompressionLevel.BEST_COMPRESSION);
     }
 
@@ -190,7 +190,7 @@ public class CommandLineTest {
         final String[] argv = new String[1];
         argv[0] = "-6";
         CommandLine commandLine = new CommandLine();
-        commandLine.parse(argv);
+        assertEquals(commandLine.parse(argv), 0);
         assertEquals(commandLine.options.getLevel(), DictZipHeader.CompressionLevel.DEFAULT_COMPRESSION);
     }
 
@@ -199,8 +199,68 @@ public class CommandLineTest {
         final String[] argv = new String[1];
         argv[0] = "target_filename";
         CommandLine commandLine = new CommandLine();
-        commandLine.parse(argv);
+        assertEquals(commandLine.parse(argv), 0);
         assertTrue(commandLine.getTargetFiles().equals(new ArrayList<String> (){{add("target_filename");}}));
     }
 
+    @Test
+    public void testParseNumber_hex() 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
+    public void testParseNumber_oct() 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
+    public void testParseNumber_dec() 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
+    public void testParse_size() 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
+    public void testParse_start_badnum() throws NumberFormatException {
+        final String[] argv = new String[1];
+        argv[0] = "-s 123A";
+        CommandLine commandLine = new CommandLine();
+        assertEquals(commandLine.parse(argv), 2);
+    }
+
+    @Test
+    public void testParse_start_badnumOct() throws NumberFormatException {
+        final String[] argv = new String[1];
+        argv[0] = "-s 0Q23A";
+        CommandLine commandLine = new CommandLine();
+        assertEquals(commandLine.parse(argv), 2);
+    }
+
+    @Test
+    public void testParse_start_badnumHex() throws NumberFormatException {
+        final String[] argv = new String[1];
+        argv[0] = "-s 0xQ23A";
+        CommandLine commandLine = new CommandLine();
+        assertEquals(commandLine.parse(argv), 2);
+    }
 }
\ No newline at end of file