cli_tool 0.1.0 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,13 +8,14 @@ module CliTool
8
8
  # Use to add the methods below to any class
9
9
  def self.included(base)
10
10
  base.extend(ClassMethods)
11
+ base.__send__(:include, ::CliTool::StdinOut)
11
12
  base.options({
12
13
  debug: {
13
14
  argument: :none,
14
15
  documentation: [
15
16
  "This is used to trigger debug mode in your app. It will be set to #{base}.debug.",
16
17
  "In debug mode we do not respect the secure option and your secure fields will be displayed in clear text!",
17
- [:black, :white_bg]
18
+ [:black, :red_bg]
18
19
  ]
19
20
  },
20
21
  help: {
@@ -29,20 +30,16 @@ module CliTool
29
30
 
30
31
  # Map for symbol types
31
32
  GOL_MAP = {
32
- none: GetoptLong::NO_ARGUMENT,
33
+ none: GetoptLong::NO_ARGUMENT,
33
34
  optional: GetoptLong::OPTIONAL_ARGUMENT,
34
35
  required: GetoptLong::REQUIRED_ARGUMENT
35
36
  }
36
37
 
37
38
  # Create the options array
38
39
  def options(opts = nil)
39
- @@options ||= []
40
-
41
- # If no options were passed then return
42
- return @@options.uniq unless opts
43
-
44
- _create_preprocess_(opts)
45
- @@options = @@options.concat(_create_gola_(opts)).uniq
40
+ return (__get_options(:gol_options) || []).uniq unless opts
41
+ __process_options(opts)
42
+ true
46
43
  end
47
44
 
48
45
  # Ensure the right format of the options (primarily dashes and casing)
@@ -57,21 +54,17 @@ module CliTool
57
54
  case retval
58
55
  when :set, :setter
59
56
  "#{option}="
57
+ when :dash, :hyphen, :clize
58
+ "#{option}".gsub(/=$/, '').gsub('_', '-')
60
59
  when :primary
61
60
  all_opts = __get_options(:all_options)
62
-
63
- if all_opts.has_key?(option)
64
- option
65
- else
66
- all_opts.map { |opt, option_args|
67
- aliases = option_args[:aliases]
68
- if aliases && aliases.include?(option)
69
- opt
70
- else
71
- nil
72
- end
73
- }.compact.flatten.first
74
- end
61
+ hyphenated = optionify(option, :hyphen)
62
+ option = all_opts.has_key?(hyphenated) ? option : Proc.new {
63
+ all_opts.reduce(nil) do |result, (opt, opt_args)|
64
+ [opt].concat(opt_args[:aliases]).include?(hyphenated) ? opt : result
65
+ end
66
+ }.call
67
+ optionify(option)
75
68
  else
76
69
  option
77
70
  end
@@ -121,7 +114,8 @@ module CliTool
121
114
 
122
115
  # Option setter proc
123
116
  option_setter = Proc.new do |option, value|
124
- option = optionify(option, :primary)
117
+ option_attr = optionify(option, :primary)
118
+ option = optionify(option_attr, :dash)
125
119
  processed_options << option
126
120
 
127
121
  # Help process values
@@ -146,18 +140,19 @@ module CliTool
146
140
  end
147
141
 
148
142
  # Show notice of the setting being set on the instance
149
- if class_vars[:private_options].include?(option) && ! instance.debug
150
- m = "Setting @#{option} = #{'*' * value.length} :: Value hidden for privacy"
143
+ if class_vars[:private_options].include?(option) && ! object.debug
144
+ value_hidden = value.respond_to?(:length) ? ('*' * value.length).inspect : colorize('<hidden>', :italic)
145
+ m = "Setting @#{option_attr} = #{value_hidden} :: Value hidden for privacy"
151
146
  max_puts_length = m.length if m.length > max_puts_length
152
147
  puts m, :blue
153
148
  else
154
- m = "Setting @#{option} = #{value}"
149
+ m = "Setting @#{option_attr} = #{value.inspect}"
155
150
  max_puts_length = m.length if m.length > max_puts_length
156
151
  puts m, :green
157
152
  end
158
153
 
159
154
  # Do the actual set for us
160
- object.__send__(optionify(option, :set), value)
155
+ object.__send__(optionify(option_attr, :set), value)
161
156
  end
162
157
 
163
158
  # Actually grab the options from GetoptLong and process them
@@ -296,112 +291,93 @@ module CliTool
296
291
 
297
292
  private
298
293
 
299
- def _create_preprocess_(opts)
294
+ def __process_options(opts)
295
+ # Standardize the options input to have hash arguments and hypenated options
296
+ opts = opts.reduce({}) do |standardized_options, (options, option_args)|
297
+ option_args = option_args.is_a?(Hash) ? option_args : {argument: option_args}
298
+ option_args[:aliases] = optionify_all((option_args[:aliases] || []).concat([options]), :dash)
299
+ standardized_options.merge(option_args[:aliases].shift => option_args)
300
+ end
300
301
 
301
302
  # Set the preprocessor for options (used primarily for formatting or changing types)
302
- __generic_option_reducer(:preprocessors, {}, opts, only_with: :preprocess) do |pre_proc, (options, option_args)|
303
- pre_proc.merge(options.shift => option_args[:preprocess])
303
+ __generic_option_reducer(:preprocessors, {}, opts, only_with: :preprocess) do |pre_proc, (option, option_args)|
304
+ pre_proc.merge(option => option_args[:preprocess])
304
305
  end
305
306
 
306
307
  # Set the required options and dependencies for the parser
307
- __generic_option_reducer(:required_options, {}, opts, only_with: :required) do |req_opts, (options, option_args)|
308
- primary_name = options.shift
308
+ __generic_option_reducer(:required_options, {}, opts, only_with: :required) do |req_opts, (option, option_args)|
309
309
 
310
310
  # Set the aliases for the required option
311
- hash = (option_args[:required].is_a?(Hash) ? option_args[:required] : {}).merge(aliases: options)
311
+ hash = option_args[:required].is_a?(Hash) ? option_args[:required] : {}
312
312
 
313
313
  # Ensure that the option names are properly formatted in the alternatives and dependencies
314
- hash[:dependencies] = optionify_all(hash[:dependencies])
315
- hash[:alternatives] = optionify_all(hash[:alternatives])
314
+ hash[:dependencies] = optionify_all(hash[:dependencies], :dash)
315
+ hash[:alternatives] = optionify_all(hash[:alternatives], :dash)
316
316
 
317
317
  # Merge together the options as required
318
318
  if option_args[:required].is_a?(Hash)
319
- req_opts.merge(primary_name => hash)
320
- else
321
- req_opts.merge(primary_name => hash.merge(force: !! option_args[:required]))
322
- end
323
- end
324
-
325
- # Create a cache of all the options available
326
- __generic_option_reducer(:all_options, {}, opts) do |opt_cache, (options, option_args)|
327
- primary_name = options.shift
328
-
329
- if option_args.is_a?(Hash)
330
- opt_cache.merge(primary_name => option_args.merge(aliases: options))
319
+ req_opts.merge(option => hash)
331
320
  else
332
- opt_cache.merge(primary_name => {aliases: options, argument: option_args})
321
+ req_opts.merge(option => hash.merge(force: !! option_args[:required]))
333
322
  end
334
323
  end
335
324
 
336
325
  # Create a list of the "secure options" (hide value from client; as much as possible)
337
- __generic_option_reducer(:private_options, [], opts, only_with: :private) do |priv_opts, (options, option_args)|
338
- primary_name = options.shift
339
-
326
+ __generic_option_reducer(:private_options, [], opts, only_with: :private) do |priv_opts, (option, option_args)|
340
327
  if option_args[:private]
341
- priv_opts << primary_name
328
+ priv_opts << option
342
329
  else
343
330
  priv_opts
344
331
  end
345
332
  end
346
333
 
347
334
  # Set the default options to be set when no values are passed
348
- __generic_option_reducer(:default_options, {}, opts, only_with: :default) do |default_opts, (options, option_args)|
349
- default_opts.merge(options.shift => option_args[:default])
335
+ __generic_option_reducer(:default_options, {}, opts, only_with: :default) do |default_opts, (option, option_args)|
336
+ default_opts.merge(option => option_args[:default])
350
337
  end
351
338
 
352
- # Create the attribute accessors
353
- opts.keys.each do |options|
354
- options = optionify_all(options)
355
- primary_name = options.shift
356
- attr_accessor(primary_name)
357
- end
358
- end
339
+ # Create a cache of all the options available
340
+ __generic_option_reducer(:all_options, {}, opts)
359
341
 
360
- def _create_gola_(opts)
361
- gola = []
342
+ # Create the Getoptlong syntax
343
+ __generic_option_reducer(:gol_options, [], opts) do |gol_options, (option, option_args)|
344
+ argument_dependency = GOL_MAP[option_args[:argument]]
345
+ aliases = option_args[:aliases] || []
362
346
 
363
- # Loop through the options to create
364
- # the GetoptLong configuration array
365
- opts.each do |opt, details|
347
+ # Create the attribute accessor
348
+ attr_accessor(optionify(option))
366
349
 
367
- short = nil
368
- dependency = :none
350
+ # Create the primary GOL option
351
+ gol_option =[ "--#{option}" ]
352
+ gol_option << "-#{option_args[:short]}" if option_args[:short]
353
+ gol_option << argument_dependency
369
354
 
370
- # If we have extended details determine them
371
- if details.is_a?(Hash)
372
- short = details[:short]
373
- dependency = details[:argument]
374
- else
375
- dependency = details
376
- end
355
+ # Get the primary setter
356
+ primary_setter = optionify(option, :set)
357
+
358
+ # Handle the aliases
359
+ gol_option_set = aliases.reduce([gol_option]) do |gol_list, ali|
360
+ alias_setter = optionify(ali, :set)
377
361
 
378
- # Prepare the GetoptLong Array option
379
- golao = []
380
- golao << "-#{short}" if short
381
- golao << GOL_MAP[dependency.to_sym]
382
-
383
- # If the option has aliases then create
384
- # additional GetoptLong Array options
385
- if opt.is_a?(Array)
386
- opt.each_with_index do |key, i|
387
- golaot = golao.dup
388
- golaot.shift if i > 0 # Remove Short Code
389
- golaot.unshift("--#{key}")
390
- gola << golaot
362
+ # Create the attribute accessor alias
363
+ define_method(alias_setter) do |value|
364
+ __send__(primary_setter, value)
391
365
  end
392
- else
393
- golao.unshift("--#{opt}")
394
- gola << golao
366
+
367
+ # Create the GOL option aliases
368
+ gol_alias =[ "--#{ali}" ]
369
+ gol_alias << argument_dependency
370
+ gol_list << gol_alias
395
371
  end
396
- end
397
372
 
398
- gola
373
+ # Append to the option master list
374
+ gol_options.concat(gol_option_set)
375
+ end
399
376
  end
400
377
 
401
378
  private
402
379
 
403
380
  def __generic_option_reducer(instance_var, default = [], opts = {}, args = {}, &block)
404
- opts = opts.reduce({}) { |o, (k, v)| o.merge(optionify_all(k) => v) }
405
381
 
406
382
  # Require certain keys be in the option cache. This is done to save time of the processing and make it easier to write
407
383
  # the code to parse the options and set the requirements and dependencies of each option.
@@ -411,7 +387,10 @@ module CliTool
411
387
 
412
388
  # Run the reducer and set the class variables accordingly
413
389
  class_variable_set("@@__#{instance_var}", default) unless class_variable_defined?("@@__#{instance_var}")
414
- class_variable_set("@@__#{instance_var}", opts.reduce(class_variable_get("@@__#{instance_var}") || default, &block))
390
+ cached_value = class_variable_get("@@__#{instance_var}") || default
391
+
392
+ # Set the value with either the reduce value or the passed value if no block was given
393
+ class_variable_set("@@__#{instance_var}", block_given? ? opts.reduce(cached_value, &block) : cached_value.merge(opts))
415
394
  end
416
395
 
417
396
  def __get_options(instance_var = nil)
@@ -213,6 +213,10 @@ module CliTool
213
213
  end
214
214
 
215
215
  module ClassMethods
216
+ def script_plugin(object)
217
+ Script.__send__(:include, object)
218
+ end
219
+
216
220
  def script(options = {}, &block)
217
221
  script = Proc.new do
218
222
  run = true
@@ -258,6 +262,10 @@ module CliTool
258
262
  run(:run_suite!, *a, &b)
259
263
  end
260
264
 
265
+ def run!(command, *a, &b)
266
+ run(command, *a, &b)
267
+ end
268
+
261
269
  def custom!(*a, &b)
262
270
  Proc.new { |obj| obj.instance_exec(*a, &b) }
263
271
  false
@@ -279,7 +287,7 @@ module CliTool
279
287
 
280
288
  def remote_exec!(script)
281
289
  ssh_cmd =[ 'ssh -t -t' ]
282
- ssh_cmd << "-I #{@identity}" if @identity
290
+ ssh_cmd << "-i #{@identity}" if @identity
283
291
  ssh_cmd << "-p #{@port}" if @port
284
292
  ssh_cmd << "#{@username}@#{@host}"
285
293
  ssh_cmd << "/bin/bash -s"
@@ -142,7 +142,11 @@ module CliTool
142
142
  ANSI_COLORS[c] ? o << "\e[#{ANSI_COLORS[c]}m" : o
143
143
  end
144
144
 
145
- "#{prefix}#{text}\e[0m" # Add color
145
+ suffix = "\e[0m"
146
+
147
+ # Allow color nesting
148
+ text = text.gsub(suffix, "#{suffix}#{prefix}")
149
+ "#{prefix}#{text}#{suffix}" # Add color
146
150
  end
147
151
  end
148
152
  end
@@ -1,3 +1,3 @@
1
1
  module CliTool
2
- VERSION = "0.1.0"
2
+ VERSION = "0.1.2"
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.0
4
+ version: 0.1.2
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-25 00:00:00.000000000 Z
12
+ date: 2014-02-26 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler