shellopts 2.0.11 → 2.0.14

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: 485e81c58a890240d2a77e24a5f3a7a291551da968f395f3aaa4f5b2dac3cc9b
4
- data.tar.gz: 2d6c92ca01ce1207f6311cf9b0e9bab3e555e309142ba6916d589396f215afb8
3
+ metadata.gz: 2101f633ad0e166982b108842a83f91b3d216869f9fe3d6ac7ecea5256f2b437
4
+ data.tar.gz: da77f695902e5e921e598dd8d1327af4ce07ffe7b9cfadb88fe1b0889b95b4e0
5
5
  SHA512:
6
- metadata.gz: 31f3801b98585bed378ff77ae6632d18951ef37a49b3cfde843df9718dc5b46da40e777dff3f7b29e01bf8b32f2ebe0e1e7ab76ddb66744a461acf0061f75b1f
7
- data.tar.gz: d72bcf1d58e9afdf4f92477af1f2824e01031a46b4cacf5283878ce1045ad91dd930c1c1a15cd588a2552a4ad90fe5ac3add6019b50e099693aa184b7747153f
6
+ metadata.gz: 36dbd8d33d74a5b985463df5fb514501c2881881bdd68f1fa479c7ecac3de2bc4a2bc3a713acfe735d3e53ccdde10f5abf5ecc066d370fa8e795a77e290f9988
7
+ data.tar.gz: b364c8d00de2f7ece58d3c721ef3d4f40a892a232aa39a0c94df2b7ef37cf86e78c99df25c07e8802b0e48640ec928c4157bd89a2cb0851955484ed5957733ca
data/TODO CHANGED
@@ -1,4 +1,24 @@
1
1
 
2
+ o In the following list is a command with a mandatory sub-command
3
+
4
+ list.tables!
5
+ list.uuids!
6
+
7
+ It should be rendered as
8
+ list tables|uuids
9
+
10
+ and not as
11
+ list tables
12
+ list uuids
13
+
14
+ and not as
15
+ list [tables|uuids]
16
+
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
  #
@@ -181,33 +194,36 @@ 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?
186
- self.instance_eval %(
187
- def #{opt.attr}(default = nil)
188
- if @__option_values__.key?(:#{opt.attr})
189
- @__option_values__[:#{opt.attr}]
190
- else
191
- default
192
- end
197
+ if opt.argument?
198
+ self.instance_eval %(
199
+ def #{opt.attr}(default = nil)
200
+ if @__option_values__.key?(:#{opt.attr})
201
+ @__option_values__[:#{opt.attr}]
202
+ else
203
+ default
193
204
  end
194
- )
195
- elsif !opt.argument? # Repeatable w/o argument
196
- self.instance_eval %(
197
- def #{opt.attr}(default = [])
198
- if @__option_values__.key?(:#{opt.attr})
199
- @__option_values__[:#{opt.attr}]
200
- else
201
- default
202
- end
205
+ end
206
+ )
207
+
208
+ elsif opt.repeatable?
209
+ self.instance_eval %(
210
+ def #{opt.attr}(default = 0)
211
+ if default > 0 && @__option_values__[:#{opt.attr}] == 0
212
+ default
213
+ else
214
+ @__option_values__[:#{opt.attr}]
203
215
  end
204
- )
205
- else
206
- self.instance_eval("def #{opt.attr}() @__option_values__[:#{opt.attr}] end")
207
- end
216
+ end
217
+ )
218
+ else
219
+ self.instance_eval("def #{opt.attr}() @__option_values__[:#{opt.attr}] end")
220
+ end
221
+
222
+ if opt.argument? || opt.repeatable?
208
223
  self.instance_eval("def #{opt.attr}=(value) @__option_values__[:#{opt.attr}] = value end")
209
224
  @__option_values__[opt.attr] = 0 if !opt.argument?
210
225
  end
226
+
211
227
  self.instance_eval("def #{opt.attr}?() @__option_values__.key?(:#{opt.attr}) end")
212
228
  }
213
229
 
@@ -1,3 +1,3 @@
1
1
  module ShellOpts
2
- VERSION = "2.0.11"
2
+ VERSION = "2.0.14"
3
3
  end
data/lib/shellopts.rb CHANGED
@@ -192,23 +192,23 @@ module ShellOpts
192
192
  #
193
193
  # #error is supposed to be used when the user made an error and the usage
194
194
  # is written to help correcting the error
195
- #
196
195
  def error(subject = nil, message)
197
- saved = $stdout
198
- $stdout = $stderr
199
196
  $stderr.puts "#{name}: #{message}"
200
- Formatter.usage(grammar)
201
- exit 1
202
- ensure
203
- $stdout = saved
197
+ saved = $stdout
198
+ begin
199
+ $stdout = $stderr
200
+ Formatter.usage(grammar)
201
+ exit 1
202
+ ensure
203
+ $stdout = saved
204
+ end
204
205
  end
205
206
 
206
207
  # Write error message to standard error and terminate program with status 1
207
208
  #
208
- # #failure is supposed to be used the used specified the correct arguments
209
- # but something went wrong during processing. Since the used didn't cause
210
- # the problem, only the error message is written
211
- #
209
+ # #failure doesn't print the program usage because is supposed to be used
210
+ # when the user specified the correct arguments but something else went
211
+ # wrong during processing
212
212
  def failure(message)
213
213
  $stderr.puts "#{name}: #{message}"
214
214
  exit 1
@@ -222,7 +222,6 @@ module ShellOpts
222
222
 
223
223
  # Print help for the given subject or the full documentation if +subject+
224
224
  # is nil. Clears the screen beforehand if :clear is true
225
- #
226
225
  def help(subject = nil, clear: true)
227
226
  node = (subject ? @grammar[subject] : @grammar) or
228
227
  raise ArgumentError, "No such command: '#{subject&.sub(".", " ")}'"
@@ -333,7 +332,17 @@ module ShellOpts
333
332
  def self.instance=(instance) @instance = instance end
334
333
  def self.shellopts() instance end
335
334
 
336
- forward_self_to :instance, :error, :failure
335
+ def self.error(subject = nil, message)
336
+ instance.error(subject, message) if instance? # Never returns
337
+ $stderr.puts "#{File.basename($PROGRAM_NAME)}: #{message}"
338
+ exit 1
339
+ end
340
+
341
+ def self.failure(message)
342
+ instance.failure(message) if instance?
343
+ $stderr.puts "#{File.basename($PROGRAM_NAME)}: #{message}"
344
+ exit 1
345
+ end
337
346
 
338
347
  # The Include module brings the reporting methods into the namespace when
339
348
  # included
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.11
4
+ version: 2.0.14
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-14 00:00:00.000000000 Z
11
+ date: 2022-04-05 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: forward_to