yawpa 1.2.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,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- Y2ViMTM4ZWVlZDBjNTNjMDUyMjEwNWJjZDMzMzhmODUxYTg4Y2I1YQ==
5
- data.tar.gz: !binary |-
6
- ZThmNmVjMDYzZTg4NjYxNDVlNjIwODgxYjRiZjc4N2NhYmUwMGY3Yw==
2
+ SHA256:
3
+ metadata.gz: e5fcd7ef44592d02c72ee21a350c6656b5cffccb8e4e2ce8b946409e3cfbf115
4
+ data.tar.gz: fc7be5044326da3ef8ec9259053b8b59c293c310ffe42344192abf887417d3bd
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- NjU4MDQwOWQ0ODk2MmEyMjVkZWI5NDQxMDg4OTI5NmY3MzcxZDNiNjJkMzNl
10
- NDE1ZjFlMDYwYTA0NzA3ODY5M2JjZDBmYTljNjY0MjJhYzA3MzExNDYzYWU2
11
- NzdhOGFkYTQ5YWQwMjcxNWE1MWRlOGYzYzFlMWRlNTczZjkyMGU=
12
- data.tar.gz: !binary |-
13
- YWRhNGM2MTZhMzljODM1Njc0YWE1ZGU4NDgxODMzYzBiYzdmMjA2ZWQ3M2Zm
14
- OTFmNjZhNjM4NmRhOWE5YTRiYTY1YzczZWNiN2RkODk3ZGE5MzQ3OGIyYTdi
15
- Y2Q3ZmI2N2Y4ZTM1ZWQyNDNiZmI0NzI2OTllMGYxYTMwNDIwMDk=
6
+ metadata.gz: ad4c50a7090a8a51a3435dd1fdbc774f33fb7eb1d4e84f513eee3aac5a9609460738b72701194f425cd0cef0f982f1817fe010510fd66c89dbae973bd0bc67c6
7
+ data.tar.gz: 7b5219aba7f6f2fc1a02724d61a8c997edeca3f91e5260996a17a346c9701a5c8271e2c774db2519cf30adaf1095ebdf86fd2787d876bb7b1b101a447380df57
data/Gemfile.lock CHANGED
@@ -1,37 +1,41 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- yawpa (1.2.0)
4
+ yawpa (1.4.0)
5
5
 
6
6
  GEM
7
7
  remote: https://rubygems.org/
8
8
  specs:
9
- diff-lcs (1.2.5)
10
- docile (1.1.5)
11
- json (1.8.3)
12
- rake (10.4.2)
13
- rdoc (4.2.2)
14
- json (~> 1.4)
15
- redcarpet (3.3.4)
16
- rspec (3.4.0)
17
- rspec-core (~> 3.4.0)
18
- rspec-expectations (~> 3.4.0)
19
- rspec-mocks (~> 3.4.0)
20
- rspec-core (3.4.4)
21
- rspec-support (~> 3.4.0)
22
- rspec-expectations (3.4.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)
23
23
  diff-lcs (>= 1.2.0, < 2.0)
24
- rspec-support (~> 3.4.0)
25
- rspec-mocks (3.4.1)
24
+ rspec-support (~> 3.12.0)
25
+ rspec-mocks (3.12.4)
26
26
  diff-lcs (>= 1.2.0, < 2.0)
27
- rspec-support (~> 3.4.0)
28
- rspec-support (3.4.1)
29
- simplecov (0.11.2)
30
- docile (~> 1.1.0)
31
- json (~> 1.8)
32
- simplecov-html (~> 0.10.0)
33
- simplecov-html (0.10.0)
34
- yard (0.8.7.6)
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)
35
39
 
36
40
  PLATFORMS
37
41
  ruby
@@ -39,11 +43,10 @@ PLATFORMS
39
43
  DEPENDENCIES
40
44
  rake
41
45
  rdoc
42
- redcarpet
43
46
  rspec
44
47
  simplecov
45
48
  yard
46
49
  yawpa!
47
50
 
48
51
  BUNDLED WITH
49
- 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
@@ -181,10 +196,11 @@ module Yawpa
181
196
  newkey = k.to_s
182
197
  newopts[newkey] = {key: k}
183
198
  nargs = v[:nargs] || 0
184
- nargs = (nargs..nargs) if nargs.is_a?(Fixnum)
199
+ nargs = (nargs..nargs) if nargs.is_a?(Integer)
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"]
@@ -20,5 +20,4 @@ Gem::Specification.new do |gem|
20
20
  gem.add_development_dependency "rake"
21
21
  gem.add_development_dependency "rdoc"
22
22
  gem.add_development_dependency "yard"
23
- gem.add_development_dependency "redcarpet"
24
23
  end
metadata CHANGED
@@ -1,97 +1,83 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: yawpa
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.2.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: 2016-04-19 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
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ! '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ! '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: simplecov
29
29
  requirement: !ruby/object:Gem::Requirement
30
30
  requirements:
31
- - - ! '>='
31
+ - - ">="
32
32
  - !ruby/object:Gem::Version
33
33
  version: '0'
34
34
  type: :development
35
35
  prerelease: false
36
36
  version_requirements: !ruby/object:Gem::Requirement
37
37
  requirements:
38
- - - ! '>='
38
+ - - ">="
39
39
  - !ruby/object:Gem::Version
40
40
  version: '0'
41
41
  - !ruby/object:Gem::Dependency
42
42
  name: rake
43
43
  requirement: !ruby/object:Gem::Requirement
44
44
  requirements:
45
- - - ! '>='
45
+ - - ">="
46
46
  - !ruby/object:Gem::Version
47
47
  version: '0'
48
48
  type: :development
49
49
  prerelease: false
50
50
  version_requirements: !ruby/object:Gem::Requirement
51
51
  requirements:
52
- - - ! '>='
52
+ - - ">="
53
53
  - !ruby/object:Gem::Version
54
54
  version: '0'
55
55
  - !ruby/object:Gem::Dependency
56
56
  name: rdoc
57
57
  requirement: !ruby/object:Gem::Requirement
58
58
  requirements:
59
- - - ! '>='
59
+ - - ">="
60
60
  - !ruby/object:Gem::Version
61
61
  version: '0'
62
62
  type: :development
63
63
  prerelease: false
64
64
  version_requirements: !ruby/object:Gem::Requirement
65
65
  requirements:
66
- - - ! '>='
66
+ - - ">="
67
67
  - !ruby/object:Gem::Version
68
68
  version: '0'
69
69
  - !ruby/object:Gem::Dependency
70
70
  name: yard
71
71
  requirement: !ruby/object:Gem::Requirement
72
72
  requirements:
73
- - - ! '>='
73
+ - - ">="
74
74
  - !ruby/object:Gem::Version
75
75
  version: '0'
76
76
  type: :development
77
77
  prerelease: false
78
78
  version_requirements: !ruby/object:Gem::Requirement
79
79
  requirements:
80
- - - ! '>='
81
- - !ruby/object:Gem::Version
82
- version: '0'
83
- - !ruby/object:Gem::Dependency
84
- name: redcarpet
85
- requirement: !ruby/object:Gem::Requirement
86
- requirements:
87
- - - ! '>='
88
- - !ruby/object:Gem::Version
89
- version: '0'
90
- type: :development
91
- prerelease: false
92
- version_requirements: !ruby/object:Gem::Requirement
93
- requirements:
94
- - - ! '>='
80
+ - - ">="
95
81
  - !ruby/object:Gem::Version
96
82
  version: '0'
97
83
  description: Yet Another Way to Parse Arguments is an argument-parsing library for
@@ -102,15 +88,14 @@ executables: []
102
88
  extensions: []
103
89
  extra_rdoc_files: []
104
90
  files:
105
- - .gitignore
106
- - .rspec
91
+ - ".gitignore"
92
+ - ".rspec"
107
93
  - Gemfile
108
94
  - Gemfile.lock
109
95
  - LICENSE
110
96
  - README.md
111
97
  - Rakefile.rb
112
98
  - lib/yawpa.rb
113
- - lib/yawpa/version.rb
114
99
  - spec/spec_helper.rb
115
100
  - spec/yawpa_spec.rb
116
101
  - yawpa.gemspec
@@ -123,21 +108,19 @@ require_paths:
123
108
  - lib
124
109
  required_ruby_version: !ruby/object:Gem::Requirement
125
110
  requirements:
126
- - - ! '>='
111
+ - - ">="
127
112
  - !ruby/object:Gem::Version
128
113
  version: '0'
129
114
  required_rubygems_version: !ruby/object:Gem::Requirement
130
115
  requirements:
131
- - - ! '>='
116
+ - - ">="
132
117
  - !ruby/object:Gem::Version
133
118
  version: '0'
134
119
  requirements: []
135
- rubyforge_project:
136
- rubygems_version: 2.4.8
120
+ rubygems_version: 3.3.7
137
121
  signing_key:
138
122
  specification_version: 4
139
123
  summary: Yet Another Way to Parse Arguments
140
124
  test_files:
141
125
  - spec/spec_helper.rb
142
126
  - spec/yawpa_spec.rb
143
- has_rdoc:
data/lib/yawpa/version.rb DELETED
@@ -1,4 +0,0 @@
1
- module Yawpa
2
- # gem version
3
- VERSION = "1.2.0"
4
- end