in progress...
[jalview.git] / forester / java / src / org / forester / util / CommandLineArguments.java
1 // $Id:
2 // FORESTER -- software libraries and applications
3 // for evolutionary biology research and applications.
4 //
5 // Copyright (C) 2008-2009 Christian M. Zmasek
6 // Copyright (C) 2008-2009 Burnham Institute for Medical Research
7 // All rights reserved
8 //
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.
13 //
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.
18 //
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
22 //
23 // Contact: phylosoft @ gmail . com
24 // WWW: https://sites.google.com/site/cmzmasek/home/software/forester
25
26 package org.forester.util;
27
28 import java.io.File;
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;
34 import java.util.Map;
35
36 public final class CommandLineArguments {
37
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;
45
46     public CommandLineArguments( final String[] args ) throws IOException {
47         init();
48         parseCommandLineArguments( args );
49     }
50
51     private Map<String, String> getAllOptions() {
52         final Map<String, String> o = new HashMap<String, String>();
53         o.putAll( getOptionsList() );
54         o.putAll( getExtendedOptionsList() );
55         return o;
56     }
57
58     public String getCommandLineArgsAsString() {
59         return _command_line_str;
60     }
61
62     private Map<String, String> getExtendedOptionsList() {
63         return _extended_options;
64     }
65
66     public File getFile( final int i ) {
67         return new File( getNames()[ i ] );
68     }
69
70     public String getName( final int i ) {
71         return getNames()[ i ];
72     }
73
74     public String[] getNames() {
75         final String[] a = new String[ getNamesList().size() ];
76         return getNamesList().toArray( a );
77     }
78
79     private List<String> getNamesList() {
80         return _names;
81     }
82
83     public int getNumberOfNames() {
84         return getNames().length;
85     }
86
87     private Map<String, String> getOptionsList() {
88         return _options;
89     }
90
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 ) ) {
96                 return value.replaceAll( "\\s+", " " ).trim();
97             }
98             else {
99                 throw new IllegalArgumentException( "value for \"" + option_name + "\" is not set" );
100             }
101         }
102         else {
103             throw new IllegalArgumentException( "option \"" + option_name + "\" is not set" );
104         }
105     }
106
107     /**
108      * Removes quotes
109      *
110      */
111     public String getOptionValueAsCleanString( final String option_name ) throws IllegalArgumentException {
112         return getOptionValue( option_name ).replaceAll( "\"", "" ).replaceAll( "\'", "" );
113     }
114
115     public char getOptionValueAsChar( final String option_name ) throws IllegalArgumentException {
116         return getOptionValue( option_name ).charAt( 0 );
117     }
118
119     public double getOptionValueAsDouble( final String option_name ) throws IOException {
120         double d = -Double.MAX_VALUE;
121         try {
122             d = new Double( getOptionValue( option_name ) ).doubleValue();
123         }
124         catch ( final NumberFormatException e ) {
125             throw new IOException( "value for option \"" + option_name + "\" is expected to be of type double" );
126         }
127         return d;
128     }
129
130     public int getOptionValueAsInt( final String option_name ) throws IOException {
131         int i = Integer.MIN_VALUE;
132         try {
133             i = new Integer( getOptionValue( option_name ) ).intValue();
134         }
135         catch ( final NumberFormatException e ) {
136             throw new IOException( "value for option \"" + option_name + "\" is expected to be of type integer" );
137         }
138         return i;
139     }
140
141     public long getOptionValueAsLong( final String option_name ) throws IOException {
142         long l = Long.MIN_VALUE;
143         try {
144             l = new Long( getOptionValue( option_name ) ).longValue();
145         }
146         catch ( final NumberFormatException e ) {
147             throw new IOException( "value for option \"" + option_name + "\" is expected to be of type long" );
148         }
149         return l;
150     }
151
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 = "";
157     }
158
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 ) );
164         }
165         else {
166             throw new IllegalArgumentException( "option \"" + option_name + "\" is not set" );
167         }
168     }
169
170     public boolean isOptionSet( final String option_name ) {
171         final Map<String, String> o = getAllOptions();
172         return ( o.containsKey( option_name ) );
173     }
174
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 ) ) );
179         }
180         else {
181             throw new IllegalArgumentException( "option \"" + option_name + "\" is not set" );
182         }
183     }
184
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 += " ";
191             }
192             if ( arg.startsWith( CommandLineArguments.EXTENDED_OPTIONS_PREFIX ) ) {
193                 parseOption( arg.substring( CommandLineArguments.EXTENDED_OPTIONS_PREFIX.length() ),
194                              getExtendedOptionsList() );
195             }
196             else if ( arg.startsWith( CommandLineArguments.OPTIONS_PREFIX ) ) {
197                 parseOption( arg.substring( CommandLineArguments.OPTIONS_PREFIX.length() ), getOptionsList() );
198             }
199             else {
200                 getNamesList().add( arg );
201             }
202         }
203     }
204
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" );
210             }
211             if ( getAllOptions().containsKey( option ) ) {
212                 throw new IOException( "attempt to set option \"" + option + "\" mutiple times" );
213             }
214             options_map.put( option, null );
215         }
216         else {
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" );
221             }
222             //  if ( ForesterUtil.isEmpty( value ) ) {
223             //      throw new IllegalArgumentException( "attempt to set option with an empty value" );
224             //  }
225             if ( getAllOptions().containsKey( key ) ) {
226                 throw new IllegalArgumentException( "attempt to set option \"" + key + "\" mutiple times [" + option
227                                                     + "]" );
228             }
229             options_map.put( key, value );
230         }
231     }
232
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 );
239             }
240         }
241         return dissallowed;
242     }
243
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 += "\", ";
251             }
252             else {
253                 dissallowed_string += "\"";
254             }
255         }
256         return dissallowed_string;
257     }
258
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 ) ) {
265                 missing.add( ma );
266             }
267         }
268         return missing;
269     }
270
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 += "\", ";
278             }
279             else {
280                 missing_string += "\"";
281             }
282         }
283         return missing_string;
284     }
285 }