2 # = lib/evo/util/command_line_arguments.rb - CommandLineArguments class
4 # Copyright:: Copyright (C) 2006-2007 Christian M. Zmasek
5 # License:: GNU Lesser General Public License (LGPL)
7 # $Id: command_line_arguments.rb,v 1.2 2007/06/12 04:51:34 cmzmasek Exp $
9 # last modified: 05/16/2007
13 class CommandLineArguments
16 EXTENDED_OPTIONS_PREFIX = "--"
17 OPTIONS_SEPARATOR = "="
19 # raises ArgumentError
20 def initialize( args )
22 @extended_options = Hash.new
23 @file_names = Array.new
24 parse_arguments( args )
31 def get_file_name( i )
32 return @file_names[ i ]
35 def get_number_of_files()
36 return @file_names.length
39 def is_option_set?( option_name )
41 return ( o.has_key?( option_name ) )
44 # raises ArgumentError
45 def get_option_value( option_name )
47 if ( o.has_key?( option_name ) )
48 value = o[ option_name ]
49 if ( !Util.is_string_empty?( value ) )
52 raise( ArgumentError, "value for option \"" +
53 option_name + "\" is not set", caller )
56 raise( ArgumentError, "option \"" + option_name +
57 "\" is not set", caller )
61 def get_option_value_as_int( option_name )
62 return get_option_value( option_name ).to_i
65 def get_option_value_as_float( option_name )
66 return get_option_value( option_name ).to_f
69 # mandatory_options (Array)
71 def validate_mandatory_options( mandatory_options )
74 for ma in mandatory_options
75 if ( !o.has_key?( ma ) )
82 # mandatory_options (Array)
84 def validate_mandatory_options_as_str( mandatory_options )
85 missing = validate_mandatory_options( mandatory_options )
86 return missing.join( ", " )
89 # allowed_options (Array)
91 def validate_allowed_options( allowed_options )
93 disallowed = Array.new
95 if ( !allowed_options.include?( op ) )
102 # allowed_options (Array)
104 def validate_allowed_options_as_str( allowed_options )
105 disallowed = validate_allowed_options( allowed_options )
106 return disallowed.join( ", " )
113 o.merge!( get_options_list )
114 o.merge!( get_extended_options_list )
118 def parse_arguments( 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 )
124 elsif ( arg.index( OPTIONS_PREFIX ) == 0 )
125 parse_option( arg.slice( OPTIONS_PREFIX.length, arg.length() - 1 ),
129 get_file_names.push( arg )
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" )
141 if ( get_all_options.has_key?( option ) )
142 raise( ArgumentError, "attempt to set option \"" +
143 option + "\" mutiple times" )
145 options_map[ option ] = ""
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" )
152 if ( Util.is_string_empty?( value ) )
153 raise( ArgumentError, "attempt to set option with an empty value" )
155 if ( get_all_options.has_key?( key ) )
156 raise( ArgumentError, "attempt to set option \"" +
157 key + "\" mutiple times [" + option + "]" )
159 options_map[ key ] = value
163 def get_file_names_list
171 def get_extended_options_list
172 return @extended_options
175 end # class CommandLineArguments