slop 4.1.0 → 4.2.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: 2621271dc1f31b8d8eac5c0f71169a921768b508
4
- data.tar.gz: 9f0bbcdf7a7adc210d7f6064e5f86545c2cd0a03
3
+ metadata.gz: 73d86e1d2fe70c0ce378fdf9e6c1ca771970b11b
4
+ data.tar.gz: 1da89cf463e52371680e66a913b09c215b1ac730
5
5
  SHA512:
6
- metadata.gz: 4748eb96ea76d2bbd0f7610cea52af99fbba605f4944924aba7e38223ee0b6e0298b898173ba6a5100a171e7b5c2aa6bfae4b636c64fda072bd9df6489333ad3
7
- data.tar.gz: 778e986eea0c1ce8cd712b71edc4f0311eddbcc4ca142dabfe84b15508959de917ccbee17f82968c35465af93f9f5709de07387dde508151b0c97ff2742626ea
6
+ metadata.gz: 3b4448b3fad94248ecdb67c8cf6af710c588a3da82a20e3d9286c0be123bdd97373cb187b7b0ed4da0ee5a1e6c9a80b1de7e52ec055e268482b56eacdcf4c0ef
7
+ data.tar.gz: b6ea3a312f1f8f5f7570b634d8f1ff42cee7e345067c3dbdaf037477c9b62e3e5c40d28a4515bf42a52bf3e506720b7b599dea25351d468001e4c0d17205ce87
@@ -1,6 +1,15 @@
1
1
  Changelog
2
2
  =========
3
3
 
4
+ v4.2.0 (2015-04-18)
5
+ -------------------
6
+
7
+ Features:
8
+ * Support for Regexp option type #167 (Laurent Arnoud)
9
+ * Support prefixed `--no-` for explicitly setting boolean options
10
+ to `false` #168
11
+ * Better handling of flags with multiple words #169 (Tim Rogers)
12
+
4
13
  v4.1.0 (2015-04-18)
5
14
  -------------------
6
15
 
data/README.md CHANGED
@@ -6,7 +6,7 @@ Slop is a simple option parser with an easy to remember syntax and friendly API.
6
6
  Version 4 of Slop is aimed at Ruby 2.0 or later. Please use
7
7
  [Version 3](https://github.com/leejarvis/slop/tree/v3) for Ruby 1.9 support.
8
8
 
9
- [![Build Status](https://travis-ci.org/leejarvis/slop.png?branch=master)](http://travis-ci.org/leejarvis/slop)
9
+ [![Build Status](https://travis-ci.org/leejarvis/slop.svg?branch=master)](http://travis-ci.org/leejarvis/slop)
10
10
 
11
11
  Installation
12
12
  ------------
@@ -22,19 +22,21 @@ opts = Slop.parse do |o|
22
22
  o.integer '--port', 'custom port', default: 80
23
23
  o.bool '-v', '--verbose', 'enable verbose mode'
24
24
  o.bool '-q', '--quiet', 'suppress output (quiet mode)'
25
+ o.bool '-c', '--check-ssl-certificate', 'check SSL certificate for host'
25
26
  o.on '--version', 'print the version' do
26
27
  puts Slop::VERSION
27
28
  exit
28
29
  end
29
30
  end
30
31
 
31
- ARGV #=> -v --host 192.168.0.1
32
+ ARGV #=> -v --host 192.168.0.1 --check-ssl-certificate
32
33
 
33
- opts[:host] #=> 192.168.0.1
34
- opts.verbose? #=> true
35
- opts.quiet? #=> false
34
+ opts[:host] #=> 192.168.0.1
35
+ opts.verbose? #=> true
36
+ opts.quiet? #=> false
37
+ opts.check_ssl_certificate? #=> true
36
38
 
37
- opts.to_hash #=> { host: "192.168.0.1", port: 80, verbose: true, quiet: false }
39
+ opts.to_hash #=> { host: "192.168.0.1", port: 80, verbose: true, quiet: false, check_ssl_certificate: true }
38
40
  ```
39
41
 
40
42
  Option types
@@ -48,6 +50,7 @@ o.bool #=> Slop::BoolOption, no argument, aliased to BooleanOption
48
50
  o.integer #=> Slop::IntegerOption, expects an argument, aliased to IntOption
49
51
  o.float #=> Slop::FloatOption, expects an argument
50
52
  o.array #=> Slop::ArrayOption, expects an argument
53
+ o.regexp #=> Slop::RegexpOption, expects an argument
51
54
  o.null #=> Slop::NullOption, no argument and ignored from `to_hash`
52
55
  o.on #=> alias for o.null
53
56
  ```
@@ -71,10 +74,10 @@ opts.int "-p", "--port", "a port", default: 80
71
74
  opts.separator ""
72
75
  opts.separator "Extra options:"
73
76
  opts.array "--files", "a list of files to import"
74
- opts.bool "-v", "--verbose", "enable verbose mode"
77
+ opts.bool "-v", "--verbose", "enable verbose mode", default: true
75
78
 
76
79
  parser = Slop::Parser.new(opts)
77
- result = parser.parse(["--hostname", "192.168.0.1"])
80
+ result = parser.parse(["--hostname", "192.168.0.1", "--no-verbose"])
78
81
 
79
82
  result.to_hash #=> { hostname: "192.168.0.1", port: 80,
80
83
  # files: [], verbose: false }
@@ -6,7 +6,7 @@ require 'slop/types'
6
6
  require 'slop/error'
7
7
 
8
8
  module Slop
9
- VERSION = '4.1.0'
9
+ VERSION = '4.2.0'
10
10
 
11
11
  # Parse an array of options (defaults to ARGV). Accepts an
12
12
  # optional hash of configuration options and block.
@@ -102,7 +102,7 @@ module Slop
102
102
 
103
103
  # Returns the last key as a symbol. Used in Options.to_hash.
104
104
  def key
105
- (config[:key] || flags.last.sub(/\A--?/, '')).to_sym
105
+ (config[:key] || flags.last.sub(/\A--?/, '')).tr("-", "_").to_sym
106
106
  end
107
107
 
108
108
  # Returns true if this option should be displayed in help text.
@@ -92,6 +92,8 @@ module Slop
92
92
  def try_process(flag, arg)
93
93
  if option = matching_option(flag)
94
94
  process(option, arg)
95
+ elsif flag.start_with?("--no-") && option = matching_option(flag.sub("no-", ""))
96
+ process(option, false)
95
97
  elsif flag =~ /\A-[^-]/ && flag.size > 2
96
98
  # try and process as a set of grouped short flags. drop(1) removes
97
99
  # the prefixed -, then we add them back to each flag separately.
@@ -33,7 +33,7 @@ module Slop
33
33
 
34
34
  # Returns an Option if it exists. Ignores any prefixed hyphens.
35
35
  def option(flag)
36
- cleaned = -> (f) { f.to_s.sub(/\A--?/, '') }
36
+ cleaned = -> (f) { f.to_s.sub(/\A--?/, '').tr('_', '-') }
37
37
  options.find do |o|
38
38
  o.flags.any? { |f| cleaned.(f) == cleaned.(flag) }
39
39
  end
@@ -8,12 +8,29 @@ module Slop
8
8
 
9
9
  # Cast the option argument to true or false.
10
10
  # Override default_value to default to false instead of nil.
11
- # This option type does not expect an argument.
11
+ # This option type does not expect an argument. However, the API
12
+ # supports value being passed. This is to ensure it can capture
13
+ # an explicit false value
12
14
  class BoolOption < Option
13
- def call(_value)
15
+ attr_accessor :explicit_value
16
+
17
+ def call(value)
18
+ self.explicit_value = value
14
19
  true
15
20
  end
16
21
 
22
+ def value
23
+ if force_false?
24
+ false
25
+ else
26
+ super
27
+ end
28
+ end
29
+
30
+ def force_false?
31
+ explicit_value == false
32
+ end
33
+
17
34
  def default_value
18
35
  config[:default] || false
19
36
  end
@@ -61,6 +78,13 @@ module Slop
61
78
  end
62
79
  end
63
80
 
81
+ # Cast the option argument to a Regexp.
82
+ class RegexpOption < Option
83
+ def call(value)
84
+ Regexp.new(value)
85
+ end
86
+ end
87
+
64
88
  # An option that discards the return value, inherits from Bool
65
89
  # since it does not expect an argument.
66
90
  class NullOption < BoolOption
@@ -17,6 +17,10 @@ describe Slop::Option do
17
17
  assert_equal :foo, option(%w(-f --foo), nil).key
18
18
  end
19
19
 
20
+ it "converts dashes to underscores to make multi-word options symbol-friendly" do
21
+ assert_equal :foo_bar, option(%w(-f --foo-bar), nil).key
22
+ end
23
+
20
24
  it "can be overridden" do
21
25
  assert_equal :bar, option(%w(-f --foo), nil, key: "bar").key
22
26
  end
@@ -12,16 +12,18 @@ end
12
12
 
13
13
  describe Slop::Result do
14
14
  before do
15
- @options = Slop::Options.new
16
- @verbose = @options.bool "-v", "--verbose"
17
- @name = @options.string "--name"
18
- @unused = @options.string "--unused"
19
- @result = @options.parse %w(foo -v --name lee argument)
15
+ @options = Slop::Options.new
16
+ @verbose = @options.bool "-v", "--verbose"
17
+ @name = @options.string "--name"
18
+ @unused = @options.string "--unused"
19
+ @long_option = @options.string "--long-option"
20
+ @result = @options.parse %w(foo -v --name lee --long-option bar argument)
20
21
  end
21
22
 
22
23
  it "increments option count" do
23
24
  # test this here so it's more "full stack"
24
25
  assert_equal 1, @verbose.count
26
+ assert_equal 1, @long_option.count
25
27
  @result.parser.parse %w(-v --verbose)
26
28
  assert_equal 2, @verbose.count
27
29
  end
@@ -51,6 +53,9 @@ describe Slop::Result do
51
53
  assert_equal "lee", @result["name"]
52
54
  assert_equal "lee", @result[:name]
53
55
  assert_equal "lee", @result["--name"]
56
+ assert_equal "bar", @result["long_option"]
57
+ assert_equal "bar", @result[:long_option]
58
+ assert_equal "bar", @result["--long-option"]
54
59
  end
55
60
  end
56
61
 
@@ -72,6 +77,7 @@ describe Slop::Result do
72
77
  it "checks if options have been used" do
73
78
  assert_equal true, @result.verbose?
74
79
  assert_equal false, @result.unused?
80
+ assert_equal true, @result.long_option?
75
81
  end
76
82
  end
77
83
 
@@ -79,6 +85,7 @@ describe Slop::Result do
79
85
  it "returns an option by flag" do
80
86
  assert_equal @verbose, @result.option("--verbose")
81
87
  assert_equal @verbose, @result.option("-v")
88
+ assert_equal @long_option, @result.option("--long-option")
82
89
  end
83
90
 
84
91
  it "ignores prefixed hyphens" do
@@ -93,7 +100,8 @@ describe Slop::Result do
93
100
 
94
101
  describe "#to_hash" do
95
102
  it "returns option keys and values" do
96
- assert_equal({ verbose: true, name: "lee", unused: nil }, @result.to_hash)
103
+ assert_equal({ verbose: true, name: "lee", unused: nil, long_option: "bar" },
104
+ @result.to_hash)
97
105
  end
98
106
  end
99
107
  end
@@ -2,10 +2,11 @@ require 'test_helper'
2
2
 
3
3
  describe Slop::BoolOption do
4
4
  before do
5
- @options = Slop::Options.new
6
- @age = @options.bool "--verbose"
7
- @age = @options.bool "--quiet"
8
- @result = @options.parse %w(--verbose)
5
+ @options = Slop::Options.new
6
+ @verbose = @options.bool "--verbose"
7
+ @quiet = @options.bool "--quiet"
8
+ @inversed = @options.bool "--inversed", default: true
9
+ @result = @options.parse %w(--verbose --no-inversed)
9
10
  end
10
11
 
11
12
  it "returns true if used" do
@@ -15,6 +16,10 @@ describe Slop::BoolOption do
15
16
  it "returns false if not used" do
16
17
  assert_equal false, @result[:quiet]
17
18
  end
19
+
20
+ it "can be inversed via --no- prefix" do
21
+ assert_equal false, @result[:inversed]
22
+ end
18
23
  end
19
24
 
20
25
  describe Slop::IntegerOption do
@@ -100,3 +105,16 @@ describe Slop::NullOption do
100
105
  assert_equal({}, @result.to_hash)
101
106
  end
102
107
  end
108
+
109
+ describe Slop::RegexpOption do
110
+ before do
111
+ @options = Slop::Options.new
112
+ @exclude = @options.regexp "--exclude"
113
+ @exclude_value = "redirect|news"
114
+ @result = @options.parse %W(--exclude #{@exclude_value})
115
+ end
116
+
117
+ it "returns the value as a Regexp" do
118
+ assert_equal Regexp.new(@exclude_value), @result[:exclude]
119
+ end
120
+ 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.1.0
4
+ version: 4.2.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: 2015-04-18 00:00:00.000000000 Z
11
+ date: 2015-06-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rake
@@ -98,3 +98,4 @@ test_files:
98
98
  - test/result_test.rb
99
99
  - test/test_helper.rb
100
100
  - test/types_test.rb
101
+ has_rdoc: