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 double getOptionValueAsDouble( final String option_name ) throws IOException {
116 double d = -Double.MAX_VALUE;
118 d = new Double( getOptionValue( option_name ) ).doubleValue();
120 catch ( final NumberFormatException e ) {
121 throw new IOException( "value for option \"" + option_name + "\" is expected to be of type double" );
126 public int getOptionValueAsInt( final String option_name ) throws IOException {
127 int i = Integer.MIN_VALUE;
129 i = new Integer( getOptionValue( option_name ) ).intValue();
131 catch ( final NumberFormatException e ) {
132 throw new IOException( "value for option \"" + option_name + "\" is expected to be of type integer" );
137 public long getOptionValueAsLong( final String option_name ) throws IOException {
138 long l = Long.MIN_VALUE;
140 l = new Long( getOptionValue( option_name ) ).longValue();
142 catch ( final NumberFormatException e ) {
143 throw new IOException( "value for option \"" + option_name + "\" is expected to be of type long" );
148 private void init() {
149 _options = new HashMap<String, String>();
150 _extended_options = new HashMap<String, String>();
151 _names = new ArrayList<String>();
152 _command_line_str = "";
155 public boolean isOptionHasAValue( final String option_name ) {
156 final Map<String, String> o = getAllOptions();
157 if ( o.containsKey( option_name ) ) {
158 final String value = o.get( option_name );
159 return ( !ForesterUtil.isEmpty( value ) );
162 throw new IllegalArgumentException( "option \"" + option_name + "\" is not set" );
166 public boolean isOptionSet( final String option_name ) {
167 final Map<String, String> o = getAllOptions();
168 return ( o.containsKey( option_name ) );
171 public boolean isOptionValueSet( final String option_name ) throws IllegalArgumentException {
172 final Map<String, String> o = getAllOptions();
173 if ( o.containsKey( option_name ) ) {
174 return !( ForesterUtil.isEmpty( o.get( option_name ) ) );
177 throw new IllegalArgumentException( "option \"" + option_name + "\" is not set" );
181 private void parseCommandLineArguments( final String[] args ) throws IOException {
182 for( int i = 0; i < args.length; ++i ) {
183 final String arg = args[ i ].trim();
184 _command_line_str += arg;
185 if ( i < ( args.length - 1 ) ) {
186 _command_line_str += " ";
188 if ( arg.startsWith( CommandLineArguments.EXTENDED_OPTIONS_PREFIX ) ) {
189 parseOption( arg.substring( CommandLineArguments.EXTENDED_OPTIONS_PREFIX.length() ),
190 getExtendedOptionsList() );
192 else if ( arg.startsWith( CommandLineArguments.OPTIONS_PREFIX ) ) {
193 parseOption( arg.substring( CommandLineArguments.OPTIONS_PREFIX.length() ), getOptionsList() );
196 getNamesList().add( arg );
201 private void parseOption( final String option, final Map<String, String> options_map ) throws IOException {
202 final int sep_index = option.indexOf( CommandLineArguments.OPTIONS_SEPARATOR );
203 if ( sep_index < 1 ) {
204 if ( ForesterUtil.isEmpty( option ) ) {
205 throw new IOException( "attempt to set option with an empty name" );
207 if ( getAllOptions().containsKey( option ) ) {
208 throw new IOException( "attempt to set option \"" + option + "\" mutiple times" );
210 options_map.put( option, null );
213 final String key = option.substring( 0, sep_index );
214 final String value = option.substring( sep_index + 1 );
215 if ( ForesterUtil.isEmpty( key ) ) {
216 throw new IllegalArgumentException( "attempt to set option with an empty name" );
218 // if ( ForesterUtil.isEmpty( value ) ) {
219 // throw new IllegalArgumentException( "attempt to set option with an empty value" );
221 if ( getAllOptions().containsKey( key ) ) {
222 throw new IllegalArgumentException( "attempt to set option \"" + key + "\" mutiple times [" + option
225 options_map.put( key, value );
229 public List<String> validateAllowedOptions( final List<String> allowed_options ) {
230 final Map<String, String> options = getAllOptions();
231 final List<String> dissallowed = new ArrayList<String>();
232 for( final String o : options.keySet() ) {
233 if ( !allowed_options.contains( o ) ) {
234 dissallowed.add( o );
240 public String validateAllowedOptionsAsString( final List<String> allowed_options ) {
241 final List<String> dissallowed = validateAllowedOptions( allowed_options );
242 String dissallowed_string = "";
243 for( final Iterator<String> iter = dissallowed.iterator(); iter.hasNext(); ) {
244 dissallowed_string += "\"" + iter.next();
245 if ( iter.hasNext() ) {
246 dissallowed_string += "\", ";
249 dissallowed_string += "\"";
252 return dissallowed_string;
255 public List<String> validateMandatoryOptions( final List<String> mandatory_options ) {
256 final Map<String, String> options = getAllOptions();
257 final List<String> missing = new ArrayList<String>();
258 for( final String string : mandatory_options ) {
259 final String ma = string;
260 if ( !options.containsKey( ma ) ) {
267 public String validateMandatoryOptionsAsString( final List<String> mandatory_options ) {
268 final List<String> missing = validateMandatoryOptions( mandatory_options );
269 String missing_string = "";
270 for( final Iterator<String> iter = missing.iterator(); iter.hasNext(); ) {
271 missing_string += "\"" + iter.next();
272 if ( iter.hasNext() ) {
273 missing_string += "\", ";
276 missing_string += "\"";
279 return missing_string;