choice 0.1.2 → 0.1.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,6 @@
1
+ 0.1.3:
2
+ - Added args_of method to retrieve the arguments of an option
3
+
1
4
  0.1.2:
2
5
  - Made validate directive accept block and validate against its boolean value.
3
6
  - Created shorthand format for defining options directly with a hash.
data/lib/choice.rb CHANGED
@@ -111,6 +111,22 @@ module Choice
111
111
  @@args
112
112
  end
113
113
 
114
+ # Returns the arguments that follow an argument
115
+ def args_of(opt)
116
+ args_of_opt = []
117
+
118
+ # Return an array of the arguments between opt and the next option,
119
+ # which all start with "-"
120
+ @@args.slice(@@args.index(opt)+1, @@args.length).select do |arg|
121
+ if arg[0].chr != "-"
122
+ args_of_opt << arg
123
+ else
124
+ break
125
+ end
126
+ end
127
+ args_of_opt
128
+ end
129
+
114
130
  # You can choose to not kill the script after the help screen is printed.
115
131
  def dont_exit_on_help=(val) #:nodoc:
116
132
  @@exit = true
data/lib/choice/parser.rb CHANGED
@@ -47,13 +47,8 @@ module Choice
47
47
  # Set the local hashes if the value exists on this option object.
48
48
  params.each { |param| hashes["#{param}s"][name] = obj[param] if obj[param] }
49
49
 
50
- # If there is a validate statement, save it as a regex.
51
- # If it's present but can't pull off a to_s (wtf?), raise an error.
52
- validators[name] = case obj['validate']
53
- when Proc then obj['validate']
54
- when Regexp, String then Regexp.new(obj['validate'].to_s)
55
- else raise ValidateExpectsRegexpOrBlock
56
- end if obj['validate']
50
+ # If there is a validate statement, make it a regex or proc.
51
+ validators[name] = make_validation(obj['validate']) if obj['validate']
57
52
 
58
53
  # Parse the long option. If it contains a =, figure out if the
59
54
  # argument is required or optional. Optional arguments are formed
@@ -208,6 +203,17 @@ module Choice
208
203
  array
209
204
  end
210
205
 
206
+ def make_validation(validation)
207
+ case validation
208
+ when Proc then
209
+ validation
210
+ when Regexp, String then
211
+ Regexp.new(validation.to_s)
212
+ else
213
+ raise ValidateExpectsRegexpOrBlock
214
+ end
215
+ end
216
+
211
217
  # All the possible exceptions this module can raise.
212
218
  class ParseError < Exception; end
213
219
  class HashExpectedForOption < Exception; end
@@ -2,7 +2,7 @@ module Choice
2
2
  module Version #:nodoc:
3
3
  MAJOR = 0
4
4
  MINOR = 1
5
- TINY = 2
5
+ TINY = 3
6
6
  STRING = [MAJOR, MINOR, TINY] * '.'
7
7
  end
8
8
  end
data/test/test_choice.rb CHANGED
@@ -180,4 +180,52 @@ HELP
180
180
  assert_equal ["Tell me about yourself?", ""], Choice.header
181
181
  assert_equal ["", "--help This message"], Choice.footer
182
182
  end
183
+
184
+ def test_args_of
185
+ suits = %w[clubs diamonds spades hearts]
186
+ stringed_numerics = (1..13).to_a.map { |a| a.to_s }
187
+ valid_cards = stringed_numerics + %w[jack queen king ace]
188
+ cards = {}
189
+ stringed_numerics.each { |n| cards[n] = n }
190
+ cards.merge!('1' => 'ace', '11' => 'jack', '12' => 'queen', '13' => 'king')
191
+
192
+ Choice.options do
193
+ header "Gambling is fun again! Pick a card and a suit (or two), then see if you win!"
194
+ header ""
195
+ header "Options:"
196
+
197
+ option :suit, :required => true do
198
+ short '-s'
199
+ long '--suit *SUITS'
200
+ desc "The suit you wish to choose. Required. You can pass in more than one, even."
201
+ desc " Valid suits: #{suits * ' '}"
202
+ valid suits
203
+ end
204
+
205
+ separator ''
206
+
207
+ option :card, :required => true do
208
+ short '-c'
209
+ long '--card CARD'
210
+ desc "The card you wish to gamble on. Required. Only one, please."
211
+ desc " Valid cards: 1 - 13, jack, queen, king, ace"
212
+ valid valid_cards
213
+ cast String
214
+ end
215
+
216
+ #cheat! to test --option=
217
+ option :autowin do
218
+ short '-a'
219
+ long '--autowin=PLAYER'
220
+ desc 'The person who should automatically win every time'
221
+ desc 'Beware: raises the suspitions of other players'
222
+ end
223
+ end
224
+
225
+ args = ["-c", "king", "--suit", "clubs", "diamonds", "spades", "hearts", "--autowin", "Grant"]
226
+ Choice.args = args
227
+ assert_equal ["king"], Choice.args_of("-c")
228
+ assert_equal ["clubs", "diamonds", "spades", "hearts"], Choice.args_of("--suit")
229
+ assert_equal ["Grant"], Choice.args_of("--autowin")
230
+ end
183
231
  end
metadata CHANGED
@@ -1,44 +1,37 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.9.0
3
- specification_version: 1
4
2
  name: choice
5
3
  version: !ruby/object:Gem::Version
6
- version: 0.1.2
7
- date: 2006-11-05 00:00:00 -08:00
8
- summary: Choice is a command line option parser.
9
- require_paths:
10
- - lib
11
- email: chris@ozmm.org
12
- homepage: http://choice.rubyforge.org/
13
- rubyforge_project:
14
- description: Choice is a simple little gem for easily defining and parsing command line options with a friendly DSL.
15
- autorequire: choice
16
- default_executable:
17
- bindir: bin
18
- has_rdoc: false
19
- required_ruby_version: !ruby/object:Gem::Version::Requirement
20
- requirements:
21
- - - ">"
22
- - !ruby/object:Gem::Version
23
- version: 0.0.0
24
- version:
4
+ version: 0.1.3
25
5
  platform: ruby
26
- signing_key:
27
- cert_chain:
28
- post_install_message:
29
6
  authors:
30
7
  - Chris Wanstrath
8
+ autorequire: choice
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-02-19 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies: []
15
+
16
+ description: Choice is a simple little gem for easily defining and parsing command line options with a friendly DSL.
17
+ email: chris@ozmm.org
18
+ executables: []
19
+
20
+ extensions: []
21
+
22
+ extra_rdoc_files: []
23
+
31
24
  files:
32
25
  - README
33
26
  - CHANGELOG
34
27
  - LICENSE
35
28
  - lib/choice
36
- - lib/choice.rb
37
29
  - lib/choice/lazyhash.rb
38
30
  - lib/choice/option.rb
39
31
  - lib/choice/parser.rb
40
32
  - lib/choice/version.rb
41
33
  - lib/choice/writer.rb
34
+ - lib/choice.rb
42
35
  - test/test_choice.rb
43
36
  - test/test_lazyhash.rb
44
37
  - test/test_option.rb
@@ -46,17 +39,31 @@ files:
46
39
  - test/test_writer.rb
47
40
  - examples/ftpd.rb
48
41
  - examples/gamble.rb
49
- test_files: []
50
-
42
+ has_rdoc: false
43
+ homepage: http://choice.rubyforge.org/
44
+ post_install_message:
51
45
  rdoc_options: []
52
46
 
53
- extra_rdoc_files: []
54
-
55
- executables: []
56
-
57
- extensions: []
58
-
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: "0"
54
+ version:
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ requirements:
57
+ - - ">="
58
+ - !ruby/object:Gem::Version
59
+ version: "0"
60
+ version:
59
61
  requirements: []
60
62
 
61
- dependencies: []
63
+ rubyforge_project:
64
+ rubygems_version: 1.3.1
65
+ signing_key:
66
+ specification_version: 2
67
+ summary: Choice is a command line option parser.
68
+ test_files: []
62
69