clasp-ruby 0.21.0 → 0.22.0.1

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
- SHA256:
3
- metadata.gz: 5d4b4a9b8453b84575f693c6c1cf3d58c0af5999843c360295f799d60dc6b57d
4
- data.tar.gz: 2089b0ead4ef6161d6cdda4d2129f2361567d434ea5aa3bcb257918093bdad7a
2
+ SHA1:
3
+ metadata.gz: bdf2add70e2d8e134e6759718fabde89ecb5c158
4
+ data.tar.gz: dcf9e41a17a28f53406daef64bdba1f614f2a080
5
5
  SHA512:
6
- metadata.gz: c24103543ec9d290ae1731ab49a8937e02148b48e567c199ed1c3bb7a12edda0c892b4cd36eeb0fa6a8232fd9936db2fbae254e8db1a835bdf054a3df0f82af0
7
- data.tar.gz: c1ea7534927c52c7c138da6da8f43c2c28f13222f35ff751c5d6b4a6aa456d18b6e655de5eaec3373d21aee864913d4ae6dc70d32e7d6e0b33815dc3fe40f57e
6
+ metadata.gz: d329ce03d0057446f877425faec12da111253f0bb51dbcd0d1e619d0f3e7f5dcc781b20b18ffbb65eb796011b3021d208e1d391881754ec3ecd61e6efad49186
7
+ data.tar.gz: 63873b0a897f4d854d6f4c80e5605cd68ebecdb4591361662cfa075292ac9c4a69ae8aa8f09ab97446db675fe5611cca41bc7ea83f01853bafb55722b5093fee
@@ -5,7 +5,7 @@
5
5
  # Purpose: Argument specification classes
6
6
  #
7
7
  # Created: 25th October 2014
8
- # Updated: 20th April 2019
8
+ # Updated: 29th April 2019
9
9
  #
10
10
  # Home: http://github.com/synesissoftware/CLASP.Ruby
11
11
  #
@@ -57,8 +57,40 @@ module CLASP
57
57
  # ######################################################################## #
58
58
  # classes
59
59
 
60
+ # @!visibility private
61
+ class SpecificationBase # :nodoc: all
62
+
63
+ private
64
+ # @!visibility private
65
+ def check_arity_(blk, range, label) # :nodoc:
66
+
67
+ raise ArgumentError, "block must be a #{Proc}; #{blk.class} given" unless blk.nil? || Proc === blk
68
+
69
+ if blk
70
+
71
+ case blk.arity
72
+ when range
73
+
74
+ ;
75
+ else
76
+
77
+ msg = "wrong arity for #{label}"
78
+
79
+ if $DEBUG
80
+
81
+ raise ArgumentError, msg
82
+ else
83
+
84
+ warn msg
85
+ end
86
+ end
87
+ end
88
+ end
89
+ public
90
+ end
91
+
60
92
  # A class that represents the specification for a command-line flag
61
- class FlagSpecification
93
+ class FlagSpecification < SpecificationBase
62
94
 
63
95
  # Creates a FlagSpecification instance from the given name, aliases, and help
64
96
  #
@@ -70,13 +102,18 @@ class FlagSpecification
70
102
  # - +help+ (+String+) The help string, which may be +nil+
71
103
  # - +extras+ An application-defined additional parameter. If +nil+, it is assigned an empty +Hash+
72
104
  #
105
+ # * *Block* An optional block that is called when a matching flag argument is found
106
+ #
73
107
  # *NOTE:* Users should prefer the +CLASP::Flag()+ method
74
- def initialize(name, aliases, help, extras = nil)
108
+ def initialize(name, aliases, help, extras = nil, &blk)
109
+
110
+ check_arity_(blk, 0..3, "flag")
75
111
 
76
112
  @name = name
77
113
  @aliases = (aliases || []).select { |a| a and not a.empty? }
78
114
  @help = help
79
115
  @extras = extras || {}
116
+ @action = blk
80
117
  end
81
118
 
82
119
  # The flag's name string
@@ -88,6 +125,17 @@ class FlagSpecification
88
125
  # The flag's extras
89
126
  attr_reader :extras
90
127
 
128
+ # (Proc) The procedure
129
+ attr_reader :action
130
+
131
+ # @!visibility private
132
+ def action=(blk) # :nodoc: all
133
+
134
+ check_arity_(blk, 0..3, "flag")
135
+
136
+ @action = blk
137
+ end
138
+
91
139
  # String form of the flag
92
140
  def to_s
93
141
 
@@ -135,28 +183,38 @@ class FlagSpecification
135
183
  @@Version_ = self.new('--version', [], 'shows version and terminates')
136
184
  public
137
185
  # An instance of FlagSpecification that provides default '--help' information
138
- def self.Help(extras = nil)
186
+ #
187
+ # If you wish to specify +extras+ or attach a block, you may do so
188
+ def self.Help(extras = nil, &blk)
139
189
 
140
190
  h = @@Help_
141
191
 
142
- return self.new(h.name, h.aliases, h.help, extras) if extras
192
+ if extras || blk
193
+
194
+ return self.new(h.name, h.aliases, h.help, extras, &blk)
195
+ end
143
196
 
144
197
  h
145
198
  end
146
199
 
147
200
  # An instance of FlagSpecification that provides default '--version' information
148
- def self.Version(extras = nil)
201
+ #
202
+ # If you wish to specify +extras+ or attach a block, you may do so
203
+ def self.Version(extras = nil, &blk)
149
204
 
150
205
  h = @@Version_
151
206
 
152
- return self.new(h.name, h.aliases, h.help, extras) if extras
207
+ if extras || blk
208
+
209
+ return self.new(h.name, h.aliases, h.help, extras, &blk)
210
+ end
153
211
 
154
212
  h
155
213
  end
156
214
  end
157
215
 
158
216
  # A class that represents the specification for a command-line option
159
- class OptionSpecification
217
+ class OptionSpecification < SpecificationBase
160
218
 
161
219
  # Creates an OptionSpecification instance from the given name, aliases, help,
162
220
  # values_range, and default_value
@@ -174,8 +232,12 @@ class OptionSpecification
174
232
  # - +constraint+ (Hash) Constraint to be applied to the parsed values of options matching this specification. NOTE: only integer constraints are supported in the current version
175
233
  # - +extras+ An application-defined additional parameter. If +nil+, it is assigned an empty +Hash+
176
234
  #
235
+ # * *Block* An optional block that is called when a matching option argument is found
236
+ #
177
237
  # *NOTE:* Users should prefer the +CLASP::Option()+ method
178
- def initialize(name, aliases, help, values_range, default_value, required, required_message, constraint, extras = nil)
238
+ def initialize(name, aliases, help, values_range, default_value, required, required_message, constraint, extras = nil, &blk)
239
+
240
+ check_arity_(blk, 0..3, "option")
179
241
 
180
242
  @name = name
181
243
  @aliases = (aliases || []).select { |a| a and not a.empty? }
@@ -186,6 +248,7 @@ class OptionSpecification
186
248
  @required_message = nil
187
249
  @constraint = constraint || {}
188
250
  @extras = extras || {}
251
+ @action = blk
189
252
 
190
253
  rm_name = nil
191
254
 
@@ -227,6 +290,17 @@ class OptionSpecification
227
290
  # The option's extras
228
291
  attr_reader :extras
229
292
 
293
+ # (Proc) The procedure
294
+ attr_reader :action
295
+
296
+ # @!visibility private
297
+ def action=(blk) # :nodoc: all
298
+
299
+ check_arity_(blk, 0..3, "flag")
300
+
301
+ @action = blk
302
+ end
303
+
230
304
  # String form of the option
231
305
  def to_s
232
306
 
@@ -318,7 +392,9 @@ end
318
392
  # - +:aliases+ (::Array) An array of aliases, e.g. [ '-v', '-verb' ]. Ignored if +:alias+ is specified
319
393
  # - +:extras+ An application-defined object, usually a hash of custom attributes
320
394
  # - +:help+ (::String) A help string
321
- def CLASP.Flag(name, options = {})
395
+ #
396
+ # * *Block* An optional block that is called when a matching flag argument is found
397
+ def CLASP.Flag(name, options = {}, &blk)
322
398
 
323
399
  aliases = nil
324
400
  help = nil
@@ -351,7 +427,7 @@ def CLASP.Flag(name, options = {})
351
427
  end
352
428
  end
353
429
 
354
- CLASP::FlagSpecification.new(name, aliases, help, extras)
430
+ CLASP::FlagSpecification.new(name, aliases, help, extras, &blk)
355
431
  end
356
432
 
357
433
  # Generator method that obtains a CLASP::OptionSpecification according to the given
@@ -376,7 +452,9 @@ end
376
452
  # - +constraint+ (Hash) Constraint to be applied to the parsed values of options matching this specification. NOTE: only integer constraints are supported in the current version
377
453
  # - +:values_range+ (::Array) An array defining the accepted values for the option
378
454
  # - +:values+ [DEPRECATED] Alternative to +:values_range+
379
- def CLASP.Option(name, options = {})
455
+ #
456
+ # * *Block* An optional block that is called when a matching option argument is found
457
+ def CLASP.Option(name, options = {}, &blk)
380
458
 
381
459
  aliases = nil
382
460
  help = nil
@@ -429,7 +507,7 @@ def CLASP.Option(name, options = {})
429
507
  end
430
508
  end
431
509
 
432
- CLASP::OptionSpecification.new(name, aliases, help, values_range, default_value, required, require_message, constraint, extras)
510
+ CLASP::OptionSpecification.new(name, aliases, help, values_range, default_value, required, require_message, constraint, extras, &blk)
433
511
  end
434
512
 
435
513
  def CLASP.Alias(name, *args)
@@ -5,12 +5,13 @@
5
5
  # Purpose: Version for CLASP.Ruby library
6
6
  #
7
7
  # Created: 16th November 2014
8
- # Updated: 20th April 2019
8
+ # Updated: 22nd August 2020
9
9
  #
10
10
  # Home: http://github.com/synesissoftware/CLASP.Ruby
11
11
  #
12
12
  # Author: Matthew Wilson
13
13
  #
14
+ # Copyright (c) 2019-2020, Matthew Wilson and Synesis Information Systems
14
15
  # Copyright (c) 2014-2019, Matthew Wilson and Synesis Software
15
16
  # All rights reserved.
16
17
  #
@@ -51,7 +52,7 @@
51
52
  module CLASP
52
53
 
53
54
  # Current version of the CLASP.Ruby library
54
- VERSION = '0.21.0'
55
+ VERSION = '0.22.0.1'
55
56
 
56
57
  private
57
58
  # @!visibility private
@@ -0,0 +1,50 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $:.unshift File.join(File.dirname(__FILE__), '../..', 'lib')
4
+
5
+ require 'clasp'
6
+
7
+ require 'xqsr3/extensions/test/unit'
8
+
9
+ require 'test/unit'
10
+
11
+ class Test_WithAction < Test::Unit::TestCase
12
+
13
+ def test_flag_with_action
14
+
15
+ debug = false
16
+
17
+ specifications = [
18
+
19
+ CLASP.Flag('--debug', alias: '-d') { debug = true }
20
+ ]
21
+ argv = []
22
+ args = CLASP.parse argv, specifications
23
+
24
+ assert_equal 0, args.flags.size
25
+ assert_equal 0, args.options.size
26
+ assert_equal 0, args.values.size
27
+
28
+ assert_false debug
29
+
30
+ argv2 = [ '--debug' ]
31
+ args2 = CLASP.parse argv2, specifications
32
+
33
+ assert_equal 1, args2.flags.size
34
+ assert_equal 0, args2.options.size
35
+ assert_equal 0, args2.values.size
36
+
37
+ assert_false debug
38
+
39
+ if ix = args2.flags.index('--debug')
40
+
41
+ flag = args2.flags[ix]
42
+
43
+ flag.argument_specification.action.call(flag, flag.argument_specification)
44
+ end
45
+
46
+ assert_true debug
47
+ end
48
+ end
49
+
50
+
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clasp-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.21.0
4
+ version: 0.22.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Matt Wilson
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2019-04-28 00:00:00.000000000 Z
11
+ date: 2020-08-22 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: xqsr3
@@ -71,6 +71,7 @@ files:
71
71
  - test/unit/tc_specifications.rb
72
72
  - test/unit/tc_typed_options.rb
73
73
  - test/unit/tc_usage.rb
74
+ - test/unit/tc_with_action.rb
74
75
  - test/unit/ts_all.rb
75
76
  homepage: http://github.com/synesissoftware/CLASP.Ruby
76
77
  licenses:
@@ -92,7 +93,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
92
93
  version: '0'
93
94
  requirements: []
94
95
  rubyforge_project:
95
- rubygems_version: 2.7.6
96
+ rubygems_version: 2.4.5.5
96
97
  signing_key:
97
98
  specification_version: 4
98
99
  summary: CLASP.Ruby