components/visual-panels/core/src/java/util/com/oracle/solaris/vp/util/cli/OptionElement.java
changeset 3553 f1d133b09a8c
parent 3552 077ebe3d0d24
child 3554 ef58713bafc4
equal deleted inserted replaced
3552:077ebe3d0d24 3553:f1d133b09a8c
     1 /*
       
     2  * CDDL HEADER START
       
     3  *
       
     4  * The contents of this file are subject to the terms of the
       
     5  * Common Development and Distribution License (the "License").
       
     6  * You may not use this file except in compliance with the License.
       
     7  *
       
     8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
       
     9  * or http://www.opensolaris.org/os/licensing.
       
    10  * See the License for the specific language governing permissions
       
    11  * and limitations under the License.
       
    12  *
       
    13  * When distributing Covered Code, include this CDDL HEADER in each
       
    14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
       
    15  * If applicable, add the following below this CDDL HEADER, with the
       
    16  * fields enclosed by brackets "[]" replaced with your own identifying
       
    17  * information: Portions Copyright [yyyy] [name of copyright owner]
       
    18  *
       
    19  * CDDL HEADER END
       
    20  */
       
    21 
       
    22 /*
       
    23  * Copyright (c) 2009, 2012, Oracle and/or its affiliates. All rights reserved.
       
    24  */
       
    25 
       
    26 package com.oracle.solaris.vp.util.cli;
       
    27 
       
    28 import java.util.*;
       
    29 import com.oracle.solaris.vp.util.cli.exception.*;
       
    30 
       
    31 public class OptionElement extends Option {
       
    32     //
       
    33     // Instance data
       
    34     //
       
    35 
       
    36     private boolean required;
       
    37     private boolean documented;
       
    38     private List<String> names;
       
    39     private String argName;
       
    40     private boolean argOptional;
       
    41     private String description;
       
    42     private int useLimit;
       
    43 
       
    44     //
       
    45     // Constructors
       
    46     //
       
    47 
       
    48     public OptionElement(String[] names, boolean required,
       
    49 	String argName, boolean argOptional, String description, int useLimit) {
       
    50 	setNames(names);
       
    51 	setRequired(required);
       
    52 	setArgName(argName);
       
    53 	setArgOptional(argOptional);
       
    54 	setDescription(description);
       
    55 	setUseLimit(useLimit);
       
    56 	setDocumented(true);
       
    57     }
       
    58 
       
    59     public OptionElement(String name, boolean required, String argName,
       
    60 	String description) {
       
    61 	this(new String[] {name}, required, argName, false, description, 1);
       
    62     }
       
    63 
       
    64     public OptionElement(String name, boolean required, String description) {
       
    65 	this(name, required, null, description);
       
    66     }
       
    67 
       
    68     public OptionElement(String name1, String name2, boolean required,
       
    69 	String argName, String description) {
       
    70 	this(new String[] {name1, name2},
       
    71 	    required, argName, false, description, 1);
       
    72     }
       
    73 
       
    74     public OptionElement(String name1, String name2, boolean required,
       
    75 	String description) {
       
    76 	this(name1, name2, required, null, description);
       
    77     }
       
    78 
       
    79     //
       
    80     // Option methods
       
    81     //
       
    82 
       
    83     @Override
       
    84     public boolean isDocumented() {
       
    85 	return documented;
       
    86     }
       
    87 
       
    88     @Override
       
    89     public boolean isRequired() {
       
    90 	return required;
       
    91     }
       
    92 
       
    93     //
       
    94     // OptionElement methods
       
    95     //
       
    96 
       
    97     public void setDocumented(boolean documented) {
       
    98 	this.documented = documented;
       
    99     }
       
   100 
       
   101     public void setRequired(boolean required) {
       
   102 	this.required = required;
       
   103     }
       
   104 
       
   105     public void setNames(String[] names) {
       
   106 	this.names = Arrays.asList(names);
       
   107     }
       
   108 
       
   109     public String[] getNames() {
       
   110 	return names.toArray(new String[names.size()]);
       
   111     }
       
   112 
       
   113     public String getName() {
       
   114 	try {
       
   115 	    return names.get(0);
       
   116 	}
       
   117 	catch (IndexOutOfBoundsException e) {
       
   118 	    return null;
       
   119 	}
       
   120     }
       
   121 
       
   122     public void setArgName(String argName) {
       
   123 	this.argName = argName;
       
   124     }
       
   125 
       
   126     public String getArgName() {
       
   127 	return argName;
       
   128     }
       
   129 
       
   130     public boolean getTakesArgument() {
       
   131 	return argName != null;
       
   132     }
       
   133 
       
   134     public void setArgOptional(boolean argOptional) {
       
   135 	this.argOptional = argOptional;
       
   136     }
       
   137 
       
   138     public boolean getArgOptional() {
       
   139 	return argOptional;
       
   140     }
       
   141 
       
   142     public void setDescription(String description) {
       
   143 	this.description = description;
       
   144     }
       
   145 
       
   146     public String getDescription() {
       
   147 	return description;
       
   148     }
       
   149 
       
   150     public void setUseLimit(int useLimit) {
       
   151 	this.useLimit = useLimit;
       
   152     }
       
   153 
       
   154     public int getUseLimit() {
       
   155 	return useLimit;
       
   156     }
       
   157 
       
   158     /**
       
   159      * Returns a boolean indicating whether the given
       
   160      * <code>ParsedOption</code> matches this
       
   161      * <code>OptionElement</code>.
       
   162      */
       
   163     protected boolean matches(ParsedOption processed) {
       
   164 	String opt = processed.getOpt();
       
   165 	if (opt != null) {
       
   166 	    for (String name : getNames()) {
       
   167 //		System.out.printf("Testing opt=%s optArg=%s against %s\n",
       
   168 //		    opt, processed.getOptArg(), name);
       
   169 
       
   170 		if (opt.equals(name)) {
       
   171 		    return true;
       
   172 		}
       
   173 	    }
       
   174 	}
       
   175 	return false;
       
   176     }
       
   177 
       
   178     /**
       
   179      * Validates the given option and optional argument against the
       
   180      * option/optArg that this OptionElement expects.  If the option is valid,
       
   181      * does nothing.	Otherwise, an exception is thrown.
       
   182      *
       
   183      * @param	    parsed
       
   184      *		    the option parsed from the command line
       
   185      *
       
   186      * @param	    useCount
       
   187      *		    the number of times this option has been used
       
   188      *
       
   189      * @param	    formatter
       
   190      *		    used to format the option string in exception
       
   191      *		    messages
       
   192      *
       
   193      * @param	    processed
       
   194      *		    the options processed so far, for use in exceptions
       
   195      *
       
   196      * @exception   InvalidOptionException
       
   197      *		    if the given option doesn't match the name of this
       
   198      *		    OptionElement
       
   199      *
       
   200      * @exception   OptionUseExceededException
       
   201      *		    if this OptionElement has been used more times
       
   202      *		    than it is allowed
       
   203      *
       
   204      * @exception   MissingOptArgException
       
   205      *		    if this OptionElement requires an argument but
       
   206      *		    none was specified
       
   207      *
       
   208      * @exception   UnexpectedOptArgException
       
   209      *		    if this OptionElement takes no argument but
       
   210      *		    one was specified
       
   211      *
       
   212      * @exception   InvalidOptArgException
       
   213      *		    if this OptionElement's argument is invalid --
       
   214      *		    not thrown in this default implementation
       
   215      */
       
   216     protected void validate(ParsedOption parsed, int useCount,
       
   217 	OptionFormatter formatter, OptionMap[] processed) throws
       
   218 	InvalidOptionException, OptionUseExceededException,
       
   219 	MissingOptArgException, UnexpectedOptArgException,
       
   220 	InvalidOptArgException {
       
   221 
       
   222 	String opt = parsed.getOpt();
       
   223 	String optArg = parsed.getOptArg();
       
   224 	String formatted = OptionMap.getFormatted(parsed, this, formatter);
       
   225 
       
   226 //	System.out.printf("Validating: opt=%s optArg=%s\n", opt, optArg);
       
   227 
       
   228 	// Verify given option identifies this OptionElement
       
   229 	if (!matches(parsed)) {
       
   230 	    throw new InvalidOptionException(formatted, processed);
       
   231 	}
       
   232 
       
   233 	// Change message to reflect the option syntax, not the passed-in opt
       
   234 	formatted = formatter.getFormatted(this, false);
       
   235 
       
   236 	// Verify this option hasn't been specified too many times
       
   237 	int useLimit = getUseLimit();
       
   238 	if (useLimit >= 0 && useCount > useLimit) {
       
   239 	    throw new OptionUseExceededException(
       
   240 		formatted, useLimit, processed);
       
   241 	}
       
   242 
       
   243 	// Verify optArg if required
       
   244 	if (getTakesArgument() && !getArgOptional() && optArg == null) {
       
   245 	    throw new MissingOptArgException(formatted, processed);
       
   246 	}
       
   247 
       
   248 	// Verify no optArg if not expected
       
   249 	if (!getTakesArgument() && optArg != null) {
       
   250 	    throw new UnexpectedOptArgException(
       
   251 		formatted, optArg, processed);
       
   252 	}
       
   253     }
       
   254 }