jdk/src/jdk.jartool/share/classes/sun/tools/jar/GNUStyleOptions.java
author prr
Tue, 09 Aug 2016 16:20:02 -0700
changeset 40435 553eb1a50733
parent 37779 7c84df693837
child 40261 86a49ba76f52
permissions -rw-r--r--
8163238: Upgrade to harfbuzz 1.3.0 in JDK 9 Reviewed-by: vadim, serb
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     1
/*
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     2
 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     4
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    10
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    15
 * accompanied this code).
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    16
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    20
 *
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    23
 * questions.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    24
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    25
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    26
package sun.tools.jar;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    27
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    28
import java.io.File;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    29
import java.io.PrintStream;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    30
import java.lang.module.ModuleFinder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.lang.module.ModuleDescriptor.Version;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.nio.file.Path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.nio.file.Paths;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.util.regex.Pattern;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.util.regex.PatternSyntaxException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
 * Parser for GNU Style Options.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
class GNUStyleOptions {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
    static class BadArgs extends Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
        static final long serialVersionUID = 0L;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
        boolean showUsage;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
        BadArgs(String key, String arg) { super(Main.formatMsg(key, arg)); }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
        BadArgs(String key) { super(Main.getMsg(key)); }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
        BadArgs showUsage(boolean b) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
            showUsage = b;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
            return this;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    54
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    55
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    56
    static Option[] recognizedOptions = {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
            // Main operations
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
            new Option(false, OptionType.MAIN_OPERATION, "--create", "-c") {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
                void process(Main tool, String opt, String arg) throws BadArgs {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
                    if (tool.iflag || tool.tflag || tool.uflag || tool.xflag || tool.printModuleDescriptor)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
                        throw new BadArgs("error.multiple.main.operations").showUsage(true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
                    tool.cflag = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
            },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
            new Option(true, OptionType.MAIN_OPERATION, "--generate-index", "-i") {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
                void process(Main tool, String opt, String arg) throws BadArgs {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
                    if (tool.cflag || tool.tflag || tool.uflag || tool.xflag || tool.printModuleDescriptor)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
                        throw new BadArgs("error.multiple.main.operations").showUsage(true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
                    tool.iflag = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
                    tool.rootjar = arg;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
            },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
            new Option(false, OptionType.MAIN_OPERATION, "--list", "-t") {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
                void process(Main tool, String opt, String arg) throws BadArgs {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
                    if (tool.cflag || tool.iflag || tool.uflag || tool.xflag || tool.printModuleDescriptor)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
                        throw new BadArgs("error.multiple.main.operations").showUsage(true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
                    tool.tflag = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
            },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
            new Option(false, OptionType.MAIN_OPERATION, "--update", "-u") {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
                void process(Main tool, String opt, String arg) throws BadArgs {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
                    if (tool.cflag || tool.iflag || tool.tflag || tool.xflag || tool.printModuleDescriptor)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
                        throw new BadArgs("error.multiple.main.operations").showUsage(true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
                    tool.uflag = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
            },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
            new Option(false, OptionType.MAIN_OPERATION, "--extract", "-x") {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
                void process(Main tool, String opt, String arg) throws BadArgs {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
                    if (tool.cflag || tool.iflag  || tool.tflag || tool.uflag || tool.printModuleDescriptor)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
                        throw new BadArgs("error.multiple.main.operations").showUsage(true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
                    tool.xflag = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
            },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
            new Option(false, OptionType.MAIN_OPERATION, "--print-module-descriptor", "-p") {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    95
                void process(Main tool, String opt, String arg) throws BadArgs {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
                    if (tool.cflag || tool.iflag  || tool.tflag || tool.uflag || tool.xflag)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
                        throw new BadArgs("error.multiple.main.operations").showUsage(true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
                    tool.printModuleDescriptor = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   100
            },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   101
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   102
            // Additional options
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
            new Option(true, OptionType.ANY, "--file", "-f") {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
                void process(Main jartool, String opt, String arg) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
                    jartool.fname = arg;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
            },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
            new Option(false, OptionType.ANY, "--verbose", "-v") {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
                void process(Main jartool, String opt, String arg) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
                    jartool.vflag = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
            },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
            new Option(false, OptionType.CREATE, "--normalize", "-n") {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
                void process(Main jartool, String opt, String arg) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
                    jartool.nflag = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
                boolean isHidden() { return true; }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
            },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
            new Option(true, OptionType.CREATE_UPDATE, "--main-class", "-e") {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
                void process(Main jartool, String opt, String arg) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
                    jartool.ename = arg;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
            },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
            new Option(true, OptionType.CREATE_UPDATE, "--manifest", "-m") {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
                void process(Main jartool, String opt, String arg) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
                    jartool.mname = arg;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
            },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
            new Option(false, OptionType.CREATE_UPDATE, "--no-manifest", "-M") {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
                void process(Main jartool, String opt, String arg) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
                    jartool.Mflag = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
            },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
            new Option(true, OptionType.CREATE_UPDATE, "--module-version") {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
                void process(Main jartool, String opt, String arg) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
                    jartool.moduleVersion = Version.parse(arg);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
            },
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   139
            new Option(true, OptionType.CREATE_UPDATE, "--hash-modules") {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   140
                void process(Main jartool, String opt, String arg) throws BadArgs {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
                    try {
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   142
                        jartool.modulesToHash = Pattern.compile(arg);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   143
                    } catch (PatternSyntaxException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
                        throw new BadArgs("err.badpattern", arg).showUsage(true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   146
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   147
            },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   148
            new Option(true, OptionType.CREATE_UPDATE, "--modulepath", "--mp") {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   149
                void process(Main jartool, String opt, String arg) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
                    String[] dirs = arg.split(File.pathSeparator);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
                    Path[] paths = new Path[dirs.length];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
                    int i = 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
                    for (String dir : dirs) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
                        paths[i++] = Paths.get(dir);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
                    jartool.moduleFinder = ModuleFinder.compose(jartool.moduleFinder,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
                                                                ModuleFinder.of(paths));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
            },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
            new Option(false, OptionType.CREATE_UPDATE_INDEX, "--no-compress", "-0") {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
                void process(Main jartool, String opt, String arg) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
                    jartool.flag0 = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   164
            },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   165
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   166
            // Hidden options
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
            new Option(false, OptionType.OTHER, "-P") {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   168
                void process(Main jartool, String opt, String arg) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
                    jartool.pflag = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
                boolean isHidden() { return true; }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   172
            },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   173
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
            // Other options
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   175
            new Option(true, true, OptionType.OTHER, "--help", "-h") {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   176
                void process(Main jartool, String opt, String arg) throws BadArgs {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   177
                    if (jartool.info == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   178
                        if (arg == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   179
                            jartool.info = Main.Info.HELP;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   180
                            return;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   181
                        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   182
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   183
                        if (!arg.equals("compat"))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   184
                            throw new BadArgs("error.illegal.option", arg).showUsage(true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   185
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   186
                        jartool.info = Main.Info.COMPAT_HELP;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   187
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   188
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   189
            },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   190
            new Option(false, OptionType.OTHER, "--version") {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   191
                void process(Main jartool, String opt, String arg) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   192
                    if (jartool.info == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   193
                        jartool.info = Main.Info.VERSION;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   194
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   195
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   196
    };
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   197
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   198
    enum OptionType {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   199
        MAIN_OPERATION("main"),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   200
        ANY("any"),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   201
        CREATE("create"),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   202
        CREATE_UPDATE("create.update"),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   203
        CREATE_UPDATE_INDEX("create.update.index"),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   204
        OTHER("other");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   205
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   206
        /** Resource lookup section prefix. */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   207
        final String name;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   208
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   209
        OptionType(String name) { this.name = name; }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   210
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   211
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   212
    static abstract class Option {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   213
        final boolean hasArg;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   214
        final boolean argIsOptional;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   215
        final String[] aliases;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   216
        final OptionType type;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   217
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   218
        Option(boolean hasArg, OptionType type, String... aliases) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   219
            this(hasArg, false, type, aliases);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   220
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   221
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   222
        Option(boolean hasArg, boolean argIsOptional, OptionType type, String... aliases) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   223
            this.hasArg = hasArg;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   224
            this.argIsOptional = argIsOptional;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   225
            this.type = type;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   226
            this.aliases = aliases;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   227
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   228
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   229
        boolean isHidden() { return false; }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   230
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   231
        boolean matches(String opt) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   232
            for (String a : aliases) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   233
                if (a.equals(opt)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   234
                    return true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   235
                } else if (opt.startsWith("--") && hasArg && opt.startsWith(a + "=")) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   236
                    return true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   237
                } else if (opt.startsWith("--help") && opt.startsWith(a + ":")) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   238
                    return true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   239
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   240
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   241
            return false;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   242
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   243
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   244
        abstract void process(Main jartool, String opt, String arg) throws BadArgs;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   245
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   246
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   247
    static int parseOptions(Main jartool, String[] args) throws BadArgs {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   248
        int count = 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   249
        if (args.length == 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   250
            jartool.info = Main.Info.USAGE_SUMMARY;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   251
            return 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   252
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   253
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   254
        // process options
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   255
        for (; count < args.length; count++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   256
            if (args[count].charAt(0) != '-' || args[count].equals("-C"))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   257
                break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   258
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   259
            String name = args[count];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   260
            Option option = getOption(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   261
            String param = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   262
            if (option.hasArg) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   263
                if (name.startsWith("--help")) {  // "special" optional separator
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   264
                    if (name.indexOf(':') > 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   265
                        param = name.substring(name.indexOf(':') + 1, name.length());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   266
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   267
                } else if (name.startsWith("--") && name.indexOf('=') > 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   268
                    param = name.substring(name.indexOf('=') + 1, name.length());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   269
                } else if (count + 1 < args.length) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   270
                    param = args[++count];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   271
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   272
                if (!option.argIsOptional &&
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   273
                    (param == null || param.isEmpty() || param.charAt(0) == '-')) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   274
                    throw new BadArgs("error.missing.arg", name).showUsage(true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   275
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   276
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   277
            option.process(jartool, name, param);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   278
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   279
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   280
        return count;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   281
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   282
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   283
    private static Option getOption(String name) throws BadArgs {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   284
        for (Option o : recognizedOptions) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   285
            if (o.matches(name)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   286
                return o;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   287
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   288
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   289
        throw new BadArgs("error.unrecognized.option", name).showUsage(true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   290
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   291
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   292
    static void printHelp(PrintStream out) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   293
        out.format("%s%n", Main.getMsg("main.help.preopt"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   294
        for (OptionType type : OptionType.values()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   295
            boolean typeHeadingWritten = false;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   296
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   297
            for (Option o : recognizedOptions) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   298
                if (!o.type.equals(type))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   299
                    continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   300
                String name = o.aliases[0].substring(1); // there must always be at least one name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   301
                name = name.charAt(0) == '-' ? name.substring(1) : name;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   302
                if (o.isHidden() || name.equals("h")) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   303
                    continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   304
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   305
                if (!typeHeadingWritten) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   306
                    out.format("%n%s%n", Main.getMsg("main.help.opt." + type.name));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   307
                    typeHeadingWritten = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   308
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   309
                out.format("%s%n", Main.getMsg("main.help.opt." + type.name + "." + name));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   310
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   311
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   312
        out.format("%n%s%n%n", Main.getMsg("main.help.postopt"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   313
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   314
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   315
    static void printCompatHelp(PrintStream out) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   316
        out.format("%s%n", Main.getMsg("usage.compat"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   317
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   318
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   319
    static void printUsageSummary(PrintStream out) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   320
        out.format("%s%n", Main.getMsg("main.usage.summary"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   321
        out.format("%s%n", Main.getMsg("main.usage.summary.try"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   322
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   323
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   324
    static void printVersion(PrintStream out) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   325
        out.format("%s %s%n", "jar", System.getProperty("java.version"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   326
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   327
}