001/*
002  Licensed to the Apache Software Foundation (ASF) under one or more
003  contributor license agreements.  See the NOTICE file distributed with
004  this work for additional information regarding copyright ownership.
005  The ASF licenses this file to You under the Apache License, Version 2.0
006  (the "License"); you may not use this file except in compliance with
007  the License.  You may obtain a copy of the License at
008
009      http://www.apache.org/licenses/LICENSE-2.0
010
011  Unless required by applicable law or agreed to in writing, software
012  distributed under the License is distributed on an "AS IS" BASIS,
013  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014  See the License for the specific language governing permissions and
015  limitations under the License.
016 */
017
018package org.apache.commons.cli;
019
020import java.io.Serializable;
021import java.util.ArrayList;
022import java.util.Collection;
023import java.util.Collections;
024import java.util.HashSet;
025import java.util.LinkedHashMap;
026import java.util.List;
027import java.util.Map;
028
029/**
030 * Main entry-point into the library.
031 * <p>
032 * Options represents a collection of {@link Option} objects, which describe the possible options for a command-line.
033 * <p>
034 * It may flexibly parse long and short options, with or without values. Additionally, it may parse only a portion of a
035 * commandline, allowing for flexible multi-stage parsing.
036 *
037 * @see org.apache.commons.cli.CommandLine
038 */
039public class Options implements Serializable {
040    /** The serial version UID. */
041    private static final long serialVersionUID = 1L;
042
043    /** a map of the options with the character key */
044    private final Map<String, Option> shortOpts = new LinkedHashMap<>();
045
046    /** a map of the options with the long key */
047    private final Map<String, Option> longOpts = new LinkedHashMap<>();
048
049    /** a map of the required options */
050    // N.B. This can contain either a String (addOption) or an OptionGroup (addOptionGroup)
051    // TODO this seems wrong
052    private final List<Object> requiredOpts = new ArrayList<>();
053
054    /** a map of the option groups */
055    private final Map<String, OptionGroup> optionGroups = new LinkedHashMap<>();
056
057    /**
058     * Adds an option instance
059     *
060     * @param opt the option that is to be added
061     * @return the resulting Options instance
062     */
063    public Options addOption(final Option opt) {
064        final String key = opt.getKey();
065
066        // add it to the long option list
067        if (opt.hasLongOpt()) {
068            longOpts.put(opt.getLongOpt(), opt);
069        }
070
071        // if the option is required add it to the required list
072        if (opt.isRequired()) {
073            if (requiredOpts.contains(key)) {
074                requiredOpts.remove(requiredOpts.indexOf(key));
075            }
076            requiredOpts.add(key);
077        }
078
079        shortOpts.put(key, opt);
080
081        return this;
082    }
083
084    /**
085     * Add an option that only contains a short-name.
086     *
087     * <p>
088     * It may be specified as requiring an argument.
089     * </p>
090     *
091     * @param opt Short single-character name of the option.
092     * @param hasArg flag signalling if an argument is required after this option
093     * @param description Self-documenting description
094     * @return the resulting Options instance
095     */
096    public Options addOption(final String opt, final boolean hasArg, final String description) {
097        addOption(opt, null, hasArg, description);
098        return this;
099    }
100
101    /**
102     * Add an option that only contains a short name.
103     *
104     * <p>
105     * The option does not take an argument.
106     * </p>
107     *
108     * @param opt Short single-character name of the option.
109     * @param description Self-documenting description
110     * @return the resulting Options instance
111     * @since 1.3
112     */
113    public Options addOption(final String opt, final String description) {
114        addOption(opt, null, false, description);
115        return this;
116    }
117
118    /**
119     * Add an option that contains a short-name and a long-name.
120     *
121     * <p>
122     * It may be specified as requiring an argument.
123     * </p>
124     *
125     * @param opt Short single-character name of the option.
126     * @param longOpt Long multi-character name of the option.
127     * @param hasArg flag signalling if an argument is required after this option
128     * @param description Self-documenting description
129     * @return the resulting Options instance
130     */
131    public Options addOption(final String opt, final String longOpt, final boolean hasArg, final String description) {
132        addOption(new Option(opt, longOpt, hasArg, description));
133        return this;
134    }
135
136    /**
137     * Add the specified option group.
138     *
139     * @param group the OptionGroup that is to be added
140     * @return the resulting Options instance
141     */
142    public Options addOptionGroup(final OptionGroup group) {
143        if (group.isRequired()) {
144            requiredOpts.add(group);
145        }
146
147        for (final Option option : group.getOptions()) {
148            // an Option cannot be required if it is in an
149            // OptionGroup, either the group is required or
150            // nothing is required
151            option.setRequired(false);
152            addOption(option);
153
154            optionGroups.put(option.getKey(), group);
155        }
156
157        return this;
158    }
159
160    /**
161     * Add an option that contains a short-name and a long-name.
162     *
163     * <p>
164     * The added option is set as required. It may be specified as requiring an argument. This method is a shortcut for:
165     * </p>
166     *
167     * <pre>
168     * <code>
169     * Options option = new Option(opt, longOpt, hasArg, description);
170     * option.setRequired(true);
171     * options.add(option);
172     * </code>
173     * </pre>
174     *
175     * @param opt Short single-character name of the option.
176     * @param longOpt Long multi-character name of the option.
177     * @param hasArg flag signalling if an argument is required after this option
178     * @param description Self-documenting description
179     * @return the resulting Options instance
180     * @since 1.4
181     */
182    public Options addRequiredOption(final String opt, final String longOpt, final boolean hasArg, final String description) {
183        final Option option = new Option(opt, longOpt, hasArg, description);
184        option.setRequired(true);
185        addOption(option);
186        return this;
187    }
188
189    /**
190     * Gets the options with a long name starting with the name specified.
191     *
192     * @param opt the partial name of the option
193     * @return the options matching the partial name specified, or an empty list if none matches
194     * @since 1.3
195     */
196    public List<String> getMatchingOptions(String opt) {
197        opt = Util.stripLeadingHyphens(opt);
198
199        final List<String> matchingOpts = new ArrayList<>();
200
201        // for a perfect match return the single option only
202        if (longOpts.containsKey(opt)) {
203            return Collections.singletonList(opt);
204        }
205
206        for (final String longOpt : longOpts.keySet()) {
207            if (longOpt.startsWith(opt)) {
208                matchingOpts.add(longOpt);
209            }
210        }
211
212        return matchingOpts;
213    }
214
215    /**
216     * Gets the {@link Option} matching the long or short name specified.
217     *
218     * <p>
219     * The leading hyphens in the name are ignored (up to 2).
220     * </p>
221     *
222     * @param opt short or long name of the {@link Option}
223     * @return the option represented by opt
224     */
225    public Option getOption(String opt) {
226        opt = Util.stripLeadingHyphens(opt);
227
228        final Option option = shortOpts.get(opt);
229        return option != null ? option : longOpts.get(opt);
230    }
231
232    /**
233     * Gets the OptionGroup the {@code opt} belongs to.
234     *
235     * @param opt the option whose OptionGroup is being queried.
236     * @return the OptionGroup if {@code opt} is part of an OptionGroup, otherwise return null
237     */
238    public OptionGroup getOptionGroup(final Option opt) {
239        return optionGroups.get(opt.getKey());
240    }
241
242    /**
243     * Gets the OptionGroups that are members of this Options instance.
244     *
245     * @return a Collection of OptionGroup instances.
246     */
247    Collection<OptionGroup> getOptionGroups() {
248        return new HashSet<>(optionGroups.values());
249    }
250
251    /**
252     * Gets a read-only list of options in this set
253     *
254     * @return read-only Collection of {@link Option} objects in this descriptor
255     */
256    public Collection<Option> getOptions() {
257        return Collections.unmodifiableCollection(helpOptions());
258    }
259
260    /**
261     * Gets the required options.
262     *
263     * @return read-only List of required options
264     */
265    public List getRequiredOptions() {
266        return Collections.unmodifiableList(requiredOpts);
267    }
268
269    /**
270     * Returns whether the named {@link Option} is a member of this {@link Options}.
271     *
272     * @param opt long name of the {@link Option}
273     * @return true if the named {@link Option} is a member of this {@link Options}
274     * @since 1.3
275     */
276    public boolean hasLongOption(String opt) {
277        opt = Util.stripLeadingHyphens(opt);
278
279        return longOpts.containsKey(opt);
280    }
281
282    /**
283     * Returns whether the named {@link Option} is a member of this {@link Options}.
284     *
285     * @param opt short or long name of the {@link Option}
286     * @return true if the named {@link Option} is a member of this {@link Options}
287     */
288    public boolean hasOption(String opt) {
289        opt = Util.stripLeadingHyphens(opt);
290
291        return shortOpts.containsKey(opt) || longOpts.containsKey(opt);
292    }
293
294    /**
295     * Returns whether the named {@link Option} is a member of this {@link Options}.
296     *
297     * @param opt short name of the {@link Option}
298     * @return true if the named {@link Option} is a member of this {@link Options}
299     * @since 1.3
300     */
301    public boolean hasShortOption(String opt) {
302        opt = Util.stripLeadingHyphens(opt);
303
304        return shortOpts.containsKey(opt);
305    }
306
307    /**
308     * Returns the Options for use by the HelpFormatter.
309     *
310     * @return the List of Options
311     */
312    List<Option> helpOptions() {
313        return new ArrayList<>(shortOpts.values());
314    }
315
316    /**
317     * Dump state, suitable for debugging.
318     *
319     * @return Stringified form of this object
320     */
321    @Override
322    public String toString() {
323        final StringBuilder buf = new StringBuilder();
324
325        buf.append("[ Options: [ short ");
326        buf.append(shortOpts.toString());
327        buf.append(" ] [ long ");
328        buf.append(longOpts);
329        buf.append(" ]");
330
331        return buf.toString();
332    }
333}