params 0.0.2 → 0.0.8

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ module BBFS
2
+ module Params
3
+ VERSION = "0.0.8" # Parser updated in params.rb
4
+ end
5
+ end
data/lib/params.rb CHANGED
@@ -1,11 +1,322 @@
1
- # A simple params module.
1
+ # Author: Yaron Dror (yaron.dror.bb@gmail.com)
2
+ # Description: The file contains 'Params' module implementation.
3
+ # Notes:
4
+ # module.init should be called if user wants to override defined parameters by file or command line.
5
+ # Parameter can be defined only once.
6
+ # Parameters have to be defined before they are accessed. see examples below.
7
+ #
8
+ # Examples of definitions:
9
+ # Params.string('parameter_str', 'this is a string' ,'description_for_string')
10
+ # Params.integer('parameter_int',1 , 'description_for_integer')
11
+ # Params.float('parameter_float',2.6 , 'description_for_float')
12
+ # Params.boolean('parameter_true', true, 'description_for_true')
13
+ # Params.boolean('parameter_false',false , 'description_for_false')
14
+ # Note. Parameters have to be defined before they are accessed.
15
+ #
16
+ # Examples of usages (get\set access is through the [] operator).
17
+ # local_var_of_type_string = Params['parameter_str']
18
+ # puts local_var_of_type_string # Will produce --> this is a string
19
+ # Params['parameter_float'] = 3.8
20
+ # puts Params['parameter_float'] # Will produce --> 3.8
21
+ # Params['parameter_float'] = 3
22
+ # puts Params['parameter_float'] # Will produce --> 3.0 (note the casting)
23
+ # Note. only after definition, the [] operator will work. Else an error is raised.
24
+ # Params['new_param'] = true # will raise an error, since the param was not defined yet.
25
+ # Params.boolean 'new_param', true, 'this is correct' # this is the definition.
26
+ # Params['new_param'] = false # Will set new value.
27
+ # puts Params['new_param'] # Will produce --> false
28
+ #
29
+ # Type check.
30
+ # A type check is forced both when overriding with file or command line and in [] usage.
31
+ # if parameter was defined as a Float using Params.float method (e.g. 2.6) and user
32
+ # provided an integer when overriding or when using [] operator then the integer will
33
+ # be casted to float.
34
+ #
35
+ # Params.Init - override parameters.
36
+ # implementation of the init sequence allows the override of
37
+ # defined parameters with new values through input file and\or command line args.
38
+ # Note that only defined parameters can be overridden. If new parameter is parsed
39
+ # through file\command line, then an error will be raised.
40
+ # Input config file override:
41
+ # Default configuration input file path is: '~/.bbfs/conf/<executable name>.conf'
42
+ # This path can be overridden by the command line arguments (see ahead).
43
+ # If path and file exist then the parameters in the file will override the defined
44
+ # parameters. File parameters format per line:<param_name>: <param value>
45
+ # Note: Space char after the colon is mandatory
46
+ # Command line arguments override:
47
+ # General format per argument is:--<param_name>=<param_value>
48
+ # those parameters will override the defined parameters and the file parameters.
49
+ # Override input config file:
50
+ # User can override the input file by using:--conf_file=<new_file_path>
51
+ #
52
+ # More examples in bbfs/examples/params/rb
53
+
54
+ require 'optparse'
55
+ require 'yaml'
56
+
2
57
  module BBFS
3
- module PARAMS
4
- def parameter(name, default_value, description)
5
- self.instance_variable_set '@' + name, default_value
6
- self.class.send('attr_accessor', name)
58
+ module Params
59
+
60
+ @init_debug_messages = []
61
+ @help_messages = []
62
+
63
+ def Params.get_init_messages
64
+ return @init_debug_messages
65
+ end
66
+
67
+ # Represents a parameter.
68
+ class Param
69
+ attr_accessor :name
70
+ attr_accessor :value
71
+ attr_accessor :desc
72
+ attr_accessor :type
73
+
74
+ # value_type_check method:
75
+ # 1. Check if member:'type' is one of:Integer, Float, String or Boolean.
76
+ # 2. input parameter:'value' class type is valid to override this parameter.
77
+ # 3. Return value. The value to override with correct type. A cast from integer to Float will
78
+ # be made for Float parameters which are set with integer values.
79
+ # 4. Check will be skipped for nil value.
80
+ def value_type_check(value)
81
+ case( @type )
82
+ when 'Integer' then
83
+ if not @value.nil?
84
+ if not ((value.class.eql? Integer) or
85
+ (value.class.eql? Fixnum))
86
+ raise "Parameter:'#{@name}' type:'Integer' but value type to override " \
87
+ "is:'#{value.class}'."
88
+ end
89
+ end
90
+ when 'Float' then
91
+ if not @value.nil?
92
+ if not value.class.eql? Float
93
+ if not ((value.class.eql? Integer) or
94
+ (value.class.eql? Fixnum))
95
+ raise("Parameter:'#{@name}' type:'Float' but value type to override " \
96
+ "is:'#{value.class}'.")
97
+ else
98
+ return value.to_f
99
+ end
100
+ end
101
+ end
102
+ when 'String' then
103
+ if not @value.nil?
104
+ if not value.class.eql? String
105
+ raise("Parameter:'#{@name}' type:'String' but value type to override " \
106
+ "is:'#{value.class}'.")
107
+ end
108
+ end
109
+ when 'Boolean' then
110
+ if not @value.nil?
111
+ if not((value.class.eql? TrueClass) or (value.class.eql? FalseClass))
112
+ raise("Parameter:'#{@name}' type:'Boolean' but value type to override " \
113
+ "is:'#{value.class}'.")
114
+ end
115
+ end
116
+ else
117
+ raise("Parameter:'#{@name}' type:'#{@value.class}' but parameter " \
118
+ "type to override:'#{value.class}' is not supported. " + \
119
+ "Supported types are:Integer, Float, String or Boolean.")
120
+ end
121
+ return value
122
+ end
123
+
124
+ # supported types are: String, Integer, Float and Boolean
125
+ def initialize(name, value, type, desc)
126
+ @name = name
127
+ @type = type
128
+ @desc = desc
129
+ @value = value_type_check value
130
+ end
131
+ end
132
+
133
+ # The globals data structure.
134
+ @globals_db = Hash.new
135
+
136
+ def Params.raise_error_if_param_does_not_exist(name)
137
+ if not @globals_db[name]
138
+ raise("before using parameter:'#{name}', it should first be defined through Param module methods:" \
139
+ "Params.string, Params.integer, Params.float or Params.boolean.")
140
+ end
141
+ end
142
+
143
+ def Params.raise_error_if_param_exists(name)
144
+ if @globals_db[name]
145
+ raise("Parameter:'#{name}', can only be defined once.")
146
+ end
147
+ end
148
+
149
+ # Read global param value by other modules.
150
+ # Note that this operator should only be used, after parameter has been defined through
151
+ # one of Param module methods: Params.string, Params.integer,
152
+ # Params.float or Params.boolean."
153
+ def Params.[](name)
154
+ raise_error_if_param_does_not_exist(name)
155
+ @globals_db[name].value
156
+ end
157
+
158
+ # Write global param value by other modules.
159
+ # Note that this operator should only be used, after parameter has been defined through
160
+ # one of Param module methods: Params.string, Params.integer,
161
+ # Params.float or Params.boolean."
162
+ def Params.[]=(name, value)
163
+ raise_error_if_param_does_not_exist(name)
164
+ set_value = @globals_db[name].value_type_check(value)
165
+ @globals_db[name].value = set_value
166
+ end
167
+
168
+ #override parameter should only be called by Params module methods.
169
+ def Params.override_param(name, value)
170
+ existing_param = @globals_db[name]
171
+ if existing_param.nil?
172
+ raise("Parameter:'#{name}' has not been defined and can not be overridden. " \
173
+ "It should first be defined through Param module methods:" \
174
+ "Params.string, Params.integer, Params.float or Params.boolean.")
175
+ end
176
+ if value.nil?
177
+ existing_param.value = nil
178
+ elsif existing_param.type.eql?('String')
179
+ existing_param.value = value.to_s
180
+ else
181
+ set_value = existing_param.value_type_check(value)
182
+ existing_param.value = set_value
183
+ end
184
+ end
185
+
186
+ # Define new global parameter of type Integer.
187
+ def Params.integer(name, value, description)
188
+ raise_error_if_param_exists(name)
189
+ @globals_db[name] = Param.new(name, value, 'Integer', description)
190
+ end
191
+
192
+ # Define new global parameter of type Float.
193
+ def Params.float(name, value, description)
194
+ raise_error_if_param_exists(name)
195
+ @globals_db[name] = Param.new(name, value, 'Float', description)
196
+ end
197
+
198
+ # Define new global parameter of type String.
199
+ def Params.string(name, value, description)
200
+ raise_error_if_param_exists(name)
201
+ @globals_db[name] = Param.new(name, value, 'String', description)
202
+ end
203
+
204
+ # Define new global parameter of type Boolean.
205
+ def Params.boolean(name, value, description)
206
+ raise_error_if_param_exists(name)
207
+ @globals_db[name] = Param.new(name, value, 'Boolean', description)
208
+ end
209
+
210
+ # Initializes the project parameters.
211
+ # Precedence is: Defined params, file and command line is highest.
212
+ def Params.init(args)
213
+ @init_debug_messages = []
214
+ results = parse_command_line_arguments(args)
215
+ if not results['conf_file'].nil?
216
+ @init_debug_messages << "Configuration file was overridden. New path:'#{results['conf_file']}'"
217
+ Params['conf_file'] = results['conf_file']
218
+ end
219
+
220
+ #load yml params
221
+ if (not Params['conf_file'].nil?) and (File.exist?(File.expand_path Params['conf_file']))
222
+ @init_debug_messages << 'Configuration file path exists. Loading file parameters.'
223
+ read_yml_params(File.open(Params['conf_file'], 'r'))
224
+ else
225
+ @init_debug_messages << 'Configuration file path does not exist. Skipping loading file parameters.'
226
+ end
227
+
228
+ #override command line argument
229
+ results.keys.each do |result_name|
230
+ override_param(result_name, results[result_name])
231
+ end
232
+
233
+ print_global_parameters
234
+
235
+ # Prints help and parameters if needed.
236
+ puts @help_messages unless @help_messages.empty?
237
+ puts @init_debug_messages if Params['print_params_to_stdout']
238
+ exit unless @help_messages.empty?
239
+ end
240
+
241
+ # Load yml params and override default values.
242
+ # raise exception if a loaded yml param does not exist. or if types mismatch occurs.
243
+ def Params.read_yml_params(yml_input_io)
244
+ proj_params = YAML::load(yml_input_io)
245
+ proj_params.keys.each do |param_name|
246
+ override_param(param_name, proj_params[param_name])
247
+ end
248
+ end
249
+
250
+ # Parse command line arguments
251
+ def Params.parse_command_line_arguments(args)
252
+ results = Hash.new # Hash to store parsing results.
253
+ options = Hash.new # Hash of parsing options from Params.
254
+
255
+ # Define options switch for parsing
256
+ # Define List of options see example on
257
+ # http://ruby.about.com/od/advancedruby/a/optionparser2.htm
258
+ opts = OptionParser.new do |opts|
259
+ @globals_db.values.each do |param|
260
+ tmp_name_long = "--" + param.name + "=MANDATORY" # Define a command with single mandatory parameter
261
+ tmp_value = "Default value:" + param.value.to_s # Description and Default value
262
+
263
+ # Define type of the mandatory value
264
+ # It can be integer, float or String(for all other types).
265
+ case( param.type )
266
+ when 'Integer' then value_type = Integer
267
+ when 'Float' then value_type = Float
268
+ else value_type = String
269
+ end
270
+ # Switches definition - according to what
271
+ # was pre-defined in the Params
272
+ opts.on(tmp_name_long, value_type, tmp_value) do |result|
273
+ if result.to_s.upcase.eql? 'TRUE'
274
+ results[param.name] = true
275
+ elsif result.to_s.upcase.eql? 'FALSE'
276
+ results[param.name] = false
277
+ else
278
+ results[param.name] = result
279
+ end
280
+ end
281
+ end
282
+
283
+ # Define help command for available options
284
+ # executing --help will printout all pre-defined switch options
285
+ opts.on_tail("-h", "--help", "Show this message") do
286
+ @help_messages << opts
287
+ end
288
+
289
+ opts.parse(args) # Parse command line
290
+ end
291
+ return results
292
+ end # end of Parse function
293
+
294
+ def Params.print_global_parameters
295
+ @init_debug_messages << "\n"
296
+ @init_debug_messages << 'Initialized global parameters:'
297
+ @init_debug_messages << '---------------------------------'
298
+ counter=0
299
+ @globals_db.values.each do |param|
300
+ counter += 1
301
+ @init_debug_messages << "#{counter}: #{param.name}=#{param.value}"
302
+ end
303
+ @init_debug_messages << '---------------------------------'
304
+ end
305
+
306
+ #Auxiliary method to retrieve the executable name
307
+ def Params.executable_name
308
+ /([a-zA-Z0-9\-_\.]+):\d+/ =~ caller[caller.size-1]
309
+ return $1
310
+ end
311
+ #define default params:
312
+ # 1. configuration file
313
+ Params.string('conf_file', File.expand_path("~/.bbfs/conf/#{executable_name}.conf"), 'Default configuration file.')
314
+ # 2. Print params to stdout
315
+ Params.boolean('print_params_to_stdout', false, 'print_params_to_stdout or not during Params.init')
316
+
317
+ private_class_method :print_global_parameters, :parse_command_line_arguments, \
318
+ :raise_error_if_param_exists, :raise_error_if_param_does_not_exist, \
319
+ :read_yml_params, :override_param, :executable_name
7
320
  end
8
- module_function :parameter
9
- end
10
321
  end
11
322
 
@@ -0,0 +1,282 @@
1
+ # Author: Yaron Dror (yaron.dror.bb@gmail.com)
2
+ # Description: The file contains 'Param' module tests
3
+
4
+ require 'rspec'
5
+ require 'yaml'
6
+
7
+ require_relative '../../lib/params.rb'
8
+
9
+ module BBFS
10
+ module Params
11
+ # make private methods or Params public for testing capability.
12
+ public_class_method :print_global_parameters, :parse_command_line_arguments, \
13
+ :raise_error_if_param_exists, :raise_error_if_param_does_not_exist, \
14
+ :read_yml_params, :override_param
15
+
16
+ module Spec
17
+
18
+ describe 'Params::parameter' do
19
+
20
+ it 'should define a new parameters' do
21
+ Params.string 'par_str', 'sss' ,'desc_str'
22
+ Params.integer 'par_int',1 , 'desc_int'
23
+ Params.float 'par_float',2.6 , 'desc_float'
24
+ Params.boolean 'par_true', true, 'desc_true'
25
+ Params.boolean 'par_false',false , 'desc_false'
26
+ Params['par_str'].should eq 'sss'
27
+ Params['par_int'].should eq 1
28
+ Params['par_float'].should eq 2.6
29
+ Params['par_true'].should eq true
30
+ Params['par_false'].should eq false
31
+ end
32
+
33
+ it 'should raise an error for wrong parameter type definition.' do
34
+ expect { Params::Param.new 'bad_type', nil, 'non_existing_type', 'desc_bad_type' }.should raise_error
35
+ end
36
+
37
+ it 'should raise an error when trying to define twice the same parameter' do
38
+ Params.string 'only_once', '1st' ,''
39
+ expect { Params.string 'only_once', '2nd' ,'' }.to raise_error \
40
+ "Parameter:'only_once', can only be defined once."
41
+ end
42
+ end
43
+
44
+ describe 'Params::read_yml_params' do
45
+ it 'should raise error when yml parameter is not defined' do
46
+ expect { Params::read_yml_params StringIO.new 'not_defined: 10' }.to raise_error \
47
+ "Parameter:'not_defined' has not been defined and can not be overridden. " \
48
+ "It should first be defined through Param module methods:" \
49
+ "Params.string, Params.integer, Params.float or Params.boolean."
50
+ end
51
+
52
+ it 'Will test yml parameter loading' do
53
+ # string to other. Will not raise error. Instead a cast is made.
54
+ Params.string('tmp4str', 'string_value', 'tmp4 def')
55
+ expect { Params::read_yml_params StringIO.new 'tmp4str: strr' }.to_not raise_error
56
+ expect { Params::read_yml_params StringIO.new 'tmp4str: 4' }.to_not raise_error
57
+ expect { Params::read_yml_params StringIO.new 'tmp4str: 4.5' }.to_not raise_error
58
+ expect { Params::read_yml_params StringIO.new 'tmp4str: true' }.to_not raise_error
59
+ expect { Params::read_yml_params StringIO.new 'tmp4str: false' }.to_not raise_error
60
+
61
+ # override integer with other types.
62
+ Params.integer('tmp4int', 1, 'tmp4 def')
63
+ expect { Params::read_yml_params StringIO.new 'tmp4int: strr' }.to raise_error \
64
+ "Parameter:'tmp4int' type:'Integer' but value type to override " \
65
+ "is:'String'."
66
+ expect { Params::read_yml_params StringIO.new 'tmp4int: 4' }.to_not raise_error
67
+ expect { Params::read_yml_params StringIO.new 'tmp4int: 4.5' }.to raise_error \
68
+ "Parameter:'tmp4int' type:'Integer' but value type to override " \
69
+ "is:'Float'."
70
+ expect { Params::read_yml_params StringIO.new 'tmp4int: true' }.to raise_error \
71
+ "Parameter:'tmp4int' type:'Integer' but value type to override " \
72
+ "is:'TrueClass'."
73
+ expect { Params::read_yml_params StringIO.new 'tmp4int: false' }.to raise_error \
74
+ "Parameter:'tmp4int' type:'Integer' but value type to override " \
75
+ "is:'FalseClass'."
76
+
77
+ # override float with other types.
78
+ Params.float('tmp4float', 1.1, 'tmp4 def')
79
+ expect { Params::read_yml_params StringIO.new 'tmp4float: strr' }.to raise_error \
80
+ "Parameter:'tmp4float' type:'Float' but value type to override " \
81
+ "is:'String'."
82
+ expect { Params::read_yml_params StringIO.new 'tmp4float: 4' }.to_not raise_error
83
+ expect { Params::read_yml_params StringIO.new 'tmp4float: 4.5' }.to_not raise_error
84
+ expect { Params::read_yml_params StringIO.new 'tmp4float: true' }.to raise_error \
85
+ "Parameter:'tmp4float' type:'Float' but value type to override " \
86
+ "is:'TrueClass'."
87
+ expect { Params::read_yml_params StringIO.new 'tmp4float: false' }.to raise_error \
88
+ "Parameter:'tmp4float' type:'Float' but value type to override " \
89
+ "is:'FalseClass'."
90
+ # override boolean with other types.
91
+ Params.boolean('tmp4true', true, 'tmp4 def')
92
+ expect { Params::read_yml_params StringIO.new 'tmp4true: strr' }.to raise_error \
93
+ "Parameter:'tmp4true' type:'Boolean' but value type to override " \
94
+ "is:'String'."
95
+ expect { Params::read_yml_params StringIO.new 'tmp4true: 4' }.to raise_error \
96
+ "Parameter:'tmp4true' type:'Boolean' but value type to override " \
97
+ "is:'Fixnum'."
98
+ expect { Params::read_yml_params StringIO.new 'tmp4true: 4.5' }.to raise_error \
99
+ "Parameter:'tmp4true' type:'Boolean' but value type to override " \
100
+ "is:'Float'."
101
+ expect { Params::read_yml_params StringIO.new 'tmp4true: true' }.to_not raise_error
102
+ expect { Params.read_yml_params StringIO.new 'tmp4true: false' }.to_not raise_error
103
+
104
+ Params.boolean('tmp4False', true, 'tmp4 def')
105
+ expect { Params.read_yml_params StringIO.new 'tmp4False: strr' }.to raise_error \
106
+ "Parameter:'tmp4False' type:'Boolean' but value type to override " \
107
+ "is:'String'."
108
+ expect { Params.read_yml_params StringIO.new 'tmp4False: 4' }.to raise_error \
109
+ "Parameter:'tmp4False' type:'Boolean' but value type to override " \
110
+ "is:'Fixnum'."
111
+ expect { Params.read_yml_params StringIO.new 'tmp4False: 4.5' }.to raise_error \
112
+ "Parameter:'tmp4False' type:'Boolean' but value type to override " \
113
+ "is:'Float'."
114
+ expect { Params.read_yml_params StringIO.new 'tmp4False: true' }.to_not raise_error
115
+ expect { Params.read_yml_params StringIO.new 'tmp4False: false' }.to_not raise_error
116
+
117
+ end
118
+
119
+ it 'should raise error when yml file format is bad' do
120
+ expect { Params.read_yml_params StringIO.new 'bad yml format' }.to raise_error
121
+ end
122
+
123
+ it 'should override defined values with yml values' do
124
+ Params.string('tmp5str', 'aaa', 'tmp5 def')
125
+ Params.integer('tmp5int', 11, 'tmp5 def')
126
+ Params.float('tmp5float', 11.11, 'tmp5 def')
127
+ Params.boolean('tmp5true', true, 'tmp5 def')
128
+ Params.boolean('tmp5false', false, 'tmp5 def')
129
+ Params.read_yml_params StringIO.new "tmp5str: bbb\ntmp5int: 12\ntmp5float: 12.12\n"
130
+ Params.read_yml_params StringIO.new "tmp5true: false\ntmp5false: true\n"
131
+ Params['tmp5str'].should eq 'bbb'
132
+ Params['tmp5int'].should eq 12
133
+ Params['tmp5float'].should eq 12.12
134
+ Params['tmp5true'].should eq false
135
+ Params['tmp5false'].should eq true
136
+ end
137
+ end
138
+
139
+ describe 'Params.parse_command_line_arguments' do
140
+ it 'should raise error when command line parameter is not defined' do
141
+ expect { Params.parse_command_line_arguments ['--new_param=9]'] }.to raise_error
142
+ end
143
+
144
+ it 'should parse parameter from command line.' do
145
+ # Override string with types.
146
+ Params.string('tmp6str', 'dummy', 'tmp6str def')
147
+ expect { Params.parse_command_line_arguments ['--tmp6str=9'] }.to_not raise_error
148
+ expect { Params.parse_command_line_arguments ['--tmp6str=8.1'] }.to_not raise_error
149
+ expect { Params.parse_command_line_arguments ['--tmp6str=ff'] }.to_not raise_error
150
+ expect { Params.parse_command_line_arguments ['--tmp6str=true'] }.to_not raise_error
151
+ expect { Params.parse_command_line_arguments ['--tmp6str=false'] }.to_not raise_error
152
+
153
+ # from fixnum to other types.
154
+ Params.integer('tmp6', 8, 'tmp6 def')
155
+ expect { Params.parse_command_line_arguments ['--tmp6=9'] }.to_not raise_error
156
+ expect { Params.parse_command_line_arguments ['--tmp6=8.1'] }.to raise_error
157
+ expect { Params.parse_command_line_arguments ['--tmp6=ff'] }.to raise_error
158
+ expect { Params.parse_command_line_arguments ['--tmp6=true'] }.to raise_error
159
+ expect { Params.parse_command_line_arguments ['--tmp6=false'] }.to raise_error
160
+
161
+ # from float to other types.
162
+ Params.float('tmp7', 8.9, 'tmp7 def')
163
+ # Casting fix num to float
164
+ expect { Params.parse_command_line_arguments ['--tmp7=9'] }.to_not raise_error
165
+ expect { Params.parse_command_line_arguments ['--tmp7=3.4'] }.to_not raise_error
166
+ expect { Params.parse_command_line_arguments ['--tmp7=ff'] }.to raise_error
167
+ expect { Params.parse_command_line_arguments ['--tmp7=true'] }.to raise_error
168
+ expect { Params.parse_command_line_arguments ['--tmp7=false'] }.to raise_error
169
+
170
+ # from TrueClass to other types.
171
+ Params.boolean('tmp8', true, 'tmp8 def')
172
+ expect { Params.parse_command_line_arguments ['--tmp8=9'] }.to_not raise_error
173
+ expect { Params.parse_command_line_arguments ['--tmp8=3.4'] }.to_not raise_error
174
+ expect { Params.parse_command_line_arguments ['--tmp8=ff'] }.to_not raise_error
175
+ expect { Params.parse_command_line_arguments ['--tmp8=true'] }.to_not raise_error
176
+ expect { Params.parse_command_line_arguments ['--tmp8=false'] }.to_not raise_error
177
+
178
+ # from FalseClass to other types.
179
+ Params.boolean('tmp9', false, 'tmp9 def')
180
+ expect { Params.parse_command_line_arguments ['--tmp9=9'] }.to_not raise_error
181
+ expect { Params.parse_command_line_arguments ['--tmp9=3.4'] }.to_not raise_error
182
+ expect { Params.parse_command_line_arguments ['--tmp9=ff'] }.to_not raise_error
183
+ expect { Params.parse_command_line_arguments ['--tmp9=true'] }.to_not raise_error
184
+ expect { Params.parse_command_line_arguments ['--tmp9=false'] }.to_not raise_error
185
+ end
186
+ end
187
+
188
+ describe 'Params.init' do
189
+ it 'should override command line arguments correctly.' do
190
+ File.stub(:exist?).and_return false
191
+ # Override string with types.
192
+ Params.string('tmp6str2', 'dummy', 'tmp6str def')
193
+ expect { Params.init ['--tmp6str2=9'] }.to_not raise_error
194
+ expect { Params.init ['--tmp6str2=8.1'] }.to_not raise_error
195
+ expect { Params.init ['--tmp6str2=ff'] }.to_not raise_error
196
+ expect { Params.init ['--tmp6str2=true'] }.to_not raise_error
197
+ expect { Params.init ['--tmp6str2=false'] }.to_not raise_error
198
+
199
+ # from fixnum to other types.
200
+ Params.integer('tmp6Fixnum2', 8, 'tmp6 def')
201
+ expect { Params.init ['--tmp6Fixnum2=9'] }.to_not raise_error
202
+ expect { Params.init ['--tmp6Fixnum2=8.1'] }.to raise_error
203
+ expect { Params.init ['--tmp6Fixnum2=ff'] }.to raise_error
204
+ expect { Params.init ['--tmp6Fixnum2=true'] }.to raise_error
205
+ expect { Params.init ['--tmp6Fixnum2=false'] }.to raise_error
206
+
207
+ # from float to other types.
208
+ Params.float('tmp7float2', 8.9, 'tmp7 def')
209
+ # Casting fix num to float
210
+ expect { Params.init ['--tmp7float2=9'] }.to_not raise_error
211
+ expect { Params.init ['--tmp7float2=3.4'] }.to_not raise_error
212
+ expect { Params.init ['--tmp7float2=ff'] }.to raise_error
213
+ expect { Params.init ['--tmp7float2=true'] }.to raise_error
214
+ expect { Params.init ['--tmp7float2=false'] }.to raise_error
215
+
216
+ # from TrueClass to other types.
217
+ Params.boolean('tmp8true2', true, 'tmp8 def')
218
+ expect { Params.init ['--tmp8true2=9'] }.to raise_error
219
+ expect { Params.init ['--tmp8true2=3.4'] }.to raise_error
220
+ expect { Params.init ['--tmp8true2=ff'] }.to raise_error
221
+ expect { Params.init ['--tmp8true2=true'] }.to_not raise_error
222
+ expect { Params.init ['--tmp8true2=false'] }.to_not raise_error
223
+
224
+ # from FalseClass to other types.
225
+ Params.boolean('tmp9false2', false, 'tmp9 def')
226
+ expect { Params.init ['--tmp9false2=9'] }.to raise_error
227
+ expect { Params.init ['--tmp9false2=3.4'] }.to raise_error
228
+ expect { Params.init ['--tmp9false2=ff'] }.to raise_error
229
+ expect { Params.init ['--tmp9false2=true'] }.to_not raise_error
230
+ expect { Params.init ['--tmp9false2=false'] }.to_not raise_error
231
+ end
232
+
233
+ it 'should override defined values with command line values' do
234
+ File.stub(:exist?).and_return false
235
+ Params.string('tmp10str', 'aaa', 'tmp10 def')
236
+ Params.integer('tmp10int', 11, 'tmp10 def')
237
+ Params.float('tmp10float', 11.11, 'tmp10 def')
238
+ Params.boolean('tmp10true', true, 'tmp10 def')
239
+ Params.boolean('tmp10false', false, 'tmp10 def')
240
+ Params.init ['--tmp10str=bbb', '--tmp10int=12', '--tmp10float=12.12', \
241
+ '--tmp10true=false', '--tmp10false=true']
242
+ Params['tmp10str'].should eq 'bbb'
243
+ Params['tmp10int'].should eq 12
244
+ Params['tmp10float'].should eq 12.12
245
+ Params['tmp10true'].should eq false
246
+ Params['tmp10false'].should eq true
247
+ end
248
+
249
+ it 'init should override parameters with file and command' do
250
+ Params.string('init_param', 'code', '')
251
+ File.stub(:exist?).and_return true
252
+ File.stub(:open).and_return StringIO.new 'init_param: yml'
253
+ Params.init ['--conf_file=dummy_file', '--init_param=command-line']
254
+ Params['init_param'].should eq 'command-line'
255
+ end
256
+
257
+ it 'init should override parameters with file only' do
258
+ Params.string('init_param2', 'code', '')
259
+ File.stub(:exist?).and_return true
260
+ File.stub(:open).and_return StringIO.new 'init_param2: yml'
261
+ Params.init ['--conf_file=dummy_file']
262
+ Params['init_param2'].should eq 'yml'
263
+ end
264
+
265
+ it 'init should override parameters with command line only' do
266
+ Params.string('init_param3', 'code', '')
267
+ File.stub(:exist?).and_return false
268
+ Params.init ['--init_param3=command-line']
269
+ Params['init_param3'].should eq 'command-line'
270
+ end
271
+
272
+ it 'init should not override any parameters' do
273
+ Params.string('init_param4', 'code', '')
274
+ File.stub(:exist?).and_return false
275
+ Params.init []
276
+ Params['init_param4'].should eq 'code'
277
+ end
278
+ end
279
+ end
280
+ end
281
+ end
282
+
@@ -0,0 +1,45 @@
1
+ # Author: Yaron Dror (yaron.dror.bb@gmail.com)
2
+ # Description: Params test file.
3
+ # run: rake test.
4
+ # Note: This file will be tested along with all project tests.
5
+
6
+ require 'params'
7
+ require 'test/unit'
8
+ require_relative '../../lib/params.rb'
9
+
10
+ module BBFS
11
+
12
+ class TestLog < Test::Unit::TestCase
13
+
14
+ def test_parsing_of_the_defined_parameters
15
+ # Define options
16
+ Params.integer('remote_server1', 3333,
17
+ 'Listening port for backup server content data.')
18
+ Params.string('backup_username1', 'tmp', 'Backup server username.')
19
+ Params.string('backup_password1', 'tmp', 'Backup server password.')
20
+ Params.string('backup_destination_folder1', '',
21
+ 'Backup server destination folder, default is the relative local folder.')
22
+ Params.string('content_data_path1', File.expand_path('~/.bbfs/var/content.data'),
23
+ 'ContentData file path.')
24
+ Params.string('monitoring_config_path1', File.expand_path('~/.bbfs/etc/file_monitoring.yml'),
25
+ 'Configuration file for monitoring.')
26
+ Params.float('time_to_start1', 0.03,
27
+ 'Time to start monitoring')
28
+
29
+ cmd = [ '--remote_server1=2222','--backup_username1=rami','--backup_password1=kavana',
30
+ '--backup_destination_folder1=C:\Users\Alexey\Backup',
31
+ '--content_data_path1=C:\Users\Alexey\Content',
32
+ '--monitoring_config_path1=C:\Users\Alexey\Config',
33
+ '--time_to_start1=1.5']
34
+ Params.init cmd
35
+ assert_equal(2222,Params['remote_server1'])
36
+ assert_equal('rami',Params['backup_username1'])
37
+ assert_equal('kavana',Params['backup_password1'])
38
+ assert_equal('C:\Users\Alexey\Backup',Params['backup_destination_folder1'])
39
+ assert_equal('C:\Users\Alexey\Content',Params['content_data_path1'])
40
+ assert_equal('C:\Users\Alexey\Config',Params['monitoring_config_path1'])
41
+ assert_equal(1.5,Params['time_to_start1'])
42
+ end
43
+ end
44
+ end
45
+
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: params
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.8
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-04-11 00:00:00.000000000Z
13
+ date: 2012-09-02 00:00:00.000000000 Z
14
14
  dependencies: []
15
15
  description: Dynamically stores, parses and providers params. Uses module local readers.
16
16
  email: kolmanv@gmail.com
@@ -19,7 +19,9 @@ extensions: []
19
19
  extra_rdoc_files: []
20
20
  files:
21
21
  - lib/params.rb
22
- - lib/params/argument_parser.rb
22
+ - lib/params/version.rb
23
+ - test/params/params_spec.rb
24
+ - test/params/params_test.rb
23
25
  homepage: http://github.com/kolmanv/bbfs
24
26
  licenses: []
25
27
  post_install_message:
@@ -40,8 +42,10 @@ required_rubygems_version: !ruby/object:Gem::Requirement
40
42
  version: '0'
41
43
  requirements: []
42
44
  rubyforge_project:
43
- rubygems_version: 1.8.15
45
+ rubygems_version: 1.8.23
44
46
  signing_key:
45
47
  specification_version: 3
46
48
  summary: Dynamically stores, parses and providers params.
47
- test_files: []
49
+ test_files:
50
+ - test/params/params_spec.rb
51
+ - test/params/params_test.rb
@@ -1,24 +0,0 @@
1
- class ReadArgs
2
- def self.read_arguments(argv, parsed_arguments, commands = nil)
3
- argv.each { |arg|
4
- if arg.start_with?("--")
5
- words = arg[2,arg.length-2].split('=')
6
- parsed_arguments[words[0]]=words[1]
7
- elsif commands != nil and not commands.key?(arg)
8
- puts "Unknown command '%s'." % arg
9
- return false
10
- else
11
- if parsed_arguments.key? arg
12
- puts "Parse error, two commands found %s and %s" % parsed_arguments["command"], arg
13
- return false
14
- end
15
- parsed_arguments["command"] = arg
16
- end
17
- }
18
-
19
- return false unless commands == nil or parsed_arguments["command"] != nil
20
- return false unless commands == nil or parsed_arguments["command"] != ""
21
-
22
- return true
23
- end
24
- end