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