shellopts 2.0.12 → 2.0.15

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: b2f378c7e7c04297221bfae81065ef06699e93b2dc4fda892b210ef4702c5836
4
- data.tar.gz: f5d83b09b371aebd7cc07e73905edffb2f9f834ba5e382894277310e984e7ecc
3
+ metadata.gz: 253954c7f2de836c89e511dffabf3217719e778d47f74740a51c3b1099dcd07f
4
+ data.tar.gz: 4a5579e3c56674d09ba380d392e600028e6c353728994e8b2445629b772cb450
5
5
  SHA512:
6
- metadata.gz: 6255a64a99ceefe0dc0a7a3195976d42b2db6b3a28d779cbeefbc9f36380e46a762939e2062ff9f32f4f78bd5bff8122606b5102d9d8473e86c0a742cd7db479
7
- data.tar.gz: b54f74a5d8fd251d1492667bd8e1340c1f17a3e96936f423efd61e82fe665294936c1edd1ac9ac4ab9df118450ccefafcda117f84ef4e0726d78159196e075cf
6
+ metadata.gz: d0f0f0418361c69037a944c1e99021c32d578ed41dc239e7f9dc9f1ce94e578a137efdea3a9b96848fc0fb0b9aa1a8114153485b6ce21ceea7a3f34ceacdd197
7
+ data.tar.gz: f70644b23103d244fe06164a337839c042d6e65e1aa3dba59fa6b028cf7b0032fd694121c582142f2f3cdfbdaa12cba17464b74114bc5caf5b2aa489cde94f9a
data/TODO CHANGED
@@ -1,4 +1,24 @@
1
+ o --option can't be escaped if on the start of a block line?
2
+ o Use require_relative
3
+ o In the following list is a command with a mandatory sub-command
1
4
 
5
+ list.tables!
6
+ list.uuids!
7
+
8
+ It should be rendered as
9
+ list tables|uuids
10
+
11
+ and not as
12
+ list tables
13
+ list uuids
14
+
15
+ and not as
16
+ list [tables|uuids]
17
+
18
+ o Replace -h with -? when -h is specified by the user (eg. as a shorthand for --host)
19
+ o Limit text width to -10 chars of what it is today (same as stackexchange width in characters)
20
+ o Fix formatting error in long arguments (see ms2pg)
21
+ o Macro that can be used in the SPEC for the program name (eg. <PROGRAM>)
2
22
  o Ignore all text after ' # ' (doesn't conflict with option flag)
3
23
  o Command aliases
4
24
  o Add user-defined setions
@@ -45,12 +45,12 @@ module ShellOpts
45
45
 
46
46
  # These names can't be used as option or command names
47
47
  RESERVED_OPTION_NAMES = %w(
48
- is_a instance_eval instance_exec method_missing singleton_method_added
48
+ is_a to_h instance_eval instance_exec method_missing singleton_method_added
49
49
  singleton_method_removed singleton_method_undefined
50
50
  )
51
51
 
52
- # These methods can be overridden by an option or a command (the value is not used -
53
- # this is just for informational purposes)
52
+ # These methods can be overridden by an option or a command (this constant
53
+ # is not used - it is just for informational purposes)
54
54
  OVERRIDEABLE_METHOD_NAMES = %w(
55
55
  subcommand subcommand! supercommand!
56
56
  )
@@ -96,6 +96,19 @@ module ShellOpts
96
96
  end
97
97
  end
98
98
 
99
+ # Returns a hash of the given options if defined. Returns all options if no
100
+ # options are given
101
+ def to_h(*keys)
102
+ keys = ::Kernel::Array(keys).flatten
103
+ if keys.empty?
104
+ @__option_values__
105
+ else
106
+ keys.map { |key|
107
+ @__option_values__.key?(key) ? [key, @__option_values__[key]] : nil
108
+ }.compact.to_h
109
+ end
110
+ end
111
+
99
112
  # Subcommand identifier or nil if not present. #subcommand is often used in
100
113
  # case statement to branch out to code that handles the given subcommand:
101
114
  #
@@ -140,10 +153,10 @@ module ShellOpts
140
153
  # Grammar object
141
154
  attr_reader :__grammar__
142
155
 
143
- # Hash from identifier to value. Can be Integer, Float, or String
144
- # depending on the option's type. Repeated options options without
145
- # arguments have the number of occurences as the value, with arguments
146
- # the value is an array of the given values
156
+ # Hash from identifier to value. Can be Integer, Float, or String depending
157
+ # on the option's type. Repeated options options without arguments have the
158
+ # number of occurences as value and with arguments the value is an array of
159
+ # the given values
147
160
  attr_reader :__option_values__
148
161
 
149
162
  # List of Option objects for the subcommand in the same order as
@@ -181,10 +194,23 @@ module ShellOpts
181
194
 
182
195
  def __define_option_methods__
183
196
  @__grammar__.options.each { |opt|
184
- if opt.argument? || opt.repeatable?
185
- if opt.optional?
197
+ if !opt.repeatable?
198
+ self.instance_eval %(
199
+ def #{opt.attr}?()
200
+ @__option_values__.key?(:#{opt.attr})
201
+ end
202
+ )
203
+ end
204
+
205
+ if opt.repeatable?
206
+ if opt.argument?
186
207
  self.instance_eval %(
187
- def #{opt.attr}(default = nil)
208
+ def #{opt.attr}?()
209
+ (@__option_values__[:#{opt.attr}]&.size || 0) > 0
210
+ end
211
+ )
212
+ self.instance_eval %(
213
+ def #{opt.attr}(default = [])
188
214
  if @__option_values__.key?(:#{opt.attr})
189
215
  @__option_values__[:#{opt.attr}]
190
216
  else
@@ -192,23 +218,41 @@ module ShellOpts
192
218
  end
193
219
  end
194
220
  )
195
- elsif !opt.argument? # Repeatable w/o argument
221
+ else
196
222
  self.instance_eval %(
197
- def #{opt.attr}(default = [])
198
- if @__option_values__.key?(:#{opt.attr})
199
- @__option_values__[:#{opt.attr}]
200
- else
223
+ def #{opt.attr}?()
224
+ (@__option_values__[:#{opt.attr}] || 0) > 0
225
+ end
226
+ )
227
+ self.instance_eval %(
228
+ def #{opt.attr}(default = 0)
229
+ if default > 0 && (@__option_values__[:#{opt.attr}] || 0) == 0
201
230
  default
231
+ else
232
+ @__option_values__[:#{opt.attr}] || 0
202
233
  end
203
234
  end
204
235
  )
205
- else
206
- self.instance_eval("def #{opt.attr}() @__option_values__[:#{opt.attr}] end")
207
236
  end
208
- self.instance_eval("def #{opt.attr}=(value) @__option_values__[:#{opt.attr}] = value end")
209
- @__option_values__[opt.attr] = 0 if !opt.argument?
237
+
238
+ elsif opt.argument?
239
+ self.instance_eval %(
240
+ def #{opt.attr}(default = nil)
241
+ if @__option_values__.key?(:#{opt.attr})
242
+ @__option_values__[:#{opt.attr}]
243
+ else
244
+ default
245
+ end
246
+ end
247
+ )
248
+
249
+ else
250
+ self.instance_eval %(
251
+ def #{opt.attr}()
252
+ @__option_values__.key?(:#{opt.attr})
253
+ end
254
+ )
210
255
  end
211
- self.instance_eval("def #{opt.attr}?() @__option_values__.key?(:#{opt.attr}) end")
212
256
  }
213
257
 
214
258
  @__grammar__.commands.each { |cmd|
@@ -1,3 +1,3 @@
1
1
  module ShellOpts
2
- VERSION = "2.0.12"
2
+ VERSION = "2.0.15"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: shellopts
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.0.12
4
+ version: 2.0.15
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claus Rasmussen
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-03-15 00:00:00.000000000 Z
11
+ date: 2022-04-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: forward_to