jdk/src/jdk.jartool/share/classes/sun/tools/jar/GNUStyleOptions.java
author mchung
Wed, 12 Oct 2016 15:41:00 -0700
changeset 41484 834b7539ada3
parent 40261 86a49ba76f52
child 41489 a289f1778f14
permissions -rw-r--r--
8164689: Retrofit jar, jlink, jmod as a ToolProvider Reviewed-by: alanb, lancea
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;
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 40261
diff changeset
    30
import java.io.PrintWriter;
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    31
import java.lang.module.ModuleFinder;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    32
import java.lang.module.ModuleDescriptor.Version;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    33
import java.nio.file.Path;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    34
import java.nio.file.Paths;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    35
import java.util.regex.Pattern;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    36
import java.util.regex.PatternSyntaxException;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    37
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    38
/**
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    39
 * Parser for GNU Style Options.
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    40
 */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    41
class GNUStyleOptions {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    42
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    43
    static class BadArgs extends Exception {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    44
        static final long serialVersionUID = 0L;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    45
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    46
        boolean showUsage;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    47
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    48
        BadArgs(String key, String arg) { super(Main.formatMsg(key, arg)); }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    49
        BadArgs(String key) { super(Main.getMsg(key)); }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    50
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    51
        BadArgs showUsage(boolean b) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    52
            showUsage = b;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    53
            return this;
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
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    57
    static Option[] recognizedOptions = {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    58
            // Main operations
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    59
            new Option(false, OptionType.MAIN_OPERATION, "--create", "-c") {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    60
                void process(Main tool, String opt, String arg) throws BadArgs {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    61
                    if (tool.iflag || tool.tflag || tool.uflag || tool.xflag || tool.printModuleDescriptor)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    62
                        throw new BadArgs("error.multiple.main.operations").showUsage(true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    63
                    tool.cflag = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    64
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    65
            },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    66
            new Option(true, OptionType.MAIN_OPERATION, "--generate-index", "-i") {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    67
                void process(Main tool, String opt, String arg) throws BadArgs {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    68
                    if (tool.cflag || tool.tflag || tool.uflag || tool.xflag || tool.printModuleDescriptor)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    69
                        throw new BadArgs("error.multiple.main.operations").showUsage(true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    70
                    tool.iflag = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    71
                    tool.rootjar = arg;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    72
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    73
            },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    74
            new Option(false, OptionType.MAIN_OPERATION, "--list", "-t") {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    75
                void process(Main tool, String opt, String arg) throws BadArgs {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    76
                    if (tool.cflag || tool.iflag || tool.uflag || tool.xflag || tool.printModuleDescriptor)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    77
                        throw new BadArgs("error.multiple.main.operations").showUsage(true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    78
                    tool.tflag = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    79
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    80
            },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    81
            new Option(false, OptionType.MAIN_OPERATION, "--update", "-u") {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    82
                void process(Main tool, String opt, String arg) throws BadArgs {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    83
                    if (tool.cflag || tool.iflag || tool.tflag || tool.xflag || tool.printModuleDescriptor)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    84
                        throw new BadArgs("error.multiple.main.operations").showUsage(true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    85
                    tool.uflag = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    86
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    87
            },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    88
            new Option(false, OptionType.MAIN_OPERATION, "--extract", "-x") {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    89
                void process(Main tool, String opt, String arg) throws BadArgs {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    90
                    if (tool.cflag || tool.iflag  || tool.tflag || tool.uflag || tool.printModuleDescriptor)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    91
                        throw new BadArgs("error.multiple.main.operations").showUsage(true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    92
                    tool.xflag = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    93
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    94
            },
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 37779
diff changeset
    95
            new Option(false, OptionType.MAIN_OPERATION, "--print-module-descriptor", "-d") {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    96
                void process(Main tool, String opt, String arg) throws BadArgs {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    97
                    if (tool.cflag || tool.iflag  || tool.tflag || tool.uflag || tool.xflag)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    98
                        throw new BadArgs("error.multiple.main.operations").showUsage(true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
    99
                    tool.printModuleDescriptor = true;
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
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   103
            // Additional options
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   104
            new Option(true, OptionType.ANY, "--file", "-f") {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   105
                void process(Main jartool, String opt, String arg) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   106
                    jartool.fname = arg;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   107
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   108
            },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   109
            new Option(false, OptionType.ANY, "--verbose", "-v") {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   110
                void process(Main jartool, String opt, String arg) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   111
                    jartool.vflag = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   112
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   113
            },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   114
            new Option(false, OptionType.CREATE, "--normalize", "-n") {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   115
                void process(Main jartool, String opt, String arg) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   116
                    jartool.nflag = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   117
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   118
                boolean isHidden() { return true; }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   119
            },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   120
            new Option(true, OptionType.CREATE_UPDATE, "--main-class", "-e") {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   121
                void process(Main jartool, String opt, String arg) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   122
                    jartool.ename = arg;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   123
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   124
            },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   125
            new Option(true, OptionType.CREATE_UPDATE, "--manifest", "-m") {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   126
                void process(Main jartool, String opt, String arg) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   127
                    jartool.mname = arg;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   128
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   129
            },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   130
            new Option(false, OptionType.CREATE_UPDATE, "--no-manifest", "-M") {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   131
                void process(Main jartool, String opt, String arg) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   132
                    jartool.Mflag = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   133
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   134
            },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   135
            new Option(true, OptionType.CREATE_UPDATE, "--module-version") {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   136
                void process(Main jartool, String opt, String arg) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   137
                    jartool.moduleVersion = Version.parse(arg);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   138
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   139
            },
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   140
            new Option(true, OptionType.CREATE_UPDATE, "--hash-modules") {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   141
                void process(Main jartool, String opt, String arg) throws BadArgs {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   142
                    try {
37779
7c84df693837 8154956: Module system implementation refresh (4/2016)
alanb
parents: 36511
diff changeset
   143
                        jartool.modulesToHash = Pattern.compile(arg);
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   144
                    } catch (PatternSyntaxException e) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   145
                        throw new BadArgs("err.badpattern", arg).showUsage(true);
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
            },
40261
86a49ba76f52 8136930: Simplify use of module-system options by custom launchers
mchung
parents: 37779
diff changeset
   149
            new Option(true, OptionType.CREATE_UPDATE, "--module-path", "-p") {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   150
                void process(Main jartool, String opt, String arg) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   151
                    String[] dirs = arg.split(File.pathSeparator);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   152
                    Path[] paths = new Path[dirs.length];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   153
                    int i = 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   154
                    for (String dir : dirs) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   155
                        paths[i++] = Paths.get(dir);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   156
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   157
                    jartool.moduleFinder = ModuleFinder.compose(jartool.moduleFinder,
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   158
                                                                ModuleFinder.of(paths));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   159
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   160
            },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   161
            new Option(false, OptionType.CREATE_UPDATE_INDEX, "--no-compress", "-0") {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   162
                void process(Main jartool, String opt, String arg) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   163
                    jartool.flag0 = true;
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
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   167
            // Hidden options
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   168
            new Option(false, OptionType.OTHER, "-P") {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   169
                void process(Main jartool, String opt, String arg) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   170
                    jartool.pflag = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   171
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   172
                boolean isHidden() { return true; }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   173
            },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   174
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   175
            // Other options
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   176
            new Option(true, true, OptionType.OTHER, "--help", "-h") {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   177
                void process(Main jartool, String opt, String arg) throws BadArgs {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   178
                    if (jartool.info == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   179
                        if (arg == null) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   180
                            jartool.info = Main.Info.HELP;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   181
                            return;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   182
                        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   183
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   184
                        if (!arg.equals("compat"))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   185
                            throw new BadArgs("error.illegal.option", arg).showUsage(true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   186
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   187
                        jartool.info = Main.Info.COMPAT_HELP;
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
            },
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   191
            new Option(false, OptionType.OTHER, "--version") {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   192
                void process(Main jartool, String opt, String arg) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   193
                    if (jartool.info == null)
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   194
                        jartool.info = Main.Info.VERSION;
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
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   199
    enum OptionType {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   200
        MAIN_OPERATION("main"),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   201
        ANY("any"),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   202
        CREATE("create"),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   203
        CREATE_UPDATE("create.update"),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   204
        CREATE_UPDATE_INDEX("create.update.index"),
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   205
        OTHER("other");
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   206
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   207
        /** Resource lookup section prefix. */
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   208
        final String name;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   209
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   210
        OptionType(String name) { this.name = name; }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   211
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   212
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   213
    static abstract class Option {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   214
        final boolean hasArg;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   215
        final boolean argIsOptional;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   216
        final String[] aliases;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   217
        final OptionType type;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   218
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   219
        Option(boolean hasArg, OptionType type, String... aliases) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   220
            this(hasArg, false, type, aliases);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   221
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   222
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   223
        Option(boolean hasArg, boolean argIsOptional, OptionType type, String... aliases) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   224
            this.hasArg = hasArg;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   225
            this.argIsOptional = argIsOptional;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   226
            this.type = type;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   227
            this.aliases = aliases;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   228
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   229
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   230
        boolean isHidden() { return false; }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   231
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   232
        boolean matches(String opt) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   233
            for (String a : aliases) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   234
                if (a.equals(opt)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   235
                    return true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   236
                } else if (opt.startsWith("--") && hasArg && opt.startsWith(a + "=")) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   237
                    return true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   238
                } else if (opt.startsWith("--help") && opt.startsWith(a + ":")) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   239
                    return true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   240
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   241
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   242
            return false;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   243
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   244
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   245
        abstract void process(Main jartool, String opt, String arg) throws BadArgs;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   246
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   247
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   248
    static int parseOptions(Main jartool, String[] args) throws BadArgs {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   249
        int count = 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   250
        if (args.length == 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   251
            jartool.info = Main.Info.USAGE_SUMMARY;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   252
            return 0;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   253
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   254
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   255
        // process options
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   256
        for (; count < args.length; count++) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   257
            if (args[count].charAt(0) != '-' || args[count].equals("-C"))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   258
                break;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   259
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   260
            String name = args[count];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   261
            Option option = getOption(name);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   262
            String param = null;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   263
            if (option.hasArg) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   264
                if (name.startsWith("--help")) {  // "special" optional separator
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   265
                    if (name.indexOf(':') > 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   266
                        param = name.substring(name.indexOf(':') + 1, name.length());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   267
                    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   268
                } else if (name.startsWith("--") && name.indexOf('=') > 0) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   269
                    param = name.substring(name.indexOf('=') + 1, name.length());
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   270
                } else if (count + 1 < args.length) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   271
                    param = args[++count];
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   272
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   273
                if (!option.argIsOptional &&
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   274
                    (param == null || param.isEmpty() || param.charAt(0) == '-')) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   275
                    throw new BadArgs("error.missing.arg", name).showUsage(true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   276
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   277
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   278
            option.process(jartool, name, param);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   279
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   280
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   281
        return count;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   282
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   283
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   284
    private static Option getOption(String name) throws BadArgs {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   285
        for (Option o : recognizedOptions) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   286
            if (o.matches(name)) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   287
                return o;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   288
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   289
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   290
        throw new BadArgs("error.unrecognized.option", name).showUsage(true);
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   291
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   292
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 40261
diff changeset
   293
    static void printHelp(PrintWriter out) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   294
        out.format("%s%n", Main.getMsg("main.help.preopt"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   295
        for (OptionType type : OptionType.values()) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   296
            boolean typeHeadingWritten = false;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   297
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   298
            for (Option o : recognizedOptions) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   299
                if (!o.type.equals(type))
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   300
                    continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   301
                String name = o.aliases[0].substring(1); // there must always be at least one name
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   302
                name = name.charAt(0) == '-' ? name.substring(1) : name;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   303
                if (o.isHidden() || name.equals("h")) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   304
                    continue;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   305
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   306
                if (!typeHeadingWritten) {
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   307
                    out.format("%n%s%n", Main.getMsg("main.help.opt." + type.name));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   308
                    typeHeadingWritten = true;
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   309
                }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   310
                out.format("%s%n", Main.getMsg("main.help.opt." + type.name + "." + name));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   311
            }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   312
        }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   313
        out.format("%n%s%n%n", Main.getMsg("main.help.postopt"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   314
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   315
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 40261
diff changeset
   316
    static void printCompatHelp(PrintWriter out) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   317
        out.format("%s%n", Main.getMsg("usage.compat"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   318
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   319
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 40261
diff changeset
   320
    static void printUsageSummary(PrintWriter out) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   321
        out.format("%s%n", Main.getMsg("main.usage.summary"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   322
        out.format("%s%n", Main.getMsg("main.usage.summary.try"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   323
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   324
41484
834b7539ada3 8164689: Retrofit jar, jlink, jmod as a ToolProvider
mchung
parents: 40261
diff changeset
   325
    static void printVersion(PrintWriter out) {
36511
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   326
        out.format("%s %s%n", "jar", System.getProperty("java.version"));
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   327
    }
9d0388c6b336 8142968: Module System implementation
alanb
parents:
diff changeset
   328
}