yawpa 1.3.0 → 1.4.0

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
- SHA1:
3
- metadata.gz: a5f3b1572ad428775ff39af66082b520cf60021e
4
- data.tar.gz: 836e39335702df951e737fc1f67d36ce5cf51161
2
+ SHA256:
3
+ metadata.gz: e5fcd7ef44592d02c72ee21a350c6656b5cffccb8e4e2ce8b946409e3cfbf115
4
+ data.tar.gz: fc7be5044326da3ef8ec9259053b8b59c293c310ffe42344192abf887417d3bd
5
5
  SHA512:
6
- metadata.gz: 1189c18023a920a81781c1334123a8877e7a63ba82e54ec92e0afce4febfb435c3c5be2be2aaf26a62410a51e1c14f4a74e5dcdc4d7ae630de7a8b4f8aba63db
7
- data.tar.gz: 200fd56c6bacb4f1c9a6fd3d1dc0cb0925a3b1f060663d0d23946a59cea7070c3fe75b8152391cf1beda7f1af7d5b66e2041fac3893d796148b1612b264785c7
6
+ metadata.gz: ad4c50a7090a8a51a3435dd1fdbc774f33fb7eb1d4e84f513eee3aac5a9609460738b72701194f425cd0cef0f982f1817fe010510fd66c89dbae973bd0bc67c6
7
+ data.tar.gz: 7b5219aba7f6f2fc1a02724d61a8c997edeca3f91e5260996a17a346c9701a5c8271e2c774db2519cf30adaf1095ebdf86fd2787d876bb7b1b101a447380df57
data/Gemfile.lock CHANGED
@@ -1,35 +1,41 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- yawpa (1.3.0)
4
+ yawpa (1.4.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
- diff-lcs (1.3)
10
- docile (1.1.5)
11
- json (2.1.0)
12
- rake (12.0.0)
13
- rdoc (5.1.0)
14
- rspec (3.6.0)
15
- rspec-core (~> 3.6.0)
16
- rspec-expectations (~> 3.6.0)
17
- rspec-mocks (~> 3.6.0)
18
- rspec-core (3.6.0)
19
- rspec-support (~> 3.6.0)
20
- rspec-expectations (3.6.0)
9
+ diff-lcs (1.5.0)
10
+ docile (1.4.0)
11
+ psych (5.1.0)
12
+ stringio
13
+ rake (13.0.6)
14
+ rdoc (6.5.0)
15
+ psych (>= 4.0.0)
16
+ rspec (3.12.0)
17
+ rspec-core (~> 3.12.0)
18
+ rspec-expectations (~> 3.12.0)
19
+ rspec-mocks (~> 3.12.0)
20
+ rspec-core (3.12.1)
21
+ rspec-support (~> 3.12.0)
22
+ rspec-expectations (3.12.2)
21
23
  diff-lcs (>= 1.2.0, < 2.0)
22
- rspec-support (~> 3.6.0)
23
- rspec-mocks (3.6.0)
24
+ rspec-support (~> 3.12.0)
25
+ rspec-mocks (3.12.4)
24
26
  diff-lcs (>= 1.2.0, < 2.0)
25
- rspec-support (~> 3.6.0)
26
- rspec-support (3.6.0)
27
- simplecov (0.15.0)
28
- docile (~> 1.1.0)
29
- json (>= 1.8, < 3)
30
- simplecov-html (~> 0.10.0)
31
- simplecov-html (0.10.2)
32
- yard (0.9.9)
27
+ rspec-support (~> 3.12.0)
28
+ rspec-support (3.12.0)
29
+ simplecov (0.22.0)
30
+ docile (~> 1.1)
31
+ simplecov-html (~> 0.11)
32
+ simplecov_json_formatter (~> 0.1)
33
+ simplecov-html (0.12.3)
34
+ simplecov_json_formatter (0.1.4)
35
+ stringio (3.0.5)
36
+ webrick (1.7.0)
37
+ yard (0.9.28)
38
+ webrick (~> 1.7.0)
33
39
 
34
40
  PLATFORMS
35
41
  ruby
@@ -43,4 +49,4 @@ DEPENDENCIES
43
49
  yawpa!
44
50
 
45
51
  BUNDLED WITH
46
- 1.10.6
52
+ 2.4.10
data/LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2013 Josh Holtrop
1
+ Copyright (c) 2013-2023 Josh Holtrop
2
2
 
3
3
  MIT License
4
4
 
@@ -19,4 +19,4 @@ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
19
  NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
20
  LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
21
  OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
- WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md CHANGED
@@ -10,6 +10,21 @@ Yet Another Way to Parse Arguments is an argument-parsing library for Ruby.
10
10
  - Options can require an arbitrary number of parameters
11
11
  - Options can be defined with a range specifying the allowed number of parameters
12
12
 
13
+ ## Usage
14
+
15
+ Yawpa can be used from another Ruby gem as a regular gem dependency, by adding
16
+ a dependency on the "yawpa" gem at a particular version.
17
+ For example:
18
+
19
+ ```ruby
20
+ gem.add_dependency "yawpa", "~> 1.3"
21
+ ```
22
+
23
+ Yawpa can also be used by simply copying the contents of the one source file
24
+ into any project that desires to use it.
25
+ This can be useful if you desire to avoid having any external dependencies, or
26
+ if you are producing a redistributable standalone script.
27
+
13
28
  ## Example 1
14
29
 
15
30
  ```ruby
@@ -69,6 +84,10 @@ Parse input parameters looking for options according to rules given in flags
69
84
  arguments to the option
70
85
  - `:boolean`: if true, specify that the option is a toggleable boolean
71
86
  option and allow a prefix of "no" to turn it off.
87
+ - `:multi`: if true, instead of the last value specified for this
88
+ option being returned, an Array of all values specified for this
89
+ option will be returned, allowing the argument to appear multiple
90
+ times in the argument list
72
91
  - `flags` is optional. It supports the following keys:
73
92
  - `:posix_order`: Stop processing parameters when a non-option is seen.
74
93
  Set this to `true` if you want to implement subcommands.
@@ -86,9 +105,12 @@ or insufficient arguments are present for an option.
86
105
  username: {nargs: 1},
87
106
  password: {nargs: 1},
88
107
  color: :boolean,
108
+ scan: {nargs: 1, multi: true},
89
109
  }
90
110
  ```
91
111
 
112
+ ### `options` Values
113
+
92
114
  The keys of the `options` hash can be either strings or symbols.
93
115
 
94
116
  Possible option flags:
@@ -98,6 +120,10 @@ Possible option flags:
98
120
  arguments to the option
99
121
  - `:boolean`: if true, specify that the option is a toggleable boolean
100
122
  option and allow a prefix of "no" to turn it off.
123
+ - `:multi`: if true, instead of the last value specified for this
124
+ option being returned, an Array of all values specified for this
125
+ option will be returned, allowing the argument to appear multiple
126
+ times in the argument list
101
127
 
102
128
  ### Return values
103
129
 
@@ -109,6 +135,14 @@ options that were not processed after observing a non-option parameters).
109
135
 
110
136
  ## Release Notes
111
137
 
138
+ ### v1.4.0
139
+
140
+ - Add multi option flag to allow accepting multiple instances of an argument
141
+
142
+ ### v1.3.0
143
+
144
+ - Remove use of Fixnum for Ruby 2.4+ compatibility
145
+
112
146
  ### v1.2.0
113
147
 
114
148
  - Always return non-frozen strings
data/lib/yawpa.rb CHANGED
@@ -1,5 +1,3 @@
1
- require "yawpa/version"
2
-
3
1
  # Yet Another Way to Parse Arguments is an argument-parsing library for Ruby.
4
2
  #
5
3
  # Yawpa does not try to provide a fancy DSL.
@@ -14,6 +12,9 @@ require "yawpa/version"
14
12
  # parameters
15
13
  module Yawpa
16
14
 
15
+ # gem version
16
+ VERSION = "1.4.0"
17
+
17
18
  # Exception class raised when an unknown option is observed.
18
19
  class ArgumentParsingException < Exception; end
19
20
 
@@ -36,6 +37,7 @@ module Yawpa
36
37
  # username: {nargs: 1},
37
38
  # password: {nargs: 1},
38
39
  # color: :boolean,
40
+ # scan: {nargs: 1, multi: true},
39
41
  # }
40
42
  #
41
43
  # The keys of the +options+ Hash can be either strings or symbols.
@@ -59,6 +61,10 @@ module Yawpa
59
61
  # arguments to the option
60
62
  # - +:boolean+: if true, specify that the option is a toggleable
61
63
  # boolean option and allow a prefix of "no" to turn it off.
64
+ # - +:multi+: if true, instead of the last value specified for this
65
+ # option being returned, an Array of all values specified for this
66
+ # option will be returned, allowing the argument to appear multiple
67
+ # times in the argument list
62
68
  # @param flags [Hash]
63
69
  # Optional flags dictating how {.parse} should do its job.
64
70
  # @option flags [Boolean] :posix_order
@@ -98,13 +104,14 @@ module Yawpa
98
104
  raise ArgumentParsingException.new("Unknown option '#{param_name}'") unless opt_config
99
105
  param_key = opt_config[:key]
100
106
  if opt_config[:boolean]
101
- opts[param_key] = bool_val
107
+ param_val = bool_val
102
108
  elsif opt_config[:nargs].last == 0
103
- opts[param_key] = true
109
+ param_val = true
104
110
  else
105
- opts[param_key] = []
106
- i += _gather(opt_config[:nargs], i + 1, params, val, param_key, opts[param_key])
111
+ param_val = []
112
+ i += _gather(opt_config[:nargs], i + 1, params, val, param_key, param_val)
107
113
  end
114
+ _record_opt_val(opts, opt_config, param_key, param_val)
108
115
  elsif param =~ /^-(.+)$/
109
116
  short_flags = $1
110
117
  short_idx = 0
@@ -115,15 +122,17 @@ module Yawpa
115
122
  end
116
123
  param_key = opt_config[:key]
117
124
  if opt_config[:nargs].last == 0
118
- opts[param_key] = true
125
+ param_val = true
126
+ _record_opt_val(opts, opt_config, param_key, param_val)
119
127
  else
120
- opts[param_key] = []
128
+ param_val = []
121
129
  i += _gather(opt_config[:nargs],
122
130
  i + 1,
123
131
  params,
124
132
  short_flags[short_idx + 1, short_flags.length],
125
133
  param_key,
126
- opts[param_key])
134
+ param_val)
135
+ _record_opt_val(opts, opt_config, param_key, param_val)
127
136
  break
128
137
  end
129
138
  short_idx += 1
@@ -137,18 +146,24 @@ module Yawpa
137
146
  i += 1
138
147
  end
139
148
 
140
- # Condense 1-element arrays of option values to just the element itself
141
- opts.each_key do |k|
142
- if opts[k].is_a?(Array) and opts[k].length == 1
143
- opts[k] = opts[k].first
144
- end
145
- end
146
-
147
149
  return [opts, args]
148
150
  end
149
151
 
150
152
  private
151
153
 
154
+ def _record_opt_val(opts, opt_config, param_key, value)
155
+ # Condense 1-element arrays of option values to just the element itself
156
+ if value.is_a?(Array) and value.length == 1
157
+ value = value.first
158
+ end
159
+ if opt_config[:multi]
160
+ opts[param_key] ||= []
161
+ opts[param_key] << value
162
+ else
163
+ opts[param_key] = value
164
+ end
165
+ end
166
+
152
167
  # Internal helper method to gather arguments for an option
153
168
  def _gather(nargs, start_idx, params, initial, param_key, result)
154
169
  n_gathered = 0
@@ -185,6 +200,7 @@ module Yawpa
185
200
  newopts[newkey][:nargs] = nargs
186
201
  newopts[newkey][:short] = v[:short] || ''
187
202
  newopts[newkey][:boolean] = v[:boolean]
203
+ newopts[newkey][:multi] = v[:multi]
188
204
  end
189
205
  end
190
206
  end
data/spec/yawpa_spec.rb CHANGED
@@ -257,5 +257,36 @@ describe Yawpa do
257
257
  expect(args[0].frozen?).to be_falsey
258
258
  expect{args[0].sub!(/./, '-')}.to_not raise_error
259
259
  end
260
+
261
+ it "overwrites an option's value when multiple argument instances are present and multi flag is not set" do
262
+ options = {
263
+ scan: {short: "s", nargs: 1},
264
+ }
265
+ params = ['--scan', 'scan1', '-s', 'scan2', 'arg']
266
+ opts, args = Yawpa.parse(params, options)
267
+ expect(opts[:scan]).to eq('scan2')
268
+ expect(args).to eq(['arg'])
269
+ end
270
+
271
+ it "returns an Array of option values when multiple argument instances are present and multi flag is set" do
272
+ options = {
273
+ scan: {short: "s", nargs: 1, multi: true},
274
+ }
275
+ params = ['--scan', 'scan1', '-s', 'scan2', 'arg']
276
+ opts, args = Yawpa.parse(params, options)
277
+ expect(opts[:scan]).to eq(%w[scan1 scan2])
278
+ expect(args).to eq(['arg'])
279
+ end
280
+
281
+ it "returns an Array of Arrays of option values when multiple argument instances are present and multi flag is set and nargs > 1" do
282
+ options = {
283
+ opt: {short: "o", nargs: 2, multi: true},
284
+ }
285
+ params = %w[--opt o1 o2 -o o3 o4 a1 a2]
286
+ opts, args = Yawpa.parse(params, options)
287
+ expect(opts[:opt]).to eq([%w[o1 o2], %w[o3 o4]])
288
+ expect(args).to eq(%w[a1 a2])
289
+ end
290
+
260
291
  end
261
292
  end
data/yawpa.gemspec CHANGED
@@ -1,5 +1,5 @@
1
1
  # -*- encoding: utf-8 -*-
2
- require File.expand_path('../lib/yawpa/version', __FILE__)
2
+ require File.expand_path('../lib/yawpa', __FILE__)
3
3
 
4
4
  Gem::Specification.new do |gem|
5
5
  gem.authors = ["Josh Holtrop"]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yawpa
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Josh Holtrop
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-09-08 00:00:00.000000000 Z
11
+ date: 2023-03-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec
@@ -96,7 +96,6 @@ files:
96
96
  - README.md
97
97
  - Rakefile.rb
98
98
  - lib/yawpa.rb
99
- - lib/yawpa/version.rb
100
99
  - spec/spec_helper.rb
101
100
  - spec/yawpa_spec.rb
102
101
  - yawpa.gemspec
@@ -118,8 +117,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
118
117
  - !ruby/object:Gem::Version
119
118
  version: '0'
120
119
  requirements: []
121
- rubyforge_project:
122
- rubygems_version: 2.6.12
120
+ rubygems_version: 3.3.7
123
121
  signing_key:
124
122
  specification_version: 4
125
123
  summary: Yet Another Way to Parse Arguments
data/lib/yawpa/version.rb DELETED
@@ -1,4 +0,0 @@
1
- module Yawpa
2
- # gem version
3
- VERSION = "1.3.0"
4
- end