src/jdk.internal.opt/share/classes/jdk/internal/joptsimple/OptionParser.java
author jlahoda
Wed, 06 Jun 2018 15:36:29 +0200
changeset 50428 8c88df2e8a78
parent 47216 71c04702a3d5
permissions -rw-r--r--
8203891: Upgrade JOpt Simple to 5.0.4 Reviewed-by: alanb, chegar, mchung
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
     1
/*
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
     2
 * Copyright (c) 2009, 2015, Oracle and/or its affiliates. All rights reserved.
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
     4
 *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    10
 *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    15
 * accompanied this code).
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    16
 *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    20
 *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    23
 * questions.
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    24
 */
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    25
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    26
/*
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    27
 * This file is available under and governed by the GNU General Public
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    28
 * License version 2 only, as published by the Free Software Foundation.
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    29
 * However, the following notice accompanied the original version of this
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    30
 * file:
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    31
 *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    32
 * The MIT License
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    33
 *
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
    34
 * Copyright (c) 2004-2015 Paul R. Holser, Jr.
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    35
 *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    36
 * Permission is hereby granted, free of charge, to any person obtaining
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    37
 * a copy of this software and associated documentation files (the
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    38
 * "Software"), to deal in the Software without restriction, including
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    39
 * without limitation the rights to use, copy, modify, merge, publish,
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    40
 * distribute, sublicense, and/or sell copies of the Software, and to
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    41
 * permit persons to whom the Software is furnished to do so, subject to
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    42
 * the following conditions:
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    43
 *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    44
 * The above copyright notice and this permission notice shall be
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    45
 * included in all copies or substantial portions of the Software.
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    46
 *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    47
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    48
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    49
 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    50
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    51
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    52
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    53
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    54
 */
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    55
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    56
package jdk.internal.joptsimple;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    57
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    58
import java.io.IOException;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    59
import java.io.OutputStream;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    60
import java.io.OutputStreamWriter;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    61
import java.io.Writer;
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
    62
import java.util.*;
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
    63
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
    64
import jdk.internal.joptsimple.internal.AbbreviationMap;
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
    65
import jdk.internal.joptsimple.internal.SimpleOptionNameMap;
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
    66
import jdk.internal.joptsimple.internal.OptionNameMap;
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
    67
import jdk.internal.joptsimple.util.KeyValuePair;
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    68
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    69
import static java.util.Collections.*;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    70
import static jdk.internal.joptsimple.OptionException.*;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    71
import static jdk.internal.joptsimple.OptionParserState.*;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    72
import static jdk.internal.joptsimple.ParserRules.*;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    73
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    74
/**
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    75
 * <p>Parses command line arguments, using a syntax that attempts to take from the best of POSIX {@code getopt()}
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    76
 * and GNU {@code getopt_long()}.</p>
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    77
 *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    78
 * <p>This parser supports short options and long options.</p>
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    79
 *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    80
 * <ul>
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
    81
 *   <li><dfn>Short options</dfn> begin with a single hyphen ("{@code -}") followed by a single letter or digit,
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
    82
 *   or question mark ("{@code ?}"), or dot ("{@code .}"), or underscore ("{@code _}").</li>
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    83
 *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    84
 *   <li>Short options can accept single arguments. The argument can be made required or optional. The option's
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    85
 *   argument can occur:
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    86
 *     <ul>
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
    87
 *       <li>in the slot after the option, as in {@code -d /tmp}</li>
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
    88
 *       <li>right up against the option, as in {@code -d/tmp}</li>
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
    89
 *       <li>right up against the option separated by an equals sign ({@code "="}), as in {@code -d=/tmp}</li>
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    90
 *     </ul>
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    91
 *   To specify <em>n</em> arguments for an option, specify the option <em>n</em> times, once for each argument,
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
    92
 *   as in {@code -d /tmp -d /var -d /opt}; or, when using the
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    93
 *   {@linkplain ArgumentAcceptingOptionSpec#withValuesSeparatedBy(char) "separated values"} clause of the "fluent
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    94
 *   interface" (see below), give multiple values separated by a given character as a single argument to the
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    95
 *   option.</li>
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    96
 *
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
    97
 *   <li>Short options can be clustered, so that {@code -abc} is treated as {@code -a -b -c}. If a short option
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    98
 *   in the cluster can accept an argument, the remaining characters are interpreted as the argument for that
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
    99
 *   option.</li>
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   100
 *
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   101
 *   <li>An argument consisting only of two hyphens ({@code "--"}) signals that the remaining arguments are to be
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   102
 *   treated as non-options.</li>
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   103
 *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   104
 *   <li>An argument consisting only of a single hyphen is considered a non-option argument (though it can be an
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   105
 *   argument of an option). Many Unix programs treat single hyphens as stand-ins for the standard input or standard
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   106
 *   output streams.</li>
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   107
 *
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   108
 *   <li><dfn>Long options</dfn> begin with two hyphens ({@code "--"}), followed by multiple letters, digits,
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   109
 *   hyphens, question marks, or dots. A hyphen cannot be the first character of a long option specification when
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   110
 *   configuring the parser.</li>
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   111
 *
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   112
 *   <li>You can abbreviate long options, so long as the abbreviation is unique. Suppress this behavior if
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   113
 *   you wish using {@linkplain OptionParser#OptionParser(boolean) this constructor}.</li>
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   114
 *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   115
 *   <li>Long options can accept single arguments.  The argument can be made required or optional.  The option's
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   116
 *   argument can occur:
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   117
 *     <ul>
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   118
 *       <li>in the slot after the option, as in {@code --directory /tmp}</li>
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   119
 *       <li>right up against the option separated by an equals sign ({@code "="}), as in
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   120
 *       {@code --directory=/tmp}
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   121
 *     </ul>
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   122
 *   Specify multiple arguments for a long option in the same manner as for short options (see above).</li>
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   123
 *
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   124
 *   <li>You can use a single hyphen ({@code "-"}) instead of a double hyphen ({@code "--"}) for a long
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   125
 *   option.</li>
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   126
 *
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   127
 *   <li>The option {@code -W} is reserved.  If you tell the parser to {@linkplain
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   128
 *   #recognizeAlternativeLongOptions(boolean) recognize alternative long options}, then it will treat, for example,
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   129
 *   {@code -W foo=bar} as the long option {@code foo} with argument {@code bar}, as though you had written
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   130
 *   {@code --foo=bar}.</li>
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   131
 *
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   132
 *   <li>You can specify {@code -W} as a valid short option, or use it as an abbreviation for a long option, but
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   133
 *   {@linkplain #recognizeAlternativeLongOptions(boolean) recognizing alternative long options} will always supersede
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   134
 *   this behavior.</li>
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   135
 *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   136
 *   <li>You can specify a given short or long option multiple times on a single command line. The parser collects
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   137
 *   any arguments specified for those options as a list.</li>
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   138
 *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   139
 *   <li>If the parser detects an option whose argument is optional, and the next argument "looks like" an option,
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   140
 *   that argument is not treated as the argument to the option, but as a potentially valid option. If, on the other
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   141
 *   hand, the optional argument is typed as a derivative of {@link Number}, then that argument is treated as the
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   142
 *   negative number argument of the option, even if the parser recognizes the corresponding numeric option.
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   143
 *   For example:
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   144
 *   <pre><code>
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   145
 *     OptionParser parser = new OptionParser();
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   146
 *     parser.accepts( "a" ).withOptionalArg().ofType( Integer.class );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   147
 *     parser.accepts( "2" );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   148
 *     OptionSet options = parser.parse( "-a", "-2" );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   149
 *   </code></pre>
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   150
 *   In this case, the option set contains {@code "a"} with argument {@code -2}, not both {@code "a"} and
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   151
 *   {@code "2"}. Swapping the elements in the <em>args</em> array gives the latter.</li>
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   152
 * </ul>
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   153
 *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   154
 * <p>There are two ways to tell the parser what options to recognize:</p>
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   155
 *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   156
 * <ol>
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   157
 *   <li>A "fluent interface"-style API for specifying options, available since version 2. Sentences in this fluent
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   158
 *   interface language begin with a call to {@link #accepts(String) accepts} or {@link #acceptsAll(List)
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   159
 *   acceptsAll} methods; calls on the ensuing chain of objects describe whether the options can take an argument,
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   160
 *   whether the argument is required or optional, to what type arguments of the options should be converted if any,
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   161
 *   etc. Since version 3, these calls return an instance of {@link OptionSpec}, which can subsequently be used to
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   162
 *   retrieve the arguments of the associated option in a type-safe manner.</li>
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   163
 *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   164
 *   <li>Since version 1, a more concise way of specifying short options has been to use the special {@linkplain
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   165
 *   #OptionParser(String) constructor}. Arguments of options specified in this manner will be of type {@link String}.
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   166
 *   Here are the rules for the format of the specification strings this constructor accepts:
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   167
 *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   168
 *     <ul>
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   169
 *       <li>Any letter or digit is treated as an option character.</li>
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   170
 *
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   171
 *       <li>An option character can be immediately followed by an asterisk ({@code *)} to indicate that
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   172
 *       the option is a "help" option.</li>
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   173
 *
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   174
 *       <li>If an option character (with possible trailing asterisk) is followed by a single colon ({@code ":"}),
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   175
 *       then the option requires an argument.</li>
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   176
 *
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   177
 *       <li>If an option character (with possible trailing asterisk) is followed by two colons ({@code "::"}),
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   178
 *       then the option accepts an optional argument.</li>
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   179
 *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   180
 *       <li>Otherwise, the option character accepts no argument.</li>
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   181
 *
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   182
 *       <li>If the option specification string begins with a plus sign ({@code "+" }), the parser will behave
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   183
 *       "POSIX-ly correct".</li>
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   184
 *
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   185
 *       <li>If the option specification string contains the sequence {@code "W;"} (capital W followed by a
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   186
 *       semicolon), the parser will recognize the alternative form of long options.</li>
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   187
 *     </ul>
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   188
 *   </li>
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   189
 * </ol>
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   190
 *
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   191
 * <p>Each of the options in a list of options given to {@link #acceptsAll(List) acceptsAll} is treated as a
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   192
 * synonym of the others.  For example:</p>
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   193
 *   <pre>
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   194
 *     <code>
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   195
 *     OptionParser parser = new OptionParser();
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   196
 *     parser.acceptsAll( asList( "w", "interactive", "confirmation" ) );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   197
 *     OptionSet options = parser.parse( "-w" );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   198
 *     </code>
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   199
 *   </pre>
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   200
 * <p>In this case, <code>options.{@link OptionSet#has(String) has}</code> would answer {@code true} when given arguments
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   201
 * {@code "w"}, {@code "interactive"}, and {@code "confirmation"}. The {@link OptionSet} would give the same
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   202
 * responses to these arguments for its other methods as well.</p>
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   203
 *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   204
 * <p>By default, as with GNU {@code getopt()}, the parser allows intermixing of options and non-options. If, however,
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   205
 * the parser has been created to be "POSIX-ly correct", then the first argument that does not look lexically like an
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   206
 * option, and is not a required argument of a preceding option, signals the end of options. You can still bind
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   207
 * optional arguments to their options using the abutting (for short options) or {@code =} syntax.</p>
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   208
 *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   209
 * <p>Unlike GNU {@code getopt()}, this parser does not honor the environment variable {@code POSIXLY_CORRECT}.
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   210
 * "POSIX-ly correct" parsers are configured by either:</p>
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   211
 *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   212
 * <ol>
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   213
 *   <li>using the method {@link #posixlyCorrect(boolean)}, or</li>
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   214
 *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   215
 *   <li>using the {@linkplain #OptionParser(String) constructor} with an argument whose first character is a plus sign
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   216
 *   ({@code "+"})</li>
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   217
 * </ol>
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   218
 *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   219
 * @author <a href="mailto:pholser@alumni.rice.edu">Paul Holser</a>
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   220
 * @see <a href="http://www.gnu.org/software/libc/manual">The GNU C Library</a>
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   221
 */
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   222
public class OptionParser implements OptionDeclarer {
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   223
    private final OptionNameMap<AbstractOptionSpec<?>> recognizedOptions;
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   224
    private final ArrayList<AbstractOptionSpec<?>> trainingOrder;
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   225
    private final Map<List<String>, Set<OptionSpec<?>>> requiredIf;
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   226
    private final Map<List<String>, Set<OptionSpec<?>>> requiredUnless;
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   227
    private final Map<List<String>, Set<OptionSpec<?>>> availableIf;
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   228
    private final Map<List<String>, Set<OptionSpec<?>>> availableUnless;
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   229
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   230
    private OptionParserState state;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   231
    private boolean posixlyCorrect;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   232
    private boolean allowsUnrecognizedOptions;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   233
    private HelpFormatter helpFormatter = new BuiltinHelpFormatter();
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   234
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   235
    /**
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   236
     * Creates an option parser that initially recognizes no options, and does not exhibit "POSIX-ly correct"
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   237
     * behavior.
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   238
     */
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   239
    public OptionParser() {
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   240
        this(true);
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   241
    }
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   242
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   243
    /**
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   244
     * Creates an option parser that initially recognizes no options, and does not exhibit "POSIX-ly correct"
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   245
     * behavior.
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   246
     *
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   247
     * @param allowAbbreviations whether unambiguous abbreviations of long options should be recognized
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   248
     * by the parser
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   249
     */
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   250
    public OptionParser( boolean allowAbbreviations ) {
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   251
        trainingOrder = new ArrayList<>();
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   252
        requiredIf = new HashMap<>();
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   253
        requiredUnless = new HashMap<>();
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   254
        availableIf = new HashMap<>();
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   255
        availableUnless = new HashMap<>();
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   256
        state = moreOptions( false );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   257
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   258
        recognizedOptions = allowAbbreviations
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   259
            ? new AbbreviationMap<AbstractOptionSpec<?>>()
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   260
            : new SimpleOptionNameMap<AbstractOptionSpec<?>>();
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   261
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   262
        recognize( new NonOptionArgumentSpec<String>() );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   263
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   264
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   265
    /**
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   266
     * Creates an option parser and configures it to recognize the short options specified in the given string.
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   267
     *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   268
     * Arguments of options specified this way will be of type {@link String}.
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   269
     *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   270
     * @param optionSpecification an option specification
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   271
     * @throws NullPointerException if {@code optionSpecification} is {@code null}
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   272
     * @throws OptionException if the option specification contains illegal characters or otherwise cannot be
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   273
     * recognized
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   274
     */
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   275
    public OptionParser( String optionSpecification ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   276
        this();
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   277
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   278
        new OptionSpecTokenizer( optionSpecification ).configure( this );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   279
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   280
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   281
    public OptionSpecBuilder accepts( String option ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   282
        return acceptsAll( singletonList( option ) );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   283
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   284
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   285
    public OptionSpecBuilder accepts( String option, String description ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   286
        return acceptsAll( singletonList( option ), description );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   287
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   288
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   289
    public OptionSpecBuilder acceptsAll( List<String> options ) {
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   290
        return acceptsAll( options, "" );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   291
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   292
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   293
    public OptionSpecBuilder acceptsAll( List<String> options, String description ) {
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   294
        if ( options.isEmpty() )
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   295
            throw new IllegalArgumentException( "need at least one option" );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   296
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   297
        ensureLegalOptions( options );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   298
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   299
        return new OptionSpecBuilder( this, options, description );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   300
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   301
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   302
    public NonOptionArgumentSpec<String> nonOptions() {
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   303
        NonOptionArgumentSpec<String> spec = new NonOptionArgumentSpec<>();
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   304
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   305
        recognize( spec );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   306
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   307
        return spec;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   308
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   309
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   310
    public NonOptionArgumentSpec<String> nonOptions( String description ) {
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   311
        NonOptionArgumentSpec<String> spec = new NonOptionArgumentSpec<>( description );
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   312
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   313
        recognize( spec );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   314
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   315
        return spec;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   316
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   317
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   318
    public void posixlyCorrect( boolean setting ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   319
        posixlyCorrect = setting;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   320
        state = moreOptions( setting );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   321
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   322
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   323
    boolean posixlyCorrect() {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   324
        return posixlyCorrect;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   325
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   326
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   327
    public void allowsUnrecognizedOptions() {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   328
        allowsUnrecognizedOptions = true;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   329
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   330
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   331
    boolean doesAllowsUnrecognizedOptions() {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   332
        return allowsUnrecognizedOptions;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   333
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   334
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   335
    public void recognizeAlternativeLongOptions( boolean recognize ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   336
        if ( recognize )
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   337
            recognize( new AlternativeLongOptionSpec() );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   338
        else
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   339
            recognizedOptions.remove( String.valueOf( RESERVED_FOR_EXTENSIONS ) );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   340
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   341
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   342
    void recognize( AbstractOptionSpec<?> spec ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   343
        recognizedOptions.putAll( spec.options(), spec );
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   344
        trainingOrder.add( spec );
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   345
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   346
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   347
    /**
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   348
     * Writes information about the options this parser recognizes to the given output sink.
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   349
     *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   350
     * The output sink is flushed, but not closed.
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   351
     *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   352
     * @param sink the sink to write information to
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   353
     * @throws IOException if there is a problem writing to the sink
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   354
     * @throws NullPointerException if {@code sink} is {@code null}
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   355
     * @see #printHelpOn(Writer)
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   356
     */
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   357
    public void printHelpOn( OutputStream sink ) throws IOException {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   358
        printHelpOn( new OutputStreamWriter( sink ) );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   359
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   360
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   361
    /**
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   362
     * Writes information about the options this parser recognizes to the given output sink.
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   363
     *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   364
     * The output sink is flushed, but not closed.
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   365
     *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   366
     * @param sink the sink to write information to
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   367
     * @throws IOException if there is a problem writing to the sink
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   368
     * @throws NullPointerException if {@code sink} is {@code null}
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   369
     * @see #printHelpOn(OutputStream)
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   370
     */
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   371
    public void printHelpOn( Writer sink ) throws IOException {
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   372
        sink.write( helpFormatter.format( _recognizedOptions() ) );
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   373
        sink.flush();
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   374
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   375
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   376
    /**
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   377
     * Tells the parser to use the given formatter when asked to {@linkplain #printHelpOn(java.io.Writer) print help}.
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   378
     *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   379
     * @param formatter the formatter to use for printing help
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   380
     * @throws NullPointerException if the formatter is {@code null}
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   381
     */
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   382
    public void formatHelpWith( HelpFormatter formatter ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   383
        if ( formatter == null )
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   384
            throw new NullPointerException();
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   385
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   386
        helpFormatter = formatter;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   387
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   388
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   389
    /**
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   390
     * Retrieves all options-spec pairings which have been configured for the parser in the same order as declared
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   391
     * during training. Option flags for specs are alphabetized by {@link OptionSpec#options()}; only the order of the
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   392
     * specs is preserved.
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   393
     *
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   394
     * (Note: prior to 4.7 the order was alphabetical across all options regardless of spec.)
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   395
     *
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   396
     * @return a map containing all the configured options and their corresponding {@link OptionSpec}
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   397
     * @since 4.6
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   398
     */
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   399
    public Map<String, OptionSpec<?>> recognizedOptions() {
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   400
        return new LinkedHashMap<String, OptionSpec<?>>( _recognizedOptions() );
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   401
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   402
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   403
    private Map<String, AbstractOptionSpec<?>> _recognizedOptions() {
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   404
        Map<String, AbstractOptionSpec<?>> options = new LinkedHashMap<>();
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   405
        for ( AbstractOptionSpec<?> spec : trainingOrder ) {
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   406
            for ( String option : spec.options() )
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   407
                options.put( option, spec );
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   408
        }
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   409
        return options;
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   410
    }
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   411
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   412
   /**
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   413
     * Parses the given command line arguments according to the option specifications given to the parser.
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   414
     *
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   415
     * @param arguments arguments to parse
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   416
     * @return an {@link OptionSet} describing the parsed options, their arguments, and any non-option arguments found
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   417
     * @throws OptionException if problems are detected while parsing
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   418
     * @throws NullPointerException if the argument list is {@code null}
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   419
     */
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   420
    public OptionSet parse( String... arguments ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   421
        ArgumentList argumentList = new ArgumentList( arguments );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   422
        OptionSet detected = new OptionSet( recognizedOptions.toJavaUtilMap() );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   423
        detected.add( recognizedOptions.get( NonOptionArgumentSpec.NAME ) );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   424
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   425
        while ( argumentList.hasMore() )
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   426
            state.handleArgument( this, argumentList, detected );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   427
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   428
        reset();
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   429
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   430
        ensureRequiredOptions( detected );
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   431
        ensureAllowedOptions( detected );
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   432
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   433
        return detected;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   434
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   435
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   436
    /**
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   437
     * Mandates mutual exclusiveness for the options built by the specified builders.
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   438
     *
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   439
     * @param specs descriptors for options that should be mutually exclusive on a command line.
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   440
     * @throws NullPointerException if {@code specs} is {@code null}
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   441
     */
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   442
    public void mutuallyExclusive( OptionSpecBuilder... specs ) {
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   443
        for ( int i = 0; i < specs.length; i++ ) {
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   444
            for ( int j = 0; j < specs.length; j++ ) {
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   445
                if ( i != j )
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   446
                    specs[i].availableUnless( specs[j] );
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   447
            }
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   448
        }
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   449
    }
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   450
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   451
    private void ensureRequiredOptions( OptionSet options ) {
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   452
        List<AbstractOptionSpec<?>> missingRequiredOptions = missingRequiredOptions(options);
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   453
        boolean helpOptionPresent = isHelpOptionPresent( options );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   454
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   455
        if ( !missingRequiredOptions.isEmpty() && !helpOptionPresent )
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   456
            throw new MissingRequiredOptionsException( missingRequiredOptions );
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   457
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   458
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   459
    private void ensureAllowedOptions( OptionSet options ) {
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   460
        List<AbstractOptionSpec<?>> forbiddenOptions = unavailableOptions( options );
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   461
        boolean helpOptionPresent = isHelpOptionPresent( options );
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   462
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   463
        if ( !forbiddenOptions.isEmpty() && !helpOptionPresent )
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   464
            throw new UnavailableOptionException( forbiddenOptions );
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   465
    }
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   466
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   467
    private List<AbstractOptionSpec<?>> missingRequiredOptions( OptionSet options ) {
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   468
        List<AbstractOptionSpec<?>> missingRequiredOptions = new ArrayList<>();
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   469
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   470
        for ( AbstractOptionSpec<?> each : recognizedOptions.toJavaUtilMap().values() ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   471
            if ( each.isRequired() && !options.has( each ) )
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   472
                missingRequiredOptions.add(each);
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   473
        }
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   474
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   475
        for ( Map.Entry<List<String>, Set<OptionSpec<?>>> each : requiredIf.entrySet() ) {
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   476
            AbstractOptionSpec<?> required = specFor( each.getKey().iterator().next() );
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   477
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   478
            if ( optionsHasAnyOf( options, each.getValue() ) && !options.has( required ) )
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   479
                missingRequiredOptions.add( required );
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   480
        }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   481
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   482
        for ( Map.Entry<List<String>, Set<OptionSpec<?>>> each : requiredUnless.entrySet() ) {
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   483
            AbstractOptionSpec<?> required = specFor(each.getKey().iterator().next());
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   484
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   485
            if ( !optionsHasAnyOf( options, each.getValue() ) && !options.has( required ) )
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   486
                missingRequiredOptions.add( required );
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   487
        }
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   488
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   489
        return missingRequiredOptions;
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   490
    }
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   491
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   492
    private List<AbstractOptionSpec<?>> unavailableOptions(OptionSet options) {
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   493
        List<AbstractOptionSpec<?>> unavailableOptions = new ArrayList<>();
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   494
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   495
        for ( Map.Entry<List<String>, Set<OptionSpec<?>>> eachEntry : availableIf.entrySet() ) {
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   496
            AbstractOptionSpec<?> forbidden = specFor( eachEntry.getKey().iterator().next() );
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   497
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   498
            if ( !optionsHasAnyOf( options, eachEntry.getValue() ) && options.has( forbidden ) ) {
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   499
                unavailableOptions.add(forbidden);
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   500
            }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   501
        }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   502
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   503
        for ( Map.Entry<List<String>, Set<OptionSpec<?>>> eachEntry : availableUnless.entrySet() ) {
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   504
            AbstractOptionSpec<?> forbidden = specFor( eachEntry.getKey().iterator().next() );
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   505
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   506
            if ( optionsHasAnyOf( options, eachEntry.getValue() ) && options.has( forbidden ) ) {
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   507
                unavailableOptions.add(forbidden);
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   508
            }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   509
        }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   510
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   511
        return unavailableOptions;
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   512
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   513
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   514
    private boolean optionsHasAnyOf( OptionSet options, Collection<OptionSpec<?>> specs ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   515
        for ( OptionSpec<?> each : specs ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   516
            if ( options.has( each ) )
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   517
                return true;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   518
        }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   519
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   520
        return false;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   521
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   522
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   523
    private boolean isHelpOptionPresent( OptionSet options ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   524
        boolean helpOptionPresent = false;
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   525
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   526
        for ( AbstractOptionSpec<?> each : recognizedOptions.toJavaUtilMap().values() ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   527
            if ( each.isForHelp() && options.has( each ) ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   528
                helpOptionPresent = true;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   529
                break;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   530
            }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   531
        }
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   532
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   533
        return helpOptionPresent;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   534
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   535
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   536
    void handleLongOptionToken( String candidate, ArgumentList arguments, OptionSet detected ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   537
        KeyValuePair optionAndArgument = parseLongOptionWithArgument( candidate );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   538
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   539
        if ( !isRecognized( optionAndArgument.key ) )
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   540
            throw unrecognizedOption( optionAndArgument.key );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   541
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   542
        AbstractOptionSpec<?> optionSpec = specFor( optionAndArgument.key );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   543
        optionSpec.handleOption( this, arguments, detected, optionAndArgument.value );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   544
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   545
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   546
    void handleShortOptionToken( String candidate, ArgumentList arguments, OptionSet detected ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   547
        KeyValuePair optionAndArgument = parseShortOptionWithArgument( candidate );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   548
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   549
        if ( isRecognized( optionAndArgument.key ) ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   550
            specFor( optionAndArgument.key ).handleOption( this, arguments, detected, optionAndArgument.value );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   551
        }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   552
        else
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   553
            handleShortOptionCluster( candidate, arguments, detected );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   554
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   555
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   556
    private void handleShortOptionCluster( String candidate, ArgumentList arguments, OptionSet detected ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   557
        char[] options = extractShortOptionsFrom( candidate );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   558
        validateOptionCharacters( options );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   559
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   560
        for ( int i = 0; i < options.length; i++ ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   561
            AbstractOptionSpec<?> optionSpec = specFor( options[ i ] );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   562
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   563
            if ( optionSpec.acceptsArguments() && options.length > i + 1 ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   564
                String detectedArgument = String.valueOf( options, i + 1, options.length - 1 - i );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   565
                optionSpec.handleOption( this, arguments, detected, detectedArgument );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   566
                break;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   567
            }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   568
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   569
            optionSpec.handleOption( this, arguments, detected, null );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   570
        }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   571
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   572
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   573
    void handleNonOptionArgument( String candidate, ArgumentList arguments, OptionSet detectedOptions ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   574
        specFor( NonOptionArgumentSpec.NAME ).handleOption( this, arguments, detectedOptions, candidate );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   575
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   576
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   577
    void noMoreOptions() {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   578
        state = OptionParserState.noMoreOptions();
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   579
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   580
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   581
    boolean looksLikeAnOption( String argument ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   582
        return isShortOptionToken( argument ) || isLongOptionToken( argument );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   583
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   584
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   585
    boolean isRecognized( String option ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   586
        return recognizedOptions.contains( option );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   587
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   588
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   589
    void requiredIf( List<String> precedentSynonyms, String required ) {
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   590
        requiredIf( precedentSynonyms, specFor( required ) );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   591
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   592
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   593
    void requiredIf( List<String> precedentSynonyms, OptionSpec<?> required ) {
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   594
        putDependentOption( precedentSynonyms, required, requiredIf );
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   595
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   596
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   597
    void requiredUnless( List<String> precedentSynonyms, String required ) {
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   598
        requiredUnless( precedentSynonyms, specFor( required ) );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   599
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   600
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   601
    void requiredUnless( List<String> precedentSynonyms, OptionSpec<?> required ) {
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   602
        putDependentOption( precedentSynonyms, required, requiredUnless );
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   603
    }
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   604
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   605
    void availableIf( List<String> precedentSynonyms, String available ) {
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   606
        availableIf( precedentSynonyms, specFor( available ) );
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   607
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   608
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   609
    void availableIf( List<String> precedentSynonyms, OptionSpec<?> available) {
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   610
        putDependentOption( precedentSynonyms, available, availableIf );
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   611
    }
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   612
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   613
    void availableUnless( List<String> precedentSynonyms, String available ) {
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   614
        availableUnless( precedentSynonyms, specFor( available ) );
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   615
    }
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   616
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   617
    void availableUnless( List<String> precedentSynonyms, OptionSpec<?> available ) {
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   618
        putDependentOption( precedentSynonyms, available, availableUnless );
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   619
    }
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   620
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   621
    private void putDependentOption( List<String> precedentSynonyms, OptionSpec<?> required,
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   622
        Map<List<String>, Set<OptionSpec<?>>> target ) {
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   623
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   624
        for ( String each : precedentSynonyms ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   625
            AbstractOptionSpec<?> spec = specFor( each );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   626
            if ( spec == null )
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   627
                throw new UnconfiguredOptionException( precedentSynonyms );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   628
        }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   629
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   630
        Set<OptionSpec<?>> associated = target.get( precedentSynonyms );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   631
        if ( associated == null ) {
50428
8c88df2e8a78 8203891: Upgrade JOpt Simple to 5.0.4
jlahoda
parents: 47216
diff changeset
   632
            associated = new HashSet<>();
35377
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   633
            target.put( precedentSynonyms, associated );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   634
        }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   635
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   636
        associated.add( required );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   637
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   638
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   639
    private AbstractOptionSpec<?> specFor( char option ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   640
        return specFor( String.valueOf( option ) );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   641
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   642
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   643
    private AbstractOptionSpec<?> specFor( String option ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   644
        return recognizedOptions.get( option );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   645
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   646
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   647
    private void reset() {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   648
        state = moreOptions( posixlyCorrect );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   649
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   650
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   651
    private static char[] extractShortOptionsFrom( String argument ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   652
        char[] options = new char[ argument.length() - 1 ];
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   653
        argument.getChars( 1, argument.length(), options, 0 );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   654
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   655
        return options;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   656
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   657
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   658
    private void validateOptionCharacters( char[] options ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   659
        for ( char each : options ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   660
            String option = String.valueOf( each );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   661
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   662
            if ( !isRecognized( option ) )
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   663
                throw unrecognizedOption( option );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   664
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   665
            if ( specFor( option ).acceptsArguments() )
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   666
                return;
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   667
        }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   668
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   669
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   670
    private static KeyValuePair parseLongOptionWithArgument( String argument ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   671
        return KeyValuePair.valueOf( argument.substring( 2 ) );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   672
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   673
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   674
    private static KeyValuePair parseShortOptionWithArgument( String argument ) {
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   675
        return KeyValuePair.valueOf( argument.substring( 1 ) );
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   676
    }
462f93ab37f2 8148154: Integrate JOpt Simple for internal usage by JDK tools
chegar
parents:
diff changeset
   677
}