cli_tool 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
@@ -77,16 +77,20 @@ module CliTool
77
77
  [option].compact.flatten.map { |x| optionify(x, retval) }
78
78
  end
79
79
 
80
- def trap!
81
- Signal.trap("INT") do |signo|
82
- if Signal.respond_to?(:signame)
83
- signal = Signal.signame(signo)
84
- puts "Received: #{signal}..."
85
- puts "Exiting..."
86
- else
87
- puts "#{signo} Exiting..."
80
+ def trap!(signal = "INT", &block)
81
+ Signal.trap(signal) do |signo|
82
+ return block.call(singal) if block.is_a?(Proc)
83
+
84
+ case signal
85
+ when 'SIGQUIT'
86
+ puts %{Received Signal "#{signal}"... Exiting...}
87
+ exit 0
88
+ when 'SIGKILL'
89
+ puts %{Received Signal "#{signal}"... Killing...}
90
+ exit 1
88
91
  end
89
- exit 1
92
+
93
+ puts %{Received Signal "#{signal}"... Don't know what to do...}
90
94
  end
91
95
  end
92
96
 
@@ -179,10 +183,10 @@ module CliTool
179
183
  exit_code = 1
180
184
  end
181
185
 
182
- # Get the missing options that were expected
183
- missing_options = class_vars[:required_options].keys - processed_options
186
+ # Get a array of all the options that are required and were not provided
187
+ missing_options = class_vars[:required_options].select{ |opt, arg| arg[:require] == true }.keys - processed_options
184
188
 
185
- # Handle missing options and their potential alternatives
189
+ # If a option was marked as missing; verify if an alternative was provided
186
190
  __slice_hash(class_vars[:required_options], *missing_options).each do |option, option_args|
187
191
  if (option_args[:alternatives] & processed_options).empty?
188
192
  object.help = true
@@ -192,27 +196,26 @@ module CliTool
192
196
  end
193
197
  end
194
198
 
195
- # Ensure that the dependencies for options are met (if applicable)
196
- __slice_hash(class_vars[:required_options], *processed_options).each do |option, option_args|
197
- missing_dependencies = option_args[:dependencies] - processed_options
198
- missing_dependencies.each do |dep_opt|
199
- puts "The option `--#{option}' expected a value for `--#{dep_opt}', but not was received", :red
200
- missing_options << dep_opt
201
-
202
- object.help = true
203
- exit_code = 1
204
- end
199
+ # Find any missing dependencies that may have been required by an option
200
+ missing_dependencies = __slice_hash(class_vars[:required_options], *processed_options).reduce({}) do |out, (option, option_args)|
201
+ missing = option_args[:dependencies] - processed_options
202
+ missing.reduce(out) { |o, missed| ((o[missed] ||= []) << option); o }
203
+ end
205
204
 
206
- puts '' unless missing_dependencies.empty?
205
+ # Let the user know that a dependency has not been met.
206
+ missing_dependencies.each do |dependency, options|
207
+ options = (options.length > 1 ? options.reduce('') { |s, o| s << "\n\t--#{o}" } : "--#{options.first}") << "\n"
208
+ puts "The option --#{dependency} is required by the following options: #{options}", :red
209
+ missing_options << dependency
210
+ object.help = true
211
+ exit_code = 1
207
212
  end
208
213
 
209
- # Raise an error when required options were not provided.
210
- # Change the exit code and enable help output (which will exit 1; on missing opts)
214
+ # Tell user that required options were not provided
211
215
  unless missing_options.empty?
212
- missing_options.uniq.each do |option|
213
- puts "The required option `--#{option}' was not provided.", :red
214
- end
215
-
216
+ options = missing_options.uniq
217
+ options = (options.length > 1 ? options.reduce('') { |s, o| s << "\n\t--#{o}" } : "--#{options.first}") << "\n"
218
+ puts "The following required options were not provided: #{options}", :red
216
219
  object.help = true
217
220
  exit_code = 1
218
221
  end
@@ -251,7 +254,7 @@ module CliTool
251
254
  end
252
255
 
253
256
  # Set up the options list
254
- message = "\t" + (option_args[:aliases] << option).map{ |x| "--#{x}#{long_dep}"}.join(', ')
257
+ message = "\t" + option_args[:aliases].map{ |x| "--#{x}#{long_dep}"}.join(', ')
255
258
  message << ", -#{option_args[:short]}#{short_dep}" if option_args[:short]
256
259
  message << %{ :: Default: "#{option_args[:default]}"} if option_args[:default]
257
260
 
@@ -296,7 +299,7 @@ module CliTool
296
299
  opts = opts.reduce({}) do |standardized_options, (options, option_args)|
297
300
  option_args = option_args.is_a?(Hash) ? option_args : {argument: option_args}
298
301
  option_args[:aliases] = optionify_all((option_args[:aliases] || []).concat([options]), :dash)
299
- standardized_options.merge(option_args[:aliases].shift => option_args)
302
+ standardized_options.merge(option_args[:aliases].first => option_args)
300
303
  end
301
304
 
302
305
  # Set the preprocessor for options (used primarily for formatting or changing types)
@@ -305,21 +308,15 @@ module CliTool
305
308
  end
306
309
 
307
310
  # Set the required options and dependencies for the parser
308
- __generic_option_reducer(:required_options, {}, opts, only_with: :required) do |req_opts, (option, option_args)|
309
-
310
- # Set the aliases for the required option
311
- hash = option_args[:required].is_a?(Hash) ? option_args[:required] : {}
311
+ __generic_option_reducer(:required_options, {}, opts, includes: [:require, :dependencies, :alternatives]) do |req_opts, (option, option_args)|
312
+ hash = __slice_hash(option_args, :require, :dependencies, :alternatives)
312
313
 
313
314
  # Ensure that the option names are properly formatted in the alternatives and dependencies
314
315
  hash[:dependencies] = optionify_all(hash[:dependencies], :dash)
315
316
  hash[:alternatives] = optionify_all(hash[:alternatives], :dash)
316
317
 
317
- # Merge together the options as required
318
- if option_args[:required].is_a?(Hash)
319
- req_opts.merge(option => hash)
320
- else
321
- req_opts.merge(option => hash.merge(force: !! option_args[:required]))
322
- end
318
+ # Apply the required options
319
+ req_opts.merge(option => hash)
323
320
  end
324
321
 
325
322
  # Create a list of the "secure options" (hide value from client; as much as possible)
@@ -342,7 +339,7 @@ module CliTool
342
339
  # Create the Getoptlong syntax
343
340
  __generic_option_reducer(:gol_options, [], opts) do |gol_options, (option, option_args)|
344
341
  argument_dependency = GOL_MAP[option_args[:argument]]
345
- aliases = option_args[:aliases] || []
342
+ aliases = option_args[:aliases][1..-1] || []
346
343
 
347
344
  # Create the attribute accessor
348
345
  attr_accessor(optionify(option))
@@ -381,10 +378,15 @@ module CliTool
381
378
 
382
379
  # Require certain keys be in the option cache. This is done to save time of the processing and make it easier to write
383
380
  # the code to parse the options and set the requirements and dependencies of each option.
384
- if args[:only_with]
381
+ if args[:only_with] && args[:includes].nil?
385
382
  opts = opts.select { |k, v| v.is_a?(Hash) && [args[:only_with]].flatten.reduce(true) { |o, key| o && v.has_key?(key) } }
386
383
  end
387
384
 
385
+ # Just like only_with except use an or condition instead
386
+ if args[:includes] && args[:only_with].nil?
387
+ opts = opts.select { |k, v| v.is_a?(Hash) && [args[:includes]].flatten.reduce(false) { |o, key| o || v.has_key?(key) } }
388
+ end
389
+
388
390
  # Run the reducer and set the class variables accordingly
389
391
  class_variable_set("@@__#{instance_var}", default) unless class_variable_defined?("@@__#{instance_var}")
390
392
  cached_value = class_variable_get("@@__#{instance_var}") || default
@@ -1,3 +1,3 @@
1
1
  module CliTool
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cli_tool
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-02-26 00:00:00.000000000 Z
12
+ date: 2014-02-27 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler