2 # = lib/evo/util/command_line_arguments.rb - CommandLineArguments class
4 # Copyright:: Copyright (C) 2017 Christian M. Zmasek
5 # License:: GNU Lesser General Public License (LGPL)
7 # Last modified: 2017/02/12
10 class CommandLineArguments
13 EXTENDED_OPTIONS_PREFIX = "--"
14 OPTIONS_SEPARATOR = "="
15 # raises ArgumentError
16 def initialize( args )
18 @extended_options = Hash.new
19 @file_names = Array.new
20 parse_arguments( args )
27 def get_file_name( i )
28 return @file_names[ i ]
31 def get_number_of_files()
32 return @file_names.length
35 def is_option_set?( option_name )
37 return ( o.has_key?( option_name ) )
40 # raises ArgumentError
41 def get_option_value( option_name )
43 if ( o.has_key?( option_name ) )
44 value = o[ option_name ]
45 if ( !Util.is_string_empty?( value ) )
49 puts( "value for option \"" + option_name + "\" is not set")
54 raise( ArgumentError, "option \"" + option_name +
55 "\" is not set", caller )
59 def get_option_value_as_int( option_name )
60 return get_option_value( option_name ).to_i
63 def get_option_value_as_float( option_name )
64 return get_option_value( option_name ).to_f
67 # mandatory_options (Array)
69 def validate_mandatory_options( mandatory_options )
72 for ma in mandatory_options
73 if ( !o.has_key?( ma ) )
80 # mandatory_options (Array)
82 def validate_mandatory_options_as_str( mandatory_options )
83 missing = validate_mandatory_options( mandatory_options )
84 return missing.join( ", " )
87 # allowed_options (Array)
89 def validate_allowed_options( allowed_options )
91 disallowed = Array.new
93 if ( !allowed_options.include?( op ) )
100 # allowed_options (Array)
102 def validate_allowed_options_as_str( allowed_options )
103 disallowed = validate_allowed_options( allowed_options )
104 return disallowed.join( ", " )
111 o.merge!( get_options_list )
112 o.merge!( get_extended_options_list )
116 def parse_arguments( 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 )
122 elsif ( arg.index( OPTIONS_PREFIX ) == 0 )
123 parse_option( arg.slice( OPTIONS_PREFIX.length, arg.length() - 1 ),
127 get_file_names.push( arg )
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" )
139 if ( get_all_options.has_key?( option ) )
140 raise( ArgumentError, "attempt to set option \"" +
141 option + "\" mutiple times" )
143 options_map[ option ] = ""
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" )
150 if ( Util.is_string_empty?( value ) )
151 raise( ArgumentError, "attempt to set option with an empty value" )
153 if ( get_all_options.has_key?( key ) )
154 raise( ArgumentError, "attempt to set option \"" +
155 key + "\" mutiple times [" + option + "]" )
157 options_map[ key ] = value
161 def get_file_names_list
169 def get_extended_options_list
170 return @extended_options
173 end # class CommandLineArguments