rake-commander 0.2.2 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: fd67a354c71f5013039c3cef58f8070aed34cad128c3b753eee699f7ed7c70e2
4
- data.tar.gz: f5b46e640c4395493906f80b1263b3b96636d1b60bc3880ba03d75b6abb3dd84
3
+ metadata.gz: e676c7895883ca91c58c1ef5a025a4a8f52795ed12ac79e2e53f304b59e5a9ee
4
+ data.tar.gz: 871c881ac31b06e1cf4a13bab7846d063bfee83bfcca4b332eeeb345f04c3051
5
5
  SHA512:
6
- metadata.gz: d16c7a9e9dcb95466463fcc3eddab2181314d4884613d6b23003e179b881be9e6eb15d72be783e76add51b3f26cf0e9082a6e4a8115c04f9d8054086d704869b
7
- data.tar.gz: 8c89aef16e4087fc5894f2033a21b09638599420a96ad6d474118e2a26ba76983e024e08ac4cec28fd811bbf7c4797bff08db6f76887b8da9fd237e5b6a36650
6
+ metadata.gz: 78d4f8199e7e220d26e48346fd0cd23aec273c739ee8bcd949ed12cf147191ec988f056d6ef273f5dc186e64bb71a2bc7eb33dbcbc2525f2d20278dac3ce1df2
7
+ data.tar.gz: 31a2dca9846b7521b43ca14d79a562f57a30fafad01af42e3a0886e4aceb3b4d43d71f8b6133f08ca538cbf9e6d5210ed82447dbfb20f70ea8ab315797dedae7
data/CHANGELOG.md CHANGED
@@ -2,6 +2,8 @@
2
2
  All notable changes to this project will be documented in this file.
3
3
 
4
4
  ## TO DO
5
+ - `option_reopen` -> upsert should be optional (add `upsert: false` as default an raise missing option if not found)
6
+ - The error on missing short pops up where it's not clear the short was missed in the option_reopen call.
5
7
  - Option results
6
8
  - Include the symbol name keys (configurable). Note that dash will be replaced by underscore.
7
9
  - Type Coercions
@@ -30,12 +32,24 @@ All notable changes to this project will be documented in this file.
30
32
  - Option to globally enable/disable the 2nd patch?
31
33
  * That would make this gem completely useless.
32
34
 
33
- ## [0.2.2] - 2023-04-xx
35
+ ## [0.2.5] - 2023-04-xx
34
36
 
35
37
  ### Added
36
38
  ### Fixed
37
39
  ### Changed
38
40
 
41
+ ## [0.2.4] - 2023-04-30
42
+
43
+ ### Added
44
+ - `RakeCommander::Options`
45
+ - `::option_get` retrieves an option by name or short, may it exist.
46
+ - `::option?` to check if an opion exists.
47
+
48
+ ## [0.2.3] - 2023-04-29
49
+
50
+ ### Added
51
+ - Include Symbol option names in the parsed option results.
52
+
39
53
  ## [0.2.2] - 2023-04-29
40
54
 
41
55
  ### Fixed
@@ -24,6 +24,7 @@ class RakeCommander::Custom::Chainer < RakeCommander
24
24
  option '-m', 'method [METHOD]', default: 'system', desc: str_desc
25
25
 
26
26
  def task(*_args)
27
+ print_options if options[:b]
27
28
  if options[:c]
28
29
  cmd = "#{subcommand_base} -- #{subcommand_arguments.join(' ')}"
29
30
  puts "Calling --> '#{cmd}'"
@@ -51,6 +52,11 @@ class RakeCommander::Custom::Chainer < RakeCommander
51
52
 
52
53
  private
53
54
 
55
+ def print_options
56
+ puts "These are the options received:"
57
+ pp options
58
+ end
59
+
54
60
  def puts(str)
55
61
  return super unless options[:b]
56
62
  super "#{app_id} #{str} #{thread_id}"
@@ -8,6 +8,7 @@ class RakeCommander::Custom::Chained < RakeCommander::Custom::Chainer
8
8
 
9
9
  def task(*_args)
10
10
  puts "Called !!"
11
+ print_options if options[:b]
11
12
  puts options[:s] if options[:s]
12
13
  end
13
14
  end
@@ -148,7 +148,7 @@ class RakeCommander
148
148
 
149
149
  # Called on parse runtime
150
150
  def option_block(&middleware)
151
- block_extra_args = [default, short, name]
151
+ block_extra_args = [default, short, name, self]
152
152
  proc do |value|
153
153
  args = block_extra_args.dup.unshift(value)
154
154
  original_block&.call(*args)
@@ -24,6 +24,12 @@ class RakeCommander
24
24
  # It **re-opens** the method and adds a middleware to gather and return
25
25
  # the results of the parsing (including the `leftovers`)
26
26
  # @note this extends the method parameters and changes the returned value.
27
+ # @yield [value, default, short, name, option] do somethin with parsed `value` for `option`
28
+ # @yieldparam value [] the resulting parsed value
29
+ # @yieldparam default [] the default value of the option
30
+ # @yieldparam short [Symbol] the symbol short of the option
31
+ # @yieldparam name [Symbol] the symbol name of the option
32
+ # @yieldparam option [RakeCommander::Option] the option that is being parsed
27
33
  # @param leftovers [Array<String>] see RakeCommander::Options#parse_options`
28
34
  # @param results [Hash] with `short` option as `key` and final value as `value`.
29
35
  # @see `RakeCommander::Options#parse_options`
@@ -66,9 +72,9 @@ class RakeCommander
66
72
  # @return [Proc] the results collector that wraps the middleware.
67
73
  def results_collector(results, &middleware)
68
74
  results = result_defaults(results)
69
- proc do |value, default, short, name|
70
- middleware&.call(value, default, short, name)
71
- results[short] = value.nil?? default : value
75
+ proc do |value, default, short, name, opt|
76
+ middleware&.call(value, default, short, name, opt)
77
+ results[name] = results[short] = value.nil?? default : value
72
78
  end
73
79
  end
74
80
 
@@ -79,7 +85,7 @@ class RakeCommander
79
85
  (options_with_defaults && opt.default?) \
80
86
  || (opt.required? && opt.default?)
81
87
  end.each do |opt|
82
- res_def[opt.short] = opt.default
88
+ res_def[opt.name] = res_def[opt.short] = opt.default
83
89
  end
84
90
  end
85
91
  end
@@ -94,6 +94,18 @@ class RakeCommander
94
94
  options_hash.values.uniq
95
95
  end
96
96
 
97
+ # @param sym [Symbol] the `name` or `short` of the option.
98
+ # @return [RakeCommander::Option, NilClass] retrieves the option.
99
+ def option_get(sym)
100
+ options_hash[sym.to_sym]
101
+ end
102
+
103
+ # @param sym [Symbol] the `name` or `short` of the option.
104
+ # @return [Boolean] whether an option has been declared.
105
+ def option?(sym)
106
+ options_hash.key?(sym.to_sym)
107
+ end
108
+
97
109
  # @return [Boolean] are there options defined?
98
110
  def options?
99
111
  !options.empty?
@@ -149,7 +161,7 @@ class RakeCommander
149
161
  def implicit_shorts
150
162
  options.each_with_object({}) do |opt, implicit|
151
163
  short = opt.short_implicit
152
- implicit[short] = opt unless options_hash.key?(short)
164
+ implicit[short] = opt unless option?(short)
153
165
  end
154
166
  end
155
167
 
@@ -159,7 +171,7 @@ class RakeCommander
159
171
  # @note `OptionParser` already has `-h --help` as a native option.
160
172
  # @param opts [OptionParser] where the help will be added.
161
173
  def option_help(opts)
162
- return false if options_hash.key?(:help) || options_hash.key?(:h)
174
+ return false if option?(:help) || option?(:h)
163
175
  option(:h, :help, 'Prints this help') do
164
176
  puts opts
165
177
  exit(0)
@@ -191,12 +203,12 @@ class RakeCommander
191
203
  # @return [RakeCommander::Option, NilClass] the option that was added, `nil` is returned otherwise.
192
204
  def add_to_options(opt, override: true)
193
205
  name_ref = respond_to?(:name)? " (#{name})" : ''
194
- if sprev = options_hash[opt.short]
206
+ if sprev = option_get(opt.short)
195
207
  return nil unless override
196
208
  puts "Warning#{name_ref}: Overriding option '#{sprev.name}' with short '#{sprev.short}' ('#{opt.name}')"
197
209
  delete_from_options(sprev)
198
210
  end
199
- if nprev = options_hash[opt.name]
211
+ if nprev = option_get(opt.name)
200
212
  return nil unless override
201
213
  puts "Warning#{name_ref}: Overriding option '#{nprev.short}' with name '#{nprev.name}' ('#{opt.short}')"
202
214
  delete_from_options(nprev)
@@ -1,3 +1,3 @@
1
1
  class RakeCommander
2
- VERSION = '0.2.2'.freeze
2
+ VERSION = '0.2.4'.freeze
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rake-commander
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.2
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - Oscar Segura Samper
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2023-04-28 00:00:00.000000000 Z
11
+ date: 2023-04-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler