inprogress
[jalview.git] / forester / ruby / evoruby / lib / evo / util / command_line_arguments.rb
1 #
2 # = lib/evo/util/command_line_arguments.rb - CommandLineArguments class
3 #
4 # Copyright::  Copyright (C) 2006-2007 Christian M. Zmasek
5 # License::    GNU Lesser General Public License (LGPL)
6 #
7 # $Id: command_line_arguments.rb,v 1.2 2007/06/12 04:51:34 cmzmasek Exp $
8 #
9 # last modified: 05/16/2007
10
11 module Evoruby
12
13     class CommandLineArguments
14
15         OPTIONS_PREFIX          = "-"
16         EXTENDED_OPTIONS_PREFIX = "--"
17         OPTIONS_SEPARATOR       = "="
18
19         # raises ArgumentError
20         def initialize( args )
21             @options  = Hash.new
22             @extended_options = Hash.new
23             @file_names = Array.new
24             parse_arguments( args )
25         end
26
27         def get_file_names
28             return @file_names
29         end
30
31         def get_file_name( i )
32             return @file_names[ i ]
33         end
34
35         def get_number_of_files()
36             return @file_names.length
37         end
38
39         def is_option_set?( option_name )
40             o = get_all_options
41             return ( o.has_key?( option_name ) )
42         end
43
44         # raises ArgumentError
45         def get_option_value( option_name )
46             o = get_all_options
47             if ( o.has_key?( option_name ) )
48                 value = o[ option_name ]
49                 if ( !Util.is_string_empty?( value ) )
50                     return value
51                 else
52                     raise( ArgumentError, "value for option \"" +
53                          option_name + "\" is not set", caller )
54                 end
55             else
56                 raise( ArgumentError, "option \"" + option_name +
57                      "\" is not set", caller )
58             end
59         end
60
61         def get_option_value_as_int( option_name )
62             return get_option_value( option_name ).to_i
63         end
64
65         def get_option_value_as_float( option_name )
66             return get_option_value( option_name ).to_f
67         end
68
69         # mandatory_options (Array)
70         #
71         def validate_mandatory_options( mandatory_options )
72             o = get_all_options
73             missing = Array.new
74             for ma in mandatory_options
75                 if ( !o.has_key?( ma ) )
76                     missing.push( ma )
77                 end
78             end
79             return missing
80         end
81
82         # mandatory_options (Array)
83         #
84         def validate_mandatory_options_as_str( mandatory_options )
85             missing = validate_mandatory_options( mandatory_options )
86             return missing.join( ", " )
87         end
88
89         # allowed_options (Array)
90         #
91         def validate_allowed_options( allowed_options )
92             o = get_all_options
93             disallowed = Array.new
94             o.each_key { |op|
95                 if ( !allowed_options.include?( op ) )
96                     disallowed.push( op )
97                 end
98             }
99             return disallowed
100         end
101
102         # allowed_options (Array)
103         #
104         def validate_allowed_options_as_str( allowed_options )
105             disallowed = validate_allowed_options( allowed_options )
106             return disallowed.join( ", " )
107         end
108
109         private
110
111         def get_all_options
112             o = Hash.new
113             o.merge!( get_options_list )
114             o.merge!( get_extended_options_list )
115             return o
116         end
117
118         def parse_arguments( args )
119             for arg in args
120                 if ( arg.index( EXTENDED_OPTIONS_PREFIX ) == 0 )
121                     parse_option( arg.slice( EXTENDED_OPTIONS_PREFIX.length, arg.length() - 1 ),
122                                   get_extended_options_list )
123
124                 elsif ( arg.index( OPTIONS_PREFIX ) == 0 )
125                     parse_option( arg.slice( OPTIONS_PREFIX.length, arg.length() - 1 ),
126                                   get_options_list )
127
128                 else
129                     get_file_names.push( arg )
130                 end
131             end
132         end
133
134         # raises ArgumentError
135         def parse_option( option, options_map )
136             sep_index = option.index( OPTIONS_SEPARATOR )
137             if ( sep_index == nil )
138                 if ( Util.is_string_empty?( option ) )
139                     raise( ArgumentError, "attempt to set option with an empty name" )
140                 end
141                 if ( get_all_options.has_key?( option ) )
142                      raise( ArgumentError, "attempt to set option \"" +
143                             option + "\" mutiple times" )
144                 end
145                 options_map[ option ] = ""
146             else
147                 key = option.slice( 0, sep_index )
148                 value = option.slice( sep_index + 1, option.length() - 1 )
149                 if ( Util.is_string_empty?( key ) )
150                     raise( ArgumentError, "attempt to set option with an empty name" )
151                 end
152                 if ( Util.is_string_empty?( value ) )
153                     raise( ArgumentError, "attempt to set option with an empty value" )
154                 end
155                 if ( get_all_options.has_key?( key ) )
156                     raise( ArgumentError, "attempt to set option \"" +
157                             key + "\" mutiple times [" + option + "]" )
158                 end
159                 options_map[ key ] = value
160             end
161         end
162
163         def get_file_names_list
164             return @file_names
165         end
166
167         def get_options_list
168             return @options
169         end
170
171         def get_extended_options_list
172             return @extended_options
173         end
174
175     end # class CommandLineArguments
176
177 end # module Evoruby