moved to: https://sites.google.com/site/cmzmasek/home/software/forester
[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;
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 double getOptionValueAsDouble( final String option_name ) throws IOException {
116         double d = -Double.MAX_VALUE;
117         try {
118             d = new Double( getOptionValue( option_name ) ).doubleValue();
119         }
120         catch ( final NumberFormatException e ) {
121             throw new IOException( "value for option \"" + option_name + "\" is expected to be of type double" );
122         }
123         return d;
124     }
125
126     public int getOptionValueAsInt( final String option_name ) throws IOException {
127         int i = Integer.MIN_VALUE;
128         try {
129             i = new Integer( getOptionValue( option_name ) ).intValue();
130         }
131         catch ( final NumberFormatException e ) {
132             throw new IOException( "value for option \"" + option_name + "\" is expected to be of type integer" );
133         }
134         return i;
135     }
136
137     public long getOptionValueAsLong( final String option_name ) throws IOException {
138         long l = Long.MIN_VALUE;
139         try {
140             l = new Long( getOptionValue( option_name ) ).longValue();
141         }
142         catch ( final NumberFormatException e ) {
143             throw new IOException( "value for option \"" + option_name + "\" is expected to be of type long" );
144         }
145         return l;
146     }
147
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 = "";
153     }
154
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 ) );
160         }
161         else {
162             throw new IllegalArgumentException( "option \"" + option_name + "\" is not set" );
163         }
164     }
165
166     public boolean isOptionSet( final String option_name ) {
167         final Map<String, String> o = getAllOptions();
168         return ( o.containsKey( option_name ) );
169     }
170
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 ) ) );
175         }
176         else {
177             throw new IllegalArgumentException( "option \"" + option_name + "\" is not set" );
178         }
179     }
180
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 += " ";
187             }
188             if ( arg.startsWith( CommandLineArguments.EXTENDED_OPTIONS_PREFIX ) ) {
189                 parseOption( arg.substring( CommandLineArguments.EXTENDED_OPTIONS_PREFIX.length() ),
190                              getExtendedOptionsList() );
191             }
192             else if ( arg.startsWith( CommandLineArguments.OPTIONS_PREFIX ) ) {
193                 parseOption( arg.substring( CommandLineArguments.OPTIONS_PREFIX.length() ), getOptionsList() );
194             }
195             else {
196                 getNamesList().add( arg );
197             }
198         }
199     }
200
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" );
206             }
207             if ( getAllOptions().containsKey( option ) ) {
208                 throw new IOException( "attempt to set option \"" + option + "\" mutiple times" );
209             }
210             options_map.put( option, null );
211         }
212         else {
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" );
217             }
218             //  if ( ForesterUtil.isEmpty( value ) ) {
219             //      throw new IllegalArgumentException( "attempt to set option with an empty value" );
220             //  }
221             if ( getAllOptions().containsKey( key ) ) {
222                 throw new IllegalArgumentException( "attempt to set option \"" + key + "\" mutiple times [" + option
223                         + "]" );
224             }
225             options_map.put( key, value );
226         }
227     }
228
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 );
235             }
236         }
237         return dissallowed;
238     }
239
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 += "\", ";
247             }
248             else {
249                 dissallowed_string += "\"";
250             }
251         }
252         return dissallowed_string;
253     }
254
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 ) ) {
261                 missing.add( ma );
262             }
263         }
264         return missing;
265     }
266
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 += "\", ";
274             }
275             else {
276                 missing_string += "\"";
277             }
278         }
279         return missing_string;
280     }
281 }