2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
9 // This library is free software; you can redistribute it and/or
10 // modify it under the terms of the GNU Lesser General Public
11 // License as published by the Free Software Foundation; either
12 // version 2.1 of the License, or (at your option) any later version.
14 // This library is distributed in the hope that it will be useful,
15 // but WITHOUT ANY WARRANTY; without even the implied warranty of
16 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 // Lesser General Public License for more details.
19 // You should have received a copy of the GNU Lesser General Public
20 // License along with this library; if not, write to the Free Software
21 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA
23 // Contact: phylosoft @ gmail . com
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
26 package org.forester.util;
29 import java.io.IOException;
30 import java.util.ArrayList;
31 import java.util.HashMap;
32 import java.util.Iterator;
33 import java.util.List;
36 public final class CommandLineArguments {
38 private final static String OPTIONS_PREFIX = "-";
39 private final static String EXTENDED_OPTIONS_PREFIX = "--";
40 private final static String OPTIONS_SEPARATOR = "=";
41 private Map<String, String> _options;
42 private Map<String, String> _extended_options;
43 private List<String> _names;
44 private String _command_line_str;
46 public CommandLineArguments( final String[] args ) throws IOException {
48 parseCommandLineArguments( args );
51 private Map<String, String> getAllOptions() {
52 final Map<String, String> o = new HashMap<String, String>();
53 o.putAll( getOptionsList() );
54 o.putAll( getExtendedOptionsList() );
58 public String getCommandLineArgsAsString() {
59 return _command_line_str;
62 private Map<String, String> getExtendedOptionsList() {
63 return _extended_options;
66 public File getFile( final int i ) {
67 return new File( getNames()[ i ] );
70 public String getName( final int i ) {
71 return getNames()[ i ];
74 public String[] getNames() {
75 final String[] a = new String[ getNamesList().size() ];
76 return getNamesList().toArray( a );
79 private List<String> getNamesList() {
83 public int getNumberOfNames() {
84 return getNames().length;
87 private Map<String, String> getOptionsList() {
91 public String getOptionValue( final String option_name ) throws IllegalArgumentException {
92 final Map<String, String> o = getAllOptions();
93 if ( o.containsKey( option_name ) ) {
94 final String value = o.get( option_name );
95 if ( !ForesterUtil.isEmpty( value ) ) {
99 throw new IllegalArgumentException( "value for \"" + option_name + "\" is not set" );
103 throw new IllegalArgumentException( "option \"" + option_name + "\" is not set" );
111 public String getOptionValueAsCleanString( final String option_name ) throws IllegalArgumentException {
112 return getOptionValue( option_name ).replaceAll( "\"", "" ).replaceAll( "\'", "" );
115 public char getOptionValueAsChar( final String option_name ) throws IllegalArgumentException {
116 return getOptionValue( option_name ).charAt( 0 );
119 public double getOptionValueAsDouble( final String option_name ) throws IOException {
120 double d = -Double.MAX_VALUE;
122 d = new Double( getOptionValue( option_name ) ).doubleValue();
124 catch ( final NumberFormatException e ) {
125 throw new IOException( "value for option \"" + option_name + "\" is expected to be of type double" );
130 public int getOptionValueAsInt( final String option_name ) throws IOException {
131 int i = Integer.MIN_VALUE;
133 i = new Integer( getOptionValue( option_name ) ).intValue();
135 catch ( final NumberFormatException e ) {
136 throw new IOException( "value for option \"" + option_name + "\" is expected to be of type integer" );
141 public long getOptionValueAsLong( final String option_name ) throws IOException {
142 long l = Long.MIN_VALUE;
144 l = new Long( getOptionValue( option_name ) ).longValue();
146 catch ( final NumberFormatException e ) {
147 throw new IOException( "value for option \"" + option_name + "\" is expected to be of type long" );
152 private void init() {
153 _options = new HashMap<String, String>();
154 _extended_options = new HashMap<String, String>();
155 _names = new ArrayList<String>();
156 _command_line_str = "";
159 public boolean isOptionHasAValue( final String option_name ) {
160 final Map<String, String> o = getAllOptions();
161 if ( o.containsKey( option_name ) ) {
162 final String value = o.get( option_name );
163 return ( !ForesterUtil.isEmpty( value ) );
166 throw new IllegalArgumentException( "option \"" + option_name + "\" is not set" );
170 public boolean isOptionSet( final String option_name ) {
171 final Map<String, String> o = getAllOptions();
172 return ( o.containsKey( option_name ) );
175 public boolean isOptionValueSet( final String option_name ) throws IllegalArgumentException {
176 final Map<String, String> o = getAllOptions();
177 if ( o.containsKey( option_name ) ) {
178 return !( ForesterUtil.isEmpty( o.get( option_name ) ) );
181 throw new IllegalArgumentException( "option \"" + option_name + "\" is not set" );
185 private void parseCommandLineArguments( final String[] args ) throws IOException {
186 for( int i = 0; i < args.length; ++i ) {
187 final String arg = args[ i ].trim();
188 _command_line_str += arg;
189 if ( i < ( args.length - 1 ) ) {
190 _command_line_str += " ";
192 if ( arg.startsWith( CommandLineArguments.EXTENDED_OPTIONS_PREFIX ) ) {
193 parseOption( arg.substring( CommandLineArguments.EXTENDED_OPTIONS_PREFIX.length() ),
194 getExtendedOptionsList() );
196 else if ( arg.startsWith( CommandLineArguments.OPTIONS_PREFIX ) ) {
197 parseOption( arg.substring( CommandLineArguments.OPTIONS_PREFIX.length() ), getOptionsList() );
200 getNamesList().add( arg );
205 private void parseOption( final String option, final Map<String, String> options_map ) throws IOException {
206 final int sep_index = option.indexOf( CommandLineArguments.OPTIONS_SEPARATOR );
207 if ( sep_index < 1 ) {
208 if ( ForesterUtil.isEmpty( option ) ) {
209 throw new IOException( "attempt to set option with an empty name" );
211 if ( getAllOptions().containsKey( option ) ) {
212 throw new IOException( "attempt to set option \"" + option + "\" mutiple times" );
214 options_map.put( option, null );
217 final String key = option.substring( 0, sep_index );
218 final String value = option.substring( sep_index + 1 );
219 if ( ForesterUtil.isEmpty( key ) ) {
220 throw new IllegalArgumentException( "attempt to set option with an empty name" );
222 // if ( ForesterUtil.isEmpty( value ) ) {
223 // throw new IllegalArgumentException( "attempt to set option with an empty value" );
225 if ( getAllOptions().containsKey( key ) ) {
226 throw new IllegalArgumentException( "attempt to set option \"" + key + "\" mutiple times [" + option
229 options_map.put( key, value );
233 public List<String> validateAllowedOptions( final List<String> allowed_options ) {
234 final Map<String, String> options = getAllOptions();
235 final List<String> dissallowed = new ArrayList<String>();
236 for( final String o : options.keySet() ) {
237 if ( !allowed_options.contains( o ) ) {
238 dissallowed.add( o );
244 public String validateAllowedOptionsAsString( final List<String> allowed_options ) {
245 final List<String> dissallowed = validateAllowedOptions( allowed_options );
246 String dissallowed_string = "";
247 for( final Iterator<String> iter = dissallowed.iterator(); iter.hasNext(); ) {
248 dissallowed_string += "\"" + iter.next();
249 if ( iter.hasNext() ) {
250 dissallowed_string += "\", ";
253 dissallowed_string += "\"";
256 return dissallowed_string;
259 public List<String> validateMandatoryOptions( final List<String> mandatory_options ) {
260 final Map<String, String> options = getAllOptions();
261 final List<String> missing = new ArrayList<String>();
262 for( final String string : mandatory_options ) {
263 final String ma = string;
264 if ( !options.containsKey( ma ) ) {
271 public String validateMandatoryOptionsAsString( final List<String> mandatory_options ) {
272 final List<String> missing = validateMandatoryOptions( mandatory_options );
273 String missing_string = "";
274 for( final Iterator<String> iter = missing.iterator(); iter.hasNext(); ) {
275 missing_string += "\"" + iter.next();
276 if ( iter.hasNext() ) {
277 missing_string += "\", ";
280 missing_string += "\"";
283 return missing_string;