slop 4.6.1 → 4.8.2

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: fb0866db90cc2226117322463845a56a78f43cc2
4
- data.tar.gz: acd05e15e79c70f68d6248a9bdcf8e3c1afa128f
2
+ SHA256:
3
+ metadata.gz: b79c8ecabc4f59e446e17b38a61381a4711e1da44918d5bfa3c459e3330fa7c5
4
+ data.tar.gz: 6a5c4c2b6a0624a0f533ea86bc7855774a23d134d581f178d2d6fa476b01da59
5
5
  SHA512:
6
- metadata.gz: 276b3847e95efdafb6eb47516168a3e4c984db3cf2b60c88eb8d7a9d67815956ea3d039a9ddc04dfc0d25c1ada21ab7c7d9a9825b39c31d85ab79e53b605cc78
7
- data.tar.gz: 211808f3fae95a2c7d69bee7072df3e8b05cc5c2c134d81706143d459741af3914f042a8b08a6952aa8628f5324d0cff50def90eb297a5307c87ce4794e8eb49
6
+ metadata.gz: 15ac86c2fe54c3c5dffb8d186e50341febfa6ed858eedae5347a416fc9261159262d68873645c7bb11d1cc2155473f6deb3a8740da2c4fcc1a78f0f477cccfc8
7
+ data.tar.gz: d1a078ebc9c8ddd2b970e9cc39dcf72fc20b3e67a50abed4bf51a7d22bafe8ae7170c03930bf8d3073f487d167f6f27fe1475b5e5735b4bf2090efa446a0eff5
@@ -1,15 +1,27 @@
1
1
  cache: bundler
2
2
  before_install:
3
- - gem install bundler
3
+ - |
4
+ export RVM_CURRENT=`rvm current|cut -c6-8`
5
+ if [ "${RVM_CURRENT}" = "2.0" ] || \
6
+ [ "${RVM_CURRENT}" = "2.1" ] || \
7
+ [ "${RVM_CURRENT}" = "2.2" ]; then
8
+ gem install bundler -v '< 2'
9
+ fi
10
+
4
11
  rvm:
5
12
  - 2.0.0
6
13
  - 2.1
7
14
  - 2.2
8
15
  - 2.3.4
9
- - 2.4.1
10
- - jruby-9.1.13.0
16
+ - 2.4.9
17
+ - 2.5.7
18
+ - 2.6.5
19
+ - 2.7.0
20
+ - jruby-9.2.9.0
11
21
  - jruby-head
12
22
  - ruby-head
23
+ jdk:
24
+ - openjdk8
13
25
  notifications:
14
26
  email:
15
27
  on_success: change
@@ -1,26 +1,70 @@
1
1
  Changelog
2
2
  =========
3
3
 
4
+ v4.8.2 (2020-07-10)
5
+ -------------------
6
+
7
+ Bug fixes:
8
+ * Fix bug where separator position was messed up if using `help: false`
9
+ [#253](https://github.com/leejarvis/slop/issues/253)
10
+
11
+ v4.8.1 (2020-03-31)
12
+ -------------------
13
+
14
+ Bug fixes:
15
+ * Fix keyword argument warning. [#251](https://github.com/leejarvis/slop/pull/251)
16
+
17
+
18
+ v4.8.0 (2020-01-17)
19
+ -------------------
20
+
21
+ Features:
22
+ * Add support for prefixing integer values with `+` character
23
+ [#243](https://github.com/leejarvis/slop/pull/243) (Juha Ylitalo)
24
+ * Add support for parsing floats with scientific notation
25
+ [#250](https://github.com/leejarvis/slop/pull/250) (Hansuk Hong)
26
+
27
+ Maintenance:
28
+ * Add 2.7.0 to CI and fix warnings
29
+ [#248](https://github.com/leejarvis/slop/pull/248) (Juha Ylitalo, Andrew Kane)
30
+
31
+ v4.7.0 (2019-06-29)
32
+ -------------------
33
+
34
+ Features:
35
+ * Add `Slop::Result#fetch`. It returns the value of given option, or raises an error if given option is not present. [#232](https://github.com/leejarvis/slop/pull/232) ([Giovanni Benussi](https://github.com/giovannibenussi))
36
+ * Adding a separator without passing any arguments now creates a separator with the empty string. [#238](https://github.com/leejarvis/slop/pull/238) ([Teemu Matilainen](https://github.com/tmatilai))
37
+
38
+ Bug fixes
39
+ * Ensure non-string option types have their flags consumed properly [#241] (Sutou Kouhei)
40
+
41
+
42
+ v4.6.2 (2018-03-12)
43
+ -------------------
44
+
45
+ Bug fixes/Enhancements
46
+ * Fix equals character (=) being parsed incorrectly in some cases. [#226](https://github.com/leejarvis/slop/issues/226)
47
+
4
48
  v4.6.1 (2017-11-20)
5
49
  -------------------
6
50
 
7
51
  Bug fixes/Enhancements
8
- * Fix separator so it doesn't mutate user data. #223 (Marc-André Lafortune)
52
+ * Fix separator so it doesn't mutate user data. [#223](https://github.com/leejarvis/slop/issues/223) (Marc-André Lafortune)
9
53
  * Add additional tests for `Options#separator` and fix issue where
10
- the last separator was ignored. #222
54
+ the last separator was ignored. [#222](https://github.com/leejarvis/slop/issues/222)
11
55
 
12
56
  v4.6.0 (2017-10-06)
13
57
  -------------------
14
58
 
15
59
  Features
16
- * Add support for required options. #218 (William Woodruff)
60
+ * Add support for required options. [#218](https://github.com/leejarvis/slop/issues/218) (William Woodruff)
17
61
 
18
62
  v4.5.0 (2017-05-22)
19
63
  -------------------
20
64
 
21
65
  Features:
22
66
  * Added config option to avoid translating flags-with-dashes into
23
- underscores. #206 (@lbriais)
67
+ underscores. [#206](https://github.com/leejarvis/slop/issues/206) (@lbriais)
24
68
 
25
69
  v4.4.3 (2017-05-02)
26
70
  -------------------
@@ -32,73 +76,73 @@ v4.4.2 (2017-04-29)
32
76
  -------------------
33
77
 
34
78
  Bug fixes:
35
- * Fix support for parsing -x5 or -nfoo. #199
36
- * Fix removing arguments after `--`. #194
79
+ * Fix support for parsing -x5 or -nfoo. [#199](https://github.com/leejarvis/slop/issues/199)
80
+ * Fix removing arguments after `--`. [#194](https://github.com/leejarvis/slop/issues/194)
37
81
 
38
82
  v4.4.1 (2016-08-21)
39
83
  -------------------
40
84
 
41
85
  Bug fixes:
42
- * Handle bad constant names in `Slop.option_defined?`. #198
86
+ * Handle bad constant names in `Slop.option_defined?`. [#198](https://github.com/leejarvis/slop/issues/198)
43
87
  (Ellen Marie Dash)
44
88
 
45
89
  v4.4.0 (2016-08-15)
46
90
  -------------------
47
91
 
48
92
  Features
49
- * Support parsing arguments prefixed with dashes. #192 (Andrew Clemons)
93
+ * Support parsing arguments prefixed with dashes. [#192](https://github.com/leejarvis/slop/issues/192) (Andrew Clemons)
50
94
 
51
95
  Bug fixes:
52
- * Retain sort order inside tail sort. #193 (Caio Chassot)
96
+ * Retain sort order inside tail sort. [#193](https://github.com/leejarvis/slop/issues/193) (Caio Chassot)
53
97
 
54
98
  v4.3.0 (2016-03-19)
55
99
  -------------------
56
100
 
57
101
  Features
58
- * Allow disabling array delimiter. #189 (Mike Pastore)
59
- * Allow passing custom banner as config. #191 (Philip Rees)
102
+ * Allow disabling array delimiter. [#189](https://github.com/leejarvis/slop/issues/189) (Mike Pastore)
103
+ * Allow passing custom banner as config. [#191](https://github.com/leejarvis/slop/issues/191) (Philip Rees)
60
104
 
61
105
  v4.2.1 (2015-11-25)
62
106
  -------------------
63
107
 
64
108
  Features:
65
- * Better handling of option names with multiple words. #169 (Tim Rogers)
109
+ * Better handling of option names with multiple words. [#169](https://github.com/leejarvis/slop/issues/169) (Tim Rogers)
66
110
 
67
111
  Minor enhancements:
68
- * add ARGF notes to Arguments (README). #173 (Rick Hull)
112
+ * add ARGF notes to Arguments (README). [#173](https://github.com/leejarvis/slop/issues/173) (Rick Hull)
69
113
 
70
114
  Bug fixes:
71
- * Fix arguments removed with option arguments. #182 (Naoki Mizuno)
115
+ * Fix arguments removed with option arguments. [#182](https://github.com/leejarvis/slop/issues/182) (Naoki Mizuno)
72
116
  * Fix bug where true is passed to BoolOption block regardless
73
- of --no- prefix. #184 (Ben Brady)
74
- * only raise MissingArgument if not `default_value`. #163 (Ben Brady)
117
+ of --no- prefix. [#184](https://github.com/leejarvis/slop/issues/184) (Ben Brady)
118
+ * only raise MissingArgument if not `default_value`. [#163](https://github.com/leejarvis/slop/issues/163) (Ben Brady)
75
119
 
76
120
  v4.2.0 (2015-04-18)
77
121
  -------------------
78
122
 
79
123
  Features:
80
- * Support for Regexp option type #167 (Laurent Arnoud)
124
+ * Support for Regexp option type [#167](https://github.com/leejarvis/slop/issues/167) (Laurent Arnoud)
81
125
  * Support prefixed `--no-` for explicitly setting boolean options
82
- to `false` #168
83
- * Better handling of flags with multiple words #169 (Tim Rogers)
126
+ to `false` [#168](https://github.com/leejarvis/slop/issues/168)
127
+ * Better handling of flags with multiple words [#169](https://github.com/leejarvis/slop/issues/169) (Tim Rogers)
84
128
 
85
129
  v4.1.0 (2015-04-18)
86
130
  -------------------
87
131
 
88
132
  Features:
89
- * Support for FloatOption #156 (Rick Hull)
133
+ * Support for FloatOption [#156](https://github.com/leejarvis/slop/issues/156) (Rick Hull)
90
134
  * Support for `limit` config to ArrayOption.
91
135
  * Support for `tail` config to add options to the bottom of
92
136
  the help text.
93
- * Add explicit setter (#[]=) to Result class. #162
137
+ * Add explicit setter (#[]=) to Result class. [#162](https://github.com/leejarvis/slop/issues/162)
94
138
  * Implement flag gettings for UnknownOption and MissingArgument
95
- error classes. #165 (sigurdsvela)
139
+ error classes. [#165](https://github.com/leejarvis/slop/issues/165) (sigurdsvela)
96
140
 
97
141
  Minor enhancements:
98
142
  * Reset parser every time `parse` is called.
99
143
 
100
144
  Bug fixes:
101
- * Remove "--" from unprocessed arguments #157 (David Rodríguez).
145
+ * Remove "--" from unprocessed arguments [#157](https://github.com/leejarvis/slop/issues/157) (David Rodríguez).
102
146
 
103
147
  v4.0.0 (2014-12-27)
104
148
  -------------------
data/README.md CHANGED
@@ -3,9 +3,6 @@ Slop
3
3
 
4
4
  Slop is a simple option parser with an easy to remember syntax and friendly API.
5
5
 
6
- Version 4 of Slop is aimed at Ruby 2.0 or later. Please use
7
- [Version 3](https://github.com/leejarvis/slop/tree/v3) for Ruby 1.9 support.
8
-
9
6
  [![Build Status](https://travis-ci.org/leejarvis/slop.svg?branch=master)](http://travis-ci.org/leejarvis/slop)
10
7
 
11
8
  Installation
@@ -275,89 +272,4 @@ end
275
272
  Commands
276
273
  --------
277
274
 
278
- As of version 4, Slop does not have built in support for git-style subcommands.
279
- You can use version 3 of Slop (see `v3` branch). I also expect there to be some
280
- external libraries released soon that wrap around Slop to provide support for
281
- this feature. I'll update this document when that happens.
282
-
283
- Upgrading from version 3
284
- ------------------------
285
-
286
- Slop v4 is completely non-backwards compatible. The code has been rewritten
287
- from the ground up. If you're already using version 3 you *have* to update
288
- your code to use version 4. Here's an overview of the more fundamental changes:
289
-
290
- #### No more `instance_eval`
291
-
292
- Before:
293
-
294
- ```ruby
295
- Slop.parse do
296
- on 'v', 'version' do
297
- puts VERSION
298
- end
299
- end
300
- ```
301
-
302
- After:
303
-
304
- ```ruby
305
- Slop.parse do |o|
306
- o.on '-v', '--version' do
307
- puts VERSION
308
- end
309
- end
310
- ```
311
-
312
- #### No more `as` for option types
313
-
314
- Instead, the type is declared in the method call. Before:
315
-
316
- ```ruby
317
- on 'port=', as: Integer
318
- ```
319
-
320
- After:
321
-
322
- ```ruby
323
- o.int '--port' # or integer
324
- ```
325
-
326
- See the custom types section of the document.
327
-
328
- #### No more trailing `=`
329
-
330
- Instead, the "does this option expect an argument" question is answered by
331
- the option type (i.e `on` and `bool` options do not expect arguments, all
332
- others do. They handle type conversion, too.
333
-
334
- #### Hyphens are required
335
-
336
- This was a hard decision to make, but you must provide prefixed hyphens when
337
- declaring your flags. This makes the underlying code much nicer and much less
338
- ambiguous, which leads to less error prone code. It also means you can easily
339
- support single hyphen prefix for a long flag, i.e `-hostname` which you
340
- could not do before. It also provides a hidden feature, which is infinity flag
341
- aliases: `o.string '-f', '-x', '--foo', '--bar', 'this is insane'`
342
-
343
- #### Strict is now on by default
344
-
345
- v3 had a `strict` option. v4 has no such option, and to suppress errors you can
346
- instead provide the `suppress_errors: true` option to Slop.
347
-
348
- #### No more parse!
349
-
350
- Where v3 has both `Slop.parse` and `Slop.parse!`, v4 only has `parse`. The
351
- former was used to decide whether Slop should or should not mutate the
352
- original args (usually ARGV). This is almost never what you want, and it
353
- can lead to confusion. Instead, `Slop::Result` provides an `arguments`
354
- methods:
355
-
356
- ```ruby
357
- opts = Slop.parse do |o|
358
- o.string '--hostname', '...'
359
- end
360
-
361
- # ARGV is "hello --hostname foo bar"
362
- p opts.arguments #=> ["hello", "bar"]
363
- ```
275
+ Slop not longer has built in support for git-style subcommands.
@@ -6,7 +6,7 @@ require 'slop/types'
6
6
  require 'slop/error'
7
7
 
8
8
  module Slop
9
- VERSION = '4.6.1'
9
+ VERSION = '4.8.2'
10
10
 
11
11
  # Parse an array of options (defaults to ARGV). Accepts an
12
12
  # optional hash of configuration options and block.
@@ -20,7 +20,7 @@ module Slop
20
20
  #
21
21
  # Returns a Slop::Result.
22
22
  def self.parse(items = ARGV, **config, &block)
23
- Options.new(config, &block).parse(items)
23
+ Options.new(**config, &block).parse(items)
24
24
  end
25
25
 
26
26
  # Example:
@@ -22,8 +22,9 @@ module Slop
22
22
  end
23
23
  end
24
24
 
25
- # Raised when an unknown option is parsed. Suppress
26
- # with the `suppress_errors` config option.
25
+ # Raised when an unknown option is parsed or when trying to fetch an
26
+ # unexisting option via `Slop::Result#fetch`.
27
+ # Suppress with the `suppress_errors` config option.
27
28
  class UnknownOption < Error
28
29
  attr_reader :flag
29
30
 
@@ -24,12 +24,12 @@ module Slop
24
24
  # The String banner prefixed to the help string.
25
25
  attr_accessor :banner
26
26
 
27
- def initialize(**config)
27
+ def initialize(**config, &block)
28
28
  @options = []
29
29
  @separators = []
30
30
  @banner = config[:banner].is_a?(String) ? config[:banner] : config.fetch(:banner, "usage: #{$0} [options]")
31
31
  @config = DEFAULT_CONFIG.merge(config)
32
- @parser = Parser.new(self, @config)
32
+ @parser = Parser.new(self, **@config)
33
33
 
34
34
  yield self if block_given?
35
35
  end
@@ -52,14 +52,14 @@ module Slop
52
52
  desc = flags.pop unless flags.last.start_with?('-')
53
53
  config = self.config.merge(config)
54
54
  klass = Slop.string_to_option_class(config[:type].to_s)
55
- option = klass.new(flags, desc, config, &block)
55
+ option = klass.new(flags, desc, **config, &block)
56
56
 
57
57
  add_option option
58
58
  end
59
59
 
60
60
  # Add a separator between options. Used when displaying
61
61
  # the help text.
62
- def separator(string)
62
+ def separator(string = "")
63
63
  if separators[options.size]
64
64
  separators[-1] += "\n#{string}"
65
65
  else
@@ -82,7 +82,7 @@ module Slop
82
82
  def method_missing(name, *args, **config, &block)
83
83
  if respond_to_missing?(name)
84
84
  config[:type] = name
85
- on(*args, config, &block)
85
+ on(*args, **config, &block)
86
86
  else
87
87
  super
88
88
  end
@@ -102,13 +102,13 @@ module Slop
102
102
  str = config[:banner] ? "#{banner}\n" : ""
103
103
  len = longest_flag_length
104
104
 
105
- options.select(&:help?).each_with_index.sort_by{ |o,i| [o.tail, i] }.each do |opt, i|
105
+ options.select.each_with_index.sort_by{ |o,i| [o.tail, i] }.each do |opt, i|
106
106
  # use the index to fetch an associated separator
107
107
  if sep = separators[i]
108
108
  str << "#{sep}\n"
109
109
  end
110
110
 
111
- str << "#{prefix}#{opt.to_s(offset: len)}\n"
111
+ str << "#{prefix}#{opt.to_s(offset: len)}\n" if opt.help?
112
112
  end
113
113
 
114
114
  if sep = separators[options.size]
@@ -52,9 +52,8 @@ module Slop
52
52
 
53
53
  # support `foo=bar`
54
54
  orig_flag = flag.dup
55
- orig_arg = arg
56
- if flag.include?("=")
57
- flag, arg = flag.split("=")
55
+ if match = flag.match(/([^=]+)=([^=]+)/)
56
+ flag, arg = match.captures
58
57
  end
59
58
 
60
59
  if opt = try_process(flag, arg)
@@ -64,7 +63,7 @@ module Slop
64
63
  if opt.expects_argument?
65
64
 
66
65
  # if we consumed the argument, remove the next pair
67
- if orig_arg == opt.value.to_s
66
+ if consume_next_argument?(orig_flag)
68
67
  pairs.delete_at(idx + 1)
69
68
  end
70
69
 
@@ -106,6 +105,13 @@ module Slop
106
105
 
107
106
  private
108
107
 
108
+ def consume_next_argument?(flag)
109
+ return false if flag.include?("=")
110
+ return true if flag.start_with?("--")
111
+ return true if /\A-[a-zA-Z]\z/ === flag
112
+ false
113
+ end
114
+
109
115
  # We've found an option, process and return it
110
116
  def process(option, arg)
111
117
  option.ensure_call(arg)
@@ -14,12 +14,23 @@ module Slop
14
14
  @options = parser.options
15
15
  end
16
16
 
17
- # Returns an options value, nil if the option does not exist.
17
+ # Returns an option's value, nil if the option does not exist.
18
18
  def [](flag)
19
19
  (o = option(flag)) && o.value
20
20
  end
21
21
  alias get []
22
22
 
23
+ # Returns an option's value, raises UnknownOption if the option does not exist.
24
+ def fetch(flag)
25
+ o = option(flag)
26
+ if o.nil?
27
+ cleaned_key = clean_key(flag)
28
+ raise UnknownOption.new("option not found: '#{cleaned_key}'", "#{cleaned_key}")
29
+ else
30
+ o.value
31
+ end
32
+ end
33
+
23
34
  # Set the value for an option. Raises an ArgumentError if the option
24
35
  # does not exist.
25
36
  def []=(flag, value)
@@ -33,13 +44,8 @@ module Slop
33
44
 
34
45
  # Returns an Option if it exists. Ignores any prefixed hyphens.
35
46
  def option(flag)
36
- cleaned = -> (f) do
37
- key = f.to_s.sub(/\A--?/, '')
38
- key = key.tr '-', '_' if parser.config[:underscore_flags]
39
- key.to_sym
40
- end
41
47
  options.find do |o|
42
- o.flags.any? { |f| cleaned.(f) == cleaned.(flag) }
48
+ o.flags.any? { |f| clean_key(f) == clean_key(flag) }
43
49
  end
44
50
  end
45
51
 
@@ -90,5 +96,13 @@ module Slop
90
96
  def to_s(**opts)
91
97
  options.to_s(**opts)
92
98
  end
99
+
100
+ private
101
+
102
+ def clean_key(key)
103
+ key = key.to_s.sub(/\A--?/, '')
104
+ key = key.tr '-', '_' if parser.config[:underscore_flags]
105
+ key.to_sym
106
+ end
93
107
  end
94
108
  end
@@ -44,16 +44,17 @@ module Slop
44
44
  # Cast the option argument to an Integer.
45
45
  class IntegerOption < Option
46
46
  def call(value)
47
- value =~ /\A-?\d+\z/ && value.to_i
47
+ value =~ /\A[+-]?\d+\z/ && value.to_i
48
48
  end
49
49
  end
50
50
  IntOption = IntegerOption
51
51
 
52
52
  # Cast the option argument to a Float.
53
53
  class FloatOption < Option
54
+ FLOAT_STRING_REGEXP = /\A[+-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+-]?\d+)?\z/.freeze
55
+
54
56
  def call(value)
55
- # TODO: scientific notation, etc.
56
- value =~ /\A-?\d*\.*\d+\z/ && value.to_f
57
+ value =~ FLOAT_STRING_REGEXP && value.to_f
57
58
  end
58
59
  end
59
60
 
@@ -1,8 +1,8 @@
1
1
  require 'test_helper'
2
2
 
3
3
  describe Slop::Option do
4
- def option(*args)
5
- Slop::Option.new(*args)
4
+ def option(*args, **kwargs, &block)
5
+ Slop::Option.new(*args, **kwargs, &block)
6
6
  end
7
7
 
8
8
  describe "#flag" do
@@ -64,6 +64,23 @@ describe Slop::Options do
64
64
  @options.separator("foo".freeze)
65
65
  @options.separator("bar".freeze)
66
66
  end
67
+
68
+ it "defaults to empty string" do
69
+ @options.separator
70
+
71
+ assert_equal [""], @options.separators
72
+ end
73
+
74
+ it "correctly handles options with `help: false`" do
75
+ @options.boolean "--opt1"
76
+ @options.boolean "--opt2", help: false
77
+ @options.separator "other options"
78
+ @options.boolean "--opt3", help: false
79
+ @options.boolean "--opt4"
80
+
81
+ _usage, help = @options.to_s.squeeze(" ").split("\n", 2)
82
+ assert_equal "--opt1 \nother options\n --opt4", help.strip
83
+ end
67
84
  end
68
85
 
69
86
  describe "#method_missing" do
@@ -115,11 +132,11 @@ describe Slop::Options do
115
132
 
116
133
  describe "custom banner" do
117
134
  it "is prefixed with defined banner" do
118
- @options_config = Slop::Options.new({banner: "custom banner"})
135
+ @options_config = Slop::Options.new(**{banner: "custom banner"})
119
136
  assert_match(/^custom banner/, @options_config.to_s)
120
137
  end
121
138
  it "banner is disabled" do
122
- @options_config = Slop::Options.new({banner: false})
139
+ @options_config = Slop::Options.new(**{banner: false})
123
140
  assert_match("", @options_config.to_s)
124
141
  end
125
142
  end
@@ -21,6 +21,11 @@ describe Slop::Parser do
21
21
  @result.parser.parse %w(--name=bob -p=123)
22
22
  assert_equal "bob", @result[:name]
23
23
  assert_equal 123, @result[:port]
24
+
25
+ @options.string "--foo"
26
+ @result.parser.parse %w(--foo = =)
27
+ assert_equal "=", @result[:foo]
28
+ assert_equal %w(=), @result.args
24
29
  end
25
30
 
26
31
  it "parses arg with leading -" do
@@ -30,6 +35,12 @@ describe Slop::Parser do
30
35
  assert_equal "--sometext", @result[:text]
31
36
  end
32
37
 
38
+ it "parses regexp arg with leading -" do
39
+ @options.regexp "--pattern"
40
+ @result.parser.parse %w(--pattern -x)
41
+ assert_equal(/-x/, @result[:pattern])
42
+ end
43
+
33
44
  it "parses negative integer" do
34
45
  @options.integer "-p", "--port"
35
46
  @result.parser.parse %w(--name=bob --port -123)
@@ -62,6 +62,31 @@ describe Slop::Result do
62
62
  end
63
63
  end
64
64
 
65
+ describe "#fetch" do
66
+ it "returns an options value" do
67
+ assert_equal "lee", @result.fetch("--name")
68
+ end
69
+
70
+ it "raises Slop::UnknownOption when an option does not exists" do
71
+ e = assert_raises(Slop::UnknownOption) { @result.fetch("--unexisting") }
72
+ assert_equal "option not found: 'unexisting'", e.message
73
+ end
74
+
75
+ it "returns the default value of an option when a value is not provided" do
76
+ @options.string("--foo", default: "bar")
77
+ @result.parser.parse %w(--foo)
78
+
79
+ assert_equal 'bar', @result.fetch('foo')
80
+ end
81
+
82
+ it "returns nil when an option is not provided and it does not have a default value" do
83
+ @options.string("--hello")
84
+ @result.parser.parse %w()
85
+
86
+ assert_nil @result.fetch('hello')
87
+ end
88
+ end
89
+
65
90
  describe "#[]=" do
66
91
  it "sets an options value" do
67
92
  assert_equal "lee", @result["name"]
@@ -1,6 +1,16 @@
1
1
  require "test_helper"
2
2
 
3
3
  describe Slop do
4
+ describe ".parse" do
5
+ it "parses a list of arguments" do
6
+ result = Slop.parse(%w[--name Lee]) do |o|
7
+ o.string "--name"
8
+ end
9
+
10
+ assert_equal "Lee", result[:name]
11
+ end
12
+ end
13
+
4
14
  describe ".option_defined?" do
5
15
  it "handles bad constant names" do
6
16
  assert_equal false, Slop.option_defined?("Foo?Bar")
@@ -32,11 +32,15 @@ describe Slop::IntegerOption do
32
32
  before do
33
33
  @options = Slop::Options.new
34
34
  @age = @options.integer "--age"
35
- @result = @options.parse %w(--age 20)
35
+ @minus = @options.integer "--minus"
36
+ @plus = @options.integer "--plus"
37
+ @result = @options.parse %w(--age 20 --minus -10 --plus +30)
36
38
  end
37
39
 
38
40
  it "returns the value as an integer" do
39
41
  assert_equal 20, @result[:age]
42
+ assert_equal (-10), @result[:minus]
43
+ assert_equal 30, @result[:plus]
40
44
  end
41
45
 
42
46
  it "returns nil for non-numbers by default" do
@@ -50,11 +54,37 @@ describe Slop::FloatOption do
50
54
  @options = Slop::Options.new
51
55
  @apr = @options.float "--apr"
52
56
  @apr_value = 2.9
53
- @result = @options.parse %W(--apr #{@apr_value})
57
+ @minus = @options.float "--minus"
58
+ @plus = @options.float "--plus"
59
+ @scientific_notation = @options.float "--scientific-notation"
60
+ @scientific_notation_value = 4e21
61
+ @result = @options.parse %W(--apr #{@apr_value} --minus -6.1 --plus +9.4 --scientific-notation #{@scientific_notation_value})
54
62
  end
55
63
 
56
64
  it "returns the value as a float" do
57
65
  assert_equal @apr_value, @result[:apr]
66
+ assert_equal (-6.1), @result[:minus]
67
+ assert_equal 9.4, @result[:plus]
68
+ end
69
+
70
+ it "parses scientific notations" do
71
+ assert_equal @scientific_notation_value, @result[:scientific_notation]
72
+
73
+ @scientific_notation_value = 4E21
74
+ @result = @options.parse %W(--scientific-notation #{@scientific_notation_value})
75
+ assert_equal @scientific_notation_value, @result[:scientific_notation]
76
+
77
+ @scientific_notation_value = 4.0e21
78
+ @result = @options.parse %W(--scientific-notation #{@scientific_notation_value})
79
+ assert_equal @scientific_notation_value, @result[:scientific_notation]
80
+
81
+ @scientific_notation_value = -4e21
82
+ @result = @options.parse %W(--scientific-notation #{@scientific_notation_value})
83
+ assert_equal @scientific_notation_value, @result[:scientific_notation]
84
+
85
+ @scientific_notation_value = 4e-21
86
+ @result = @options.parse %W(--scientific-notation #{@scientific_notation_value})
87
+ assert_equal @scientific_notation_value, @result[:scientific_notation]
58
88
  end
59
89
 
60
90
  it "returns nil for non-numbers by default" do
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slop
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.6.1
4
+ version: 4.8.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lee Jarvis
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-11-20 00:00:00.000000000 Z
11
+ date: 2020-07-10 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -71,7 +71,7 @@ homepage: http://github.com/leejarvis/slop
71
71
  licenses:
72
72
  - MIT
73
73
  metadata: {}
74
- post_install_message:
74
+ post_install_message:
75
75
  rdoc_options: []
76
76
  require_paths:
77
77
  - lib
@@ -86,9 +86,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
86
86
  - !ruby/object:Gem::Version
87
87
  version: '0'
88
88
  requirements: []
89
- rubyforge_project:
90
- rubygems_version: 2.5.2.1
91
- signing_key:
89
+ rubygems_version: 3.0.3
90
+ signing_key:
92
91
  specification_version: 4
93
92
  summary: Simple Lightweight Option Parsing
94
93
  test_files: