slop 4.6.2 → 4.9.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: 2ebf99ce79c4681f07967df62008038b3b3ef878
4
- data.tar.gz: e074639079cede3024fc00eac71218a744427678
2
+ SHA256:
3
+ metadata.gz: a823b880e0feb218800faed1d191258594fb26ab2078fdfd5282df024677fd06
4
+ data.tar.gz: fe212b6f93ff3548873896606de836842081319c3acbfd99438a15b38539c2a2
5
5
  SHA512:
6
- metadata.gz: 28ea608999bd09c8c02a43d9f67d571590dfd8742d3dd6712b5a67d3259d80273cfe3e434285dba0b6c524481d38de2fb2db599c4cab8d52d080088ace093cfb
7
- data.tar.gz: 2cbedaf601a63a4a5ae57e46ea832827c54ebde7c69319f8313c034f89b0482af0f767fd6a4f9adbcfd5834ca31d7f6a087e4e60240399cbe96a19a3019a6380
6
+ metadata.gz: 467b52c6de36c7a164739b301ef016efcef5abd62891682e8353f67f63b125bc8238abd853112fb544cdc2391d93dc492446c93a01220a5ef8c908ff2cb95ee1
7
+ data.tar.gz: 6d027c76c43e84f8dab52bc04236568045c1dff962a58b0df029693b35cf535f03b2d5e4445993964bfcabe3d9093ff6465ba3ebfb399e661cc187388178b059
data/.travis.yml CHANGED
@@ -1,15 +1,28 @@
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.16.0
11
21
  - jruby-head
12
22
  - ruby-head
23
+ - truffleruby-head
24
+ jdk:
25
+ - openjdk8
13
26
  notifications:
14
27
  email:
15
28
  on_success: change
data/CHANGELOG.md CHANGED
@@ -1,31 +1,79 @@
1
1
  Changelog
2
2
  =========
3
3
 
4
+ v4.9.0 (2021-05-11)
5
+ -------------------
6
+
7
+ Features:
8
+ * Add SymbolOption [#263](https://github.com/leejarvis/slop/pull/263)
9
+
10
+ Bug fixes:
11
+ * Use `+=` over `<<` to handle frozen string literals. [255](https://github.com/leejarvis/slop/pull/255)
12
+
13
+ v4.8.2 (2020-07-10)
14
+ -------------------
15
+
16
+ Bug fixes:
17
+ * Fix bug where separator position was messed up if using `help: false`
18
+ [#253](https://github.com/leejarvis/slop/issues/253)
19
+
20
+ v4.8.1 (2020-03-31)
21
+ -------------------
22
+
23
+ Bug fixes:
24
+ * Fix keyword argument warning. [#251](https://github.com/leejarvis/slop/pull/251)
25
+
26
+
27
+ v4.8.0 (2020-01-17)
28
+ -------------------
29
+
30
+ Features:
31
+ * Add support for prefixing integer values with `+` character
32
+ [#243](https://github.com/leejarvis/slop/pull/243) (Juha Ylitalo)
33
+ * Add support for parsing floats with scientific notation
34
+ [#250](https://github.com/leejarvis/slop/pull/250) (Hansuk Hong)
35
+
36
+ Maintenance:
37
+ * Add 2.7.0 to CI and fix warnings
38
+ [#248](https://github.com/leejarvis/slop/pull/248) (Juha Ylitalo, Andrew Kane)
39
+
40
+ v4.7.0 (2019-06-29)
41
+ -------------------
42
+
43
+ Features:
44
+ * 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))
45
+ * 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))
46
+
47
+ Bug fixes
48
+ * Ensure non-string option types have their flags consumed properly [#241] (Sutou Kouhei)
49
+
50
+
4
51
  v4.6.2 (2018-03-12)
52
+ -------------------
5
53
 
6
54
  Bug fixes/Enhancements
7
- * Fix equals character (=) being parsed incorrectly in some cases. #226
55
+ * Fix equals character (=) being parsed incorrectly in some cases. [#226](https://github.com/leejarvis/slop/issues/226)
8
56
 
9
57
  v4.6.1 (2017-11-20)
10
58
  -------------------
11
59
 
12
60
  Bug fixes/Enhancements
13
- * Fix separator so it doesn't mutate user data. #223 (Marc-André Lafortune)
61
+ * Fix separator so it doesn't mutate user data. [#223](https://github.com/leejarvis/slop/issues/223) (Marc-André Lafortune)
14
62
  * Add additional tests for `Options#separator` and fix issue where
15
- the last separator was ignored. #222
63
+ the last separator was ignored. [#222](https://github.com/leejarvis/slop/issues/222)
16
64
 
17
65
  v4.6.0 (2017-10-06)
18
66
  -------------------
19
67
 
20
68
  Features
21
- * Add support for required options. #218 (William Woodruff)
69
+ * Add support for required options. [#218](https://github.com/leejarvis/slop/issues/218) (William Woodruff)
22
70
 
23
71
  v4.5.0 (2017-05-22)
24
72
  -------------------
25
73
 
26
74
  Features:
27
75
  * Added config option to avoid translating flags-with-dashes into
28
- underscores. #206 (@lbriais)
76
+ underscores. [#206](https://github.com/leejarvis/slop/issues/206) (@lbriais)
29
77
 
30
78
  v4.4.3 (2017-05-02)
31
79
  -------------------
@@ -37,73 +85,73 @@ v4.4.2 (2017-04-29)
37
85
  -------------------
38
86
 
39
87
  Bug fixes:
40
- * Fix support for parsing -x5 or -nfoo. #199
41
- * Fix removing arguments after `--`. #194
88
+ * Fix support for parsing -x5 or -nfoo. [#199](https://github.com/leejarvis/slop/issues/199)
89
+ * Fix removing arguments after `--`. [#194](https://github.com/leejarvis/slop/issues/194)
42
90
 
43
91
  v4.4.1 (2016-08-21)
44
92
  -------------------
45
93
 
46
94
  Bug fixes:
47
- * Handle bad constant names in `Slop.option_defined?`. #198
95
+ * Handle bad constant names in `Slop.option_defined?`. [#198](https://github.com/leejarvis/slop/issues/198)
48
96
  (Ellen Marie Dash)
49
97
 
50
98
  v4.4.0 (2016-08-15)
51
99
  -------------------
52
100
 
53
101
  Features
54
- * Support parsing arguments prefixed with dashes. #192 (Andrew Clemons)
102
+ * Support parsing arguments prefixed with dashes. [#192](https://github.com/leejarvis/slop/issues/192) (Andrew Clemons)
55
103
 
56
104
  Bug fixes:
57
- * Retain sort order inside tail sort. #193 (Caio Chassot)
105
+ * Retain sort order inside tail sort. [#193](https://github.com/leejarvis/slop/issues/193) (Caio Chassot)
58
106
 
59
107
  v4.3.0 (2016-03-19)
60
108
  -------------------
61
109
 
62
110
  Features
63
- * Allow disabling array delimiter. #189 (Mike Pastore)
64
- * Allow passing custom banner as config. #191 (Philip Rees)
111
+ * Allow disabling array delimiter. [#189](https://github.com/leejarvis/slop/issues/189) (Mike Pastore)
112
+ * Allow passing custom banner as config. [#191](https://github.com/leejarvis/slop/issues/191) (Philip Rees)
65
113
 
66
114
  v4.2.1 (2015-11-25)
67
115
  -------------------
68
116
 
69
117
  Features:
70
- * Better handling of option names with multiple words. #169 (Tim Rogers)
118
+ * Better handling of option names with multiple words. [#169](https://github.com/leejarvis/slop/issues/169) (Tim Rogers)
71
119
 
72
120
  Minor enhancements:
73
- * add ARGF notes to Arguments (README). #173 (Rick Hull)
121
+ * add ARGF notes to Arguments (README). [#173](https://github.com/leejarvis/slop/issues/173) (Rick Hull)
74
122
 
75
123
  Bug fixes:
76
- * Fix arguments removed with option arguments. #182 (Naoki Mizuno)
124
+ * Fix arguments removed with option arguments. [#182](https://github.com/leejarvis/slop/issues/182) (Naoki Mizuno)
77
125
  * Fix bug where true is passed to BoolOption block regardless
78
- of --no- prefix. #184 (Ben Brady)
79
- * only raise MissingArgument if not `default_value`. #163 (Ben Brady)
126
+ of --no- prefix. [#184](https://github.com/leejarvis/slop/issues/184) (Ben Brady)
127
+ * only raise MissingArgument if not `default_value`. [#163](https://github.com/leejarvis/slop/issues/163) (Ben Brady)
80
128
 
81
129
  v4.2.0 (2015-04-18)
82
130
  -------------------
83
131
 
84
132
  Features:
85
- * Support for Regexp option type #167 (Laurent Arnoud)
133
+ * Support for Regexp option type [#167](https://github.com/leejarvis/slop/issues/167) (Laurent Arnoud)
86
134
  * Support prefixed `--no-` for explicitly setting boolean options
87
- to `false` #168
88
- * Better handling of flags with multiple words #169 (Tim Rogers)
135
+ to `false` [#168](https://github.com/leejarvis/slop/issues/168)
136
+ * Better handling of flags with multiple words [#169](https://github.com/leejarvis/slop/issues/169) (Tim Rogers)
89
137
 
90
138
  v4.1.0 (2015-04-18)
91
139
  -------------------
92
140
 
93
141
  Features:
94
- * Support for FloatOption #156 (Rick Hull)
142
+ * Support for FloatOption [#156](https://github.com/leejarvis/slop/issues/156) (Rick Hull)
95
143
  * Support for `limit` config to ArrayOption.
96
144
  * Support for `tail` config to add options to the bottom of
97
145
  the help text.
98
- * Add explicit setter (#[]=) to Result class. #162
146
+ * Add explicit setter (#[]=) to Result class. [#162](https://github.com/leejarvis/slop/issues/162)
99
147
  * Implement flag gettings for UnknownOption and MissingArgument
100
- error classes. #165 (sigurdsvela)
148
+ error classes. [#165](https://github.com/leejarvis/slop/issues/165) (sigurdsvela)
101
149
 
102
150
  Minor enhancements:
103
151
  * Reset parser every time `parse` is called.
104
152
 
105
153
  Bug fixes:
106
- * Remove "--" from unprocessed arguments #157 (David Rodríguez).
154
+ * Remove "--" from unprocessed arguments [#157](https://github.com/leejarvis/slop/issues/157) (David Rodríguez).
107
155
 
108
156
  v4.0.0 (2014-12-27)
109
157
  -------------------
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
@@ -21,6 +18,7 @@ opts = Slop.parse do |o|
21
18
  o.string '-h', '--host', 'a hostname'
22
19
  o.integer '--port', 'custom port', default: 80
23
20
  o.string '-l', '--login', required: true
21
+ o.symbol '-m', '--method', default: :get
24
22
  o.bool '-v', '--verbose', 'enable verbose mode'
25
23
  o.bool '-q', '--quiet', 'suppress output (quiet mode)'
26
24
  o.bool '-c', '--check-ssl-certificate', 'check SSL certificate for host'
@@ -30,15 +28,16 @@ opts = Slop.parse do |o|
30
28
  end
31
29
  end
32
30
 
33
- ARGV #=> -v --login alice --host 192.168.0.1 --check-ssl-certificate
31
+ ARGV #=> -v --login alice --host 192.168.0.1 -m post --check-ssl-certificate
34
32
 
35
33
  opts[:host] #=> 192.168.0.1
36
34
  opts[:login] #=> alice
35
+ opts[:method] #=> :post
37
36
  opts.verbose? #=> true
38
37
  opts.quiet? #=> false
39
38
  opts.check_ssl_certificate? #=> true
40
39
 
41
- opts.to_hash #=> { host: "192.168.0.1", login: "alice", port: 80, verbose: true, quiet: false, check_ssl_certificate: true }
40
+ opts.to_hash #=> { host: "192.168.0.1", port: 80, login: "alice", method: :post, verbose: true, quiet: false, check_ssl_certificate: true }
42
41
  ```
43
42
 
44
43
  Note that the block we've added to the `--version` flag will be executed
@@ -59,6 +58,7 @@ o.integer #=> Slop::IntegerOption, expects an argument, aliased to IntOption
59
58
  o.float #=> Slop::FloatOption, expects an argument
60
59
  o.array #=> Slop::ArrayOption, expects an argument
61
60
  o.regexp #=> Slop::RegexpOption, expects an argument
61
+ o.symbol #=> Slop::SymbolOption, expects an argument
62
62
  o.null #=> Slop::NullOption, no argument and ignored from `to_hash`
63
63
  o.on #=> alias for o.null
64
64
  ```
@@ -275,89 +275,4 @@ end
275
275
  Commands
276
276
  --------
277
277
 
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
- ```
278
+ Slop not longer has built in support for git-style subcommands.
data/lib/slop.rb CHANGED
@@ -6,7 +6,7 @@ require 'slop/types'
6
6
  require 'slop/error'
7
7
 
8
8
  module Slop
9
- VERSION = '4.6.2'
9
+ VERSION = '4.9.0'
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:
data/lib/slop/error.rb CHANGED
@@ -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
 
data/lib/slop/options.rb CHANGED
@@ -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,17 +102,17 @@ 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
- str << "#{sep}\n"
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]
115
- str << "#{sep}\n"
115
+ str += "#{sep}\n"
116
116
  end
117
117
 
118
118
  str
data/lib/slop/parser.rb CHANGED
@@ -52,7 +52,6 @@ module Slop
52
52
 
53
53
  # support `foo=bar`
54
54
  orig_flag = flag.dup
55
- orig_arg = arg
56
55
  if match = flag.match(/([^=]+)=([^=]+)/)
57
56
  flag, arg = match.captures
58
57
  end
@@ -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)
data/lib/slop/result.rb CHANGED
@@ -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
data/lib/slop/types.rb CHANGED
@@ -6,6 +6,13 @@ module Slop
6
6
  end
7
7
  end
8
8
 
9
+ # Cast the option argument to a symbol.
10
+ class SymbolOption < Option
11
+ def call(value)
12
+ value.to_sym
13
+ end
14
+ end
15
+
9
16
  # Cast the option argument to true or false.
10
17
  # Override default_value to default to false instead of nil.
11
18
  # This option type does not expect an argument. However, the API
@@ -44,16 +51,17 @@ module Slop
44
51
  # Cast the option argument to an Integer.
45
52
  class IntegerOption < Option
46
53
  def call(value)
47
- value =~ /\A-?\d+\z/ && value.to_i
54
+ value =~ /\A[+-]?\d+\z/ && value.to_i
48
55
  end
49
56
  end
50
57
  IntOption = IntegerOption
51
58
 
52
59
  # Cast the option argument to a Float.
53
60
  class FloatOption < Option
61
+ FLOAT_STRING_REGEXP = /\A[+-]?(?:0|[1-9]\d*)(?:\.\d*)?(?:[eE][+-]?\d+)?\z/.freeze
62
+
54
63
  def call(value)
55
- # TODO: scientific notation, etc.
56
- value =~ /\A-?\d*\.*\d+\z/ && value.to_f
64
+ value =~ FLOAT_STRING_REGEXP && value.to_f
57
65
  end
58
66
  end
59
67
 
data/test/option_test.rb CHANGED
@@ -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
data/test/options_test.rb CHANGED
@@ -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
data/test/parser_test.rb CHANGED
@@ -35,6 +35,12 @@ describe Slop::Parser do
35
35
  assert_equal "--sometext", @result[:text]
36
36
  end
37
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
+
38
44
  it "parses negative integer" do
39
45
  @options.integer "-p", "--port"
40
46
  @result.parser.parse %w(--name=bob --port -123)
data/test/result_test.rb CHANGED
@@ -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"]
data/test/slop_test.rb CHANGED
@@ -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")
data/test/types_test.rb CHANGED
@@ -1,5 +1,33 @@
1
1
  require 'test_helper'
2
2
 
3
+ describe Slop::StringOption do
4
+ before do
5
+ @options = Slop::Options.new
6
+ @age = @options.string "--name"
7
+ @minus = @options.string "--zipcode"
8
+ @result = @options.parse %w(--name Foo --zipcode 12345)
9
+ end
10
+
11
+ it "returns the value as a string" do
12
+ assert_equal "Foo", @result[:name]
13
+ assert_equal "12345", @result[:zipcode]
14
+ end
15
+ end
16
+
17
+ describe Slop::SymbolOption do
18
+ before do
19
+ @options = Slop::Options.new
20
+ @age = @options.symbol "--name"
21
+ @minus = @options.symbol "--zipcode"
22
+ @result = @options.parse %w(--name Foo --zipcode 12345)
23
+ end
24
+
25
+ it "returns the value as a symbol" do
26
+ assert_equal :Foo, @result[:name]
27
+ assert_equal :'12345', @result[:zipcode]
28
+ end
29
+ end
30
+
3
31
  describe Slop::BoolOption do
4
32
  before do
5
33
  @options = Slop::Options.new
@@ -32,11 +60,15 @@ describe Slop::IntegerOption do
32
60
  before do
33
61
  @options = Slop::Options.new
34
62
  @age = @options.integer "--age"
35
- @result = @options.parse %w(--age 20)
63
+ @minus = @options.integer "--minus"
64
+ @plus = @options.integer "--plus"
65
+ @result = @options.parse %w(--age 20 --minus -10 --plus +30)
36
66
  end
37
67
 
38
68
  it "returns the value as an integer" do
39
69
  assert_equal 20, @result[:age]
70
+ assert_equal (-10), @result[:minus]
71
+ assert_equal 30, @result[:plus]
40
72
  end
41
73
 
42
74
  it "returns nil for non-numbers by default" do
@@ -50,11 +82,37 @@ describe Slop::FloatOption do
50
82
  @options = Slop::Options.new
51
83
  @apr = @options.float "--apr"
52
84
  @apr_value = 2.9
53
- @result = @options.parse %W(--apr #{@apr_value})
85
+ @minus = @options.float "--minus"
86
+ @plus = @options.float "--plus"
87
+ @scientific_notation = @options.float "--scientific-notation"
88
+ @scientific_notation_value = 4e21
89
+ @result = @options.parse %W(--apr #{@apr_value} --minus -6.1 --plus +9.4 --scientific-notation #{@scientific_notation_value})
54
90
  end
55
91
 
56
92
  it "returns the value as a float" do
57
93
  assert_equal @apr_value, @result[:apr]
94
+ assert_equal (-6.1), @result[:minus]
95
+ assert_equal 9.4, @result[:plus]
96
+ end
97
+
98
+ it "parses scientific notations" do
99
+ assert_equal @scientific_notation_value, @result[:scientific_notation]
100
+
101
+ @scientific_notation_value = 4E21
102
+ @result = @options.parse %W(--scientific-notation #{@scientific_notation_value})
103
+ assert_equal @scientific_notation_value, @result[:scientific_notation]
104
+
105
+ @scientific_notation_value = 4.0e21
106
+ @result = @options.parse %W(--scientific-notation #{@scientific_notation_value})
107
+ assert_equal @scientific_notation_value, @result[:scientific_notation]
108
+
109
+ @scientific_notation_value = -4e21
110
+ @result = @options.parse %W(--scientific-notation #{@scientific_notation_value})
111
+ assert_equal @scientific_notation_value, @result[:scientific_notation]
112
+
113
+ @scientific_notation_value = 4e-21
114
+ @result = @options.parse %W(--scientific-notation #{@scientific_notation_value})
115
+ assert_equal @scientific_notation_value, @result[:scientific_notation]
58
116
  end
59
117
 
60
118
  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.2
4
+ version: 4.9.0
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: 2018-03-12 00:00:00.000000000 Z
11
+ date: 2021-05-11 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
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: