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 */ 017package org.apache.commons.cli2.commandline; 018 019import java.util.Collections; 020import java.util.Iterator; 021import java.util.List; 022 023import org.apache.commons.cli2.CommandLine; 024import org.apache.commons.cli2.Option; 025import org.apache.commons.cli2.resource.ResourceConstants; 026import org.apache.commons.cli2.resource.ResourceHelper; 027 028/** 029 * Instances of CommandLine represent a command line that has been processed 030 * according to the definition supplied to the parser. 031 */ 032public abstract class CommandLineImpl implements CommandLine { 033 public final boolean hasOption(final String trigger) { 034 return hasOption(getOption(trigger)); 035 } 036 037 public final List getValues(final String trigger) { 038 return getValues(getOption(trigger), Collections.EMPTY_LIST); 039 } 040 041 public final List getValues(final String trigger, 042 final List defaultValues) { 043 return getValues(getOption(trigger), defaultValues); 044 } 045 046 public final List getValues(final Option option) { 047 return getValues(option, Collections.EMPTY_LIST); 048 } 049 050 public final Object getValue(final String trigger) { 051 return getValue(getOption(trigger), null); 052 } 053 054 public final Object getValue(final String trigger, 055 final Object defaultValue) { 056 return getValue(getOption(trigger), defaultValue); 057 } 058 059 public final Object getValue(final Option option) { 060 return getValue(option, null); 061 } 062 063 public final Object getValue(final Option option, 064 final Object defaultValue) { 065 final List values; 066 067 if (defaultValue == null) { 068 values = getValues(option); 069 } else { 070 values = getValues(option, Collections.singletonList(defaultValue)); 071 } 072 073 if (values.size() > 1) { 074 throw new IllegalStateException(ResourceHelper.getResourceHelper().getMessage(ResourceConstants.ARGUMENT_TOO_MANY_VALUES)); 075 } 076 077 if (values.isEmpty()) { 078 return defaultValue; 079 } 080 081 return values.get(0); 082 } 083 084 public final Boolean getSwitch(final String trigger) { 085 return getSwitch(getOption(trigger), null); 086 } 087 088 public final Boolean getSwitch(final String trigger, 089 final Boolean defaultValue) { 090 return getSwitch(getOption(trigger), defaultValue); 091 } 092 093 public final Boolean getSwitch(final Option option) { 094 return getSwitch(option, null); 095 } 096 097 public final String getProperty(final Option option, final String property) { 098 return getProperty(option, property, null); 099 } 100 101 public final int getOptionCount(final String trigger) { 102 return getOptionCount(getOption(trigger)); 103 } 104 105 public final int getOptionCount(final Option option) { 106 if (option == null) { 107 return 0; 108 } 109 110 int count = 0; 111 112 for (Iterator i = getOptions().iterator(); i.hasNext();) { 113 if (option.equals(i.next())) { 114 ++count; 115 } 116 } 117 118 return count; 119 } 120}