slop 4.5.0 → 4.6.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
2
  SHA1:
3
- metadata.gz: 63095938a88e1994b174a29d9c1bcca25fa84562
4
- data.tar.gz: 4df7ece6ed84e45245081a7b6617372cd386385e
3
+ metadata.gz: 66456d3c9b1836976bf1db77744397d30ed6eddd
4
+ data.tar.gz: 615f7a217ab9668b2eab4234230f67b7bad76ffc
5
5
  SHA512:
6
- metadata.gz: abd18392e43a5413199e6b7ab482afdb4c5782c5eeb65c298d916279df060df614b7875e3ad836a803064d5aa0c04c093eb682753397ddb6b65bfa26550be9d6
7
- data.tar.gz: 0f03c20ff5e6d240d57586a133cc531a6a68ac2fcf4f4aea4ac931726cdf7420f92ea736fadec0f0c715e91c395ccc1ca73f3e683da42b89f5544f5afe3d02eb
6
+ metadata.gz: 5976dae0692f77d98ef26995737385da682592ddd4631209543f1604130c607f6f584a787a036595d668914245b17cc0141050e8fe36843f288bda93f5bcd74a
7
+ data.tar.gz: a5ed147ed93952f3d3d3e38abd8ece30bb6131e43d085281c91c0c12bd4490473fc0f3169e6f82cc6b8a1aa494d1d77188eeba63ec622794614f586bcf1fccbe
@@ -7,7 +7,7 @@ rvm:
7
7
  - 2.2
8
8
  - 2.3.4
9
9
  - 2.4.1
10
- - jruby-9.1.8.0
10
+ - jruby-9.1.13.0
11
11
  - jruby-head
12
12
  - ruby-head
13
13
  notifications:
@@ -1,6 +1,12 @@
1
1
  Changelog
2
2
  =========
3
3
 
4
+ v4.6.0 (2017-10-06)
5
+ -------------------
6
+
7
+ Features
8
+ * Add support for required options. #218 (William Woodruff)
9
+
4
10
  v4.5.0 (2017-05-22)
5
11
  -------------------
6
12
 
data/README.md CHANGED
@@ -20,6 +20,7 @@ Usage
20
20
  opts = Slop.parse do |o|
21
21
  o.string '-h', '--host', 'a hostname'
22
22
  o.integer '--port', 'custom port', default: 80
23
+ o.string '-l', '--login', required: true
23
24
  o.bool '-v', '--verbose', 'enable verbose mode'
24
25
  o.bool '-q', '--quiet', 'suppress output (quiet mode)'
25
26
  o.bool '-c', '--check-ssl-certificate', 'check SSL certificate for host'
@@ -39,6 +40,12 @@ opts.check_ssl_certificate? #=> true
39
40
  opts.to_hash #=> { host: "192.168.0.1", port: 80, verbose: true, quiet: false, check_ssl_certificate: true }
40
41
  ```
41
42
 
43
+ Note that the block we've added to the `--version` flag will be executed
44
+ during parse time. Therefore these blocks should be reserved
45
+ for immediately reacting to the presence of a flag. If you want to
46
+ access other options or mutate values, check out the "Custom option types"
47
+ section below and implement the `#finish` method.
48
+
42
49
  Option types
43
50
  ------------
44
51
 
@@ -191,6 +198,7 @@ Slop will raise errors for the following:
191
198
 
192
199
  * An option used without an argument when it expects one: `Slop::MissingArgument`
193
200
  * An option used that Slop doesn't know about: `Slop::UnknownOption`
201
+ * An option marked as `required` when not provided: `Slop::MissingRequiredOption`
194
202
 
195
203
  These errors inherit from `Slop::Error`, so you can rescue them all.
196
204
  Alternatively you can suppress these errors with the `suppress_errors` config
@@ -6,7 +6,7 @@ require 'slop/types'
6
6
  require 'slop/error'
7
7
 
8
8
  module Slop
9
- VERSION = '4.5.0'
9
+ VERSION = '4.6.0'
10
10
 
11
11
  # Parse an array of options (defaults to ARGV). Accepts an
12
12
  # optional hash of configuration options and block.
@@ -32,4 +32,9 @@ module Slop
32
32
  @flag = flag
33
33
  end
34
34
  end
35
+
36
+ # Raised when a required option is *not* parsed.
37
+ # Suppress with the `suppress_errors` config option.
38
+ class MissingRequiredOption < Error
39
+ end
35
40
  end
@@ -4,6 +4,7 @@ module Slop
4
4
  help: true,
5
5
  tail: false,
6
6
  underscore_flags: true,
7
+ required: false,
7
8
  }
8
9
 
9
10
  # An Array of flags this option matches.
@@ -101,6 +102,11 @@ module Slop
101
102
  config[:suppress_errors]
102
103
  end
103
104
 
105
+ # Returns true if an exception should be raised when this option isn't supplied.
106
+ def required?
107
+ config[:required]
108
+ end
109
+
104
110
  # Returns all flags joined by a comma. Used by the help string.
105
111
  def flag
106
112
  flags.join(", ")
@@ -80,6 +80,15 @@ module Slop
80
80
 
81
81
  @arguments += ignored_args
82
82
 
83
+ if !suppress_errors?
84
+ unused_options.each do |o|
85
+ if o.config[:required]
86
+ pretty_flags = o.flags.map { |f| "`#{f}'" }.join(", ")
87
+ raise MissingRequiredOption, "missing required option #{pretty_flags}"
88
+ end
89
+ end
90
+ end
91
+
83
92
  Result.new(self).tap do |result|
84
93
  used_options.each { |o| o.finish(result) }
85
94
  end
@@ -148,6 +157,7 @@ module Slop
148
157
  def partition(strings)
149
158
  if strings.include?("--")
150
159
  partition_idx = strings.index("--")
160
+ return [[], strings[1..-1]] if partition_idx.zero?
151
161
  [strings[0..partition_idx-1], strings[partition_idx+1..-1]]
152
162
  else
153
163
  [strings, []]
@@ -21,6 +21,12 @@ describe Slop::MissingArgument do
21
21
  opts.string "-n", "--name"
22
22
  opts.parse %w(--name)
23
23
  end
24
+
25
+ it "does not raise if '--' appears as the first argument" do
26
+ opts = Slop::Options.new
27
+ opts.string "-n", "--name"
28
+ opts.parse %w(-- --name)
29
+ end
24
30
  end
25
31
 
26
32
  describe Slop::UnknownOption do
@@ -43,3 +49,17 @@ describe Slop::UnknownOption do
43
49
  opts.parse %w(--foo)
44
50
  end
45
51
  end
52
+
53
+ describe Slop::MissingRequiredOption do
54
+ it "raises when a required option is missing" do
55
+ opts = Slop::Options.new
56
+ opts.string "-n", "--name", required: true
57
+ assert_raises(Slop::MissingRequiredOption) { opts.parse [] }
58
+ end
59
+
60
+ it "does not raise when errors are suppressed" do
61
+ opts = Slop::Options.new(suppress_errors: true)
62
+ opts.string "-n", "--name", required: true
63
+ opts.parse []
64
+ end
65
+ end
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.5.0
4
+ version: 4.6.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lee Jarvis
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2017-05-22 00:00:00.000000000 Z
11
+ date: 2017-10-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -87,7 +87,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
87
87
  version: '0'
88
88
  requirements: []
89
89
  rubyforge_project:
90
- rubygems_version: 2.5.1
90
+ rubygems_version: 2.6.13
91
91
  signing_key:
92
92
  specification_version: 4
93
93
  summary: Simple Lightweight Option Parsing