All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.aspectran.shell.command.option.Options Maven / Gradle / Ivy

There is a newer version: 8.1.3
Show newest version
/*
 * Copyright (c) 2008-2019 The Aspectran Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.aspectran.shell.command.option;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

/**
 * Main entry-point into the library.
 *
 * 

Options represents a collection of {@link Option} objects, which * describe the possible options for a command-line.

* *

It may flexibly parse long and short options, with or without * values. Additionally, it may parse only a portion of a commandline, * allowing for flexible multi-stage parsing.

* * @see ParsedOptions */ public class Options implements Serializable { /** @serial */ private static final long serialVersionUID = -6416293453155205092L; /** A map of the options with the character key */ private final Map shortOpts = new LinkedHashMap<>(); /** A map of the options with the long key */ private final Map longOpts = new LinkedHashMap<>(); /** A map of the required options */ private final List requiredOpts = new ArrayList<>(); /** A map of the option groups */ private final Map optionGroups = new LinkedHashMap<>(); private String title = "Options:"; private boolean skipParsingAtNonOption; public Options() { this(false); } public Options(boolean skipParsingAtNonOption) { this.skipParsingAtNonOption = skipParsingAtNonOption; } public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } public boolean isSkipParsingAtNonOption() { return skipParsingAtNonOption; } public void setSkipParsingAtNonOption(boolean skipParsingAtNonOption) { this.skipParsingAtNonOption = skipParsingAtNonOption; } /** * Add the specified option group. * * @param group the OptionGroup that is to be added * @return the resulting Options instance */ public Options addOptionGroup(OptionGroup group) { if (group.isRequired()) { requiredOpts.add(group); } for (Option option : group.getOptions()) { // an Option cannot be required if it is in an // OptionGroup, either the group is required or // nothing is required option.setRequired(false); addOption(option); optionGroups.put(option.getKey(), group); } return this; } /** * Lists the OptionGroups that are members of this Options instance. * * @return a Collection of OptionGroup instances */ Collection getOptionGroups() { return new HashSet<>(optionGroups.values()); } /** * Adds an option instance. * * @param opt the option that is to be added * @return the resulting Options instance */ public Options addOption(Option opt) { String key = opt.getKey(); // add it to the long option list if (opt.hasLongName()) { longOpts.put(opt.getLongName(), opt); } // if the option is required add it to the required list if (opt.isRequired()) { if (requiredOpts.contains(key)) { requiredOpts.remove(requiredOpts.indexOf(key)); } requiredOpts.add(key); } shortOpts.put(key, opt); return this; } /** * Returns {@code true} if no options have been added. * * @return {@code true} if no options have been added */ public boolean isEmpty() { return shortOpts.isEmpty(); } /** * Retrieve a read-only list of options in this set. * * @return read-only Collection of {@link Option} objects in this descriptor */ public Collection