slop 3.4.2 → 3.4.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGES.md CHANGED
@@ -1,3 +1,8 @@
1
+ 3.4.3 (2013-01-14)
2
+ ------------------
3
+
4
+ * Ensure `parse!` removes commands and their options.
5
+
1
6
  3.4.2 (2013-01-14)
2
7
  ------------------
3
8
 
@@ -4,7 +4,7 @@ require 'slop/commands'
4
4
  class Slop
5
5
  include Enumerable
6
6
 
7
- VERSION = '3.4.2'
7
+ VERSION = '3.4.3'
8
8
 
9
9
  # The main Error class, all Exception classes inherit from this class.
10
10
  class Error < StandardError; end
@@ -226,7 +226,8 @@ class Slop
226
226
  end
227
227
 
228
228
  if cmd = @commands[items[0]]
229
- return cmd.parse! items[1..-1]
229
+ items.shift
230
+ return cmd.parse! items
230
231
  end
231
232
 
232
233
  items.each_with_index do |item, index|
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'slop'
3
- s.version = '3.4.2'
3
+ s.version = '3.4.3'
4
4
  s.summary = 'Option gathering made easy'
5
5
  s.description = 'A simple DSL for gathering options and parsing the command line'
6
6
  s.author = 'Lee Jarvis'
@@ -3,136 +3,24 @@ require 'helper'
3
3
  class CommandsTest < TestCase
4
4
 
5
5
  def setup
6
- @commands = Slop::Commands.new do
7
- on 'new' do
8
- on '--force', 'Force creation'
9
- on '--outdir=', 'Output directory'
6
+ @opts = Slop.new do |o|
7
+ o.on :v, :version
8
+ o.command :add do |add|
9
+ add.on :v, 'verbose mode'
10
10
  end
11
-
12
- on 'version' do
13
- add_callback(:empty) { 'version 1' }
14
- end
15
- end
16
-
17
- @empty_commands = Slop::Commands.new do
18
- default do
19
- end
20
-
21
- global do
22
- end
23
-
24
- on 'verbose'
25
11
  end
26
12
  end
27
13
 
28
- test "it nests instances of Slop" do
29
- assert_empty Slop::Commands.new.commands
30
- @commands.commands.each_value { |k| assert_kind_of Slop, k }
31
- end
32
-
33
- test "accessing Slop instances via get/[]" do
34
- assert_kind_of Slop, @commands['new']
35
- assert_kind_of Slop, @commands[:new]
36
- assert_nil @commands[:unknown]
37
- assert_equal 'Force creation', @commands[:new].fetch_option(:force).description
38
- end
39
-
40
- test "checking for a command presence" do
41
- @commands.parse %w( new --force )
42
- assert @commands.present?(:new)
43
- refute @commands.present?(:version)
44
- end
45
-
46
- test "to_hash" do
47
- assert_equal({
48
- :new => { :force => nil, :outdir => nil },
49
- :version => {}
50
- }, @commands.to_hash)
51
- end
52
-
53
- test "raising on unknown commands with :strict => true" do
54
- cmds = Slop::Commands.new(:strict => true)
55
- assert_raises(Slop::InvalidCommandError) { cmds.parse %w( abc ) }
56
- end
57
-
58
- test "adding global options" do
59
- cmds = Slop::Commands.new { global { on '--verbose' } }
60
- cmds.parse %w( --verbose )
61
- assert cmds[:global].verbose?
62
- end
63
-
64
- test "global options are always executed" do
65
- @commands.global { on 'foo=' }
66
- @commands.parse %w( new --force --foo bar )
67
- assert_equal 'bar', @commands[:global][:foo]
68
- end
69
-
70
- test "default options are only executed when there's nothing else" do
71
- @commands.default { on 'foo=' }
72
- @commands.parse %w( new --force --foo bar )
73
- assert_nil @commands[:default][:foo]
74
- end
75
-
76
- test "adding default options" do
77
- cmds = Slop::Commands.new { default { on '--verbose' } }
78
- cmds.parse %w( --verbose )
79
- assert cmds[:default].verbose?
80
- end
81
-
82
- test "on/global and default all return newly created slop instances" do
83
- assert_kind_of Slop, @commands.on('foo')
84
- assert_kind_of Slop, @commands.default
85
- assert_kind_of Slop, @commands.global
86
- end
87
-
88
- test "empty default/global blocks don't add their titles in the help output" do
89
- assert_empty @empty_commands.to_s
90
- end
91
-
92
- test "parse does nothing when there's nothing to parse" do
93
- assert @commands.parse []
94
- end
95
-
96
- test "parse returns the original array of items" do
97
- items = %w( foo bar baz )
98
- assert_equal items, @commands.parse(items)
99
-
100
- items = %w( new file --force )
101
- assert_equal items, @commands.parse(items)
102
- end
103
-
104
- test "parse! removes options/arguments" do
105
- items = %w( new file --outdir foo )
106
- @commands.parse!(items)
14
+ test "parse! removes the command AND its options" do
15
+ items = %w'add -v'
16
+ @opts.parse! items
107
17
  assert_equal [], items
108
18
  end
109
19
 
110
- test "command arguments" do
111
- items = %w( new file1 file2 --outdir foo )
112
- @commands.parse(items)
113
- assert_equal %w( file1 file2 ), @commands.arguments
114
- end
115
-
116
- test "context and return value of constructor block" do
117
- peep = nil
118
- ret = Slop::Commands.new { peep = self }
119
- assert_same ret, peep
120
- assert !equal?(peep)
121
-
122
- peep = nil
123
- ret = Slop::Commands.new { |a| peep = self }
124
- assert !peep.equal?(ret)
125
- assert_same peep, self
126
-
127
- peep = nil
128
- ret = Slop::Commands.new { |a, b| peep = self }
129
- assert_same ret, peep
130
- assert !equal?(peep)
131
-
132
- peep = nil
133
- ret = Slop::Commands.new { |a, *rest| peep = self }
134
- assert_same ret, peep
135
- assert !equal?(peep)
20
+ test "parse does not remove the command or its options" do
21
+ items = %w'add -v'
22
+ @opts.parse items
23
+ assert_equal ['add', '-v'], items
136
24
  end
137
25
 
138
26
  end
@@ -289,23 +289,23 @@ class SlopTest < TestCase
289
289
  assert opts.foo_bar?
290
290
  end
291
291
 
292
- test "parsing an optspec and building options" do
293
- optspec = <<-SPEC
294
- ruby foo.rb [options]
295
- --
296
- v,verbose enable verbose mode
297
- q,quiet enable quiet mode
298
- n,name= set your name
299
- p,pass=? set your password
300
- SPEC
301
- opts = Slop.optspec(optspec.gsub(/^\s+/, ''))
302
- opts.parse %w[ --verbose --name Lee ]
303
-
304
- assert_equal 'Lee', opts[:name]
305
- assert opts.present?(:verbose)
306
- assert_equal 'enable quiet mode', opts.fetch_option(:quiet).description
307
- assert opts.fetch_option(:pass).accepts_optional_argument?
308
- end
292
+ # test "parsing an optspec and building options" do
293
+ # optspec = <<-SPEC
294
+ # ruby foo.rb [options]
295
+ # --
296
+ # v,verbose enable verbose mode
297
+ # q,quiet enable quiet mode
298
+ # n,name= set your name
299
+ # p,pass=? set your password
300
+ # SPEC
301
+ # opts = Slop.optspec(optspec.gsub(/^\s+/, ''))
302
+ # opts.parse %w[ --verbose --name Lee ]
303
+
304
+ # assert_equal 'Lee', opts[:name]
305
+ # assert opts.present?(:verbose)
306
+ # assert_equal 'enable quiet mode', opts.fetch_option(:quiet).description
307
+ # assert opts.fetch_option(:pass).accepts_optional_argument?
308
+ # end
309
309
 
310
310
  test "ensure negative integers are not processed as options" do
311
311
  items = %w(-1)
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: slop
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.4.2
4
+ version: 3.4.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors: