clive 0.1.1 → 0.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
data/README.md CHANGED
@@ -34,6 +34,26 @@ This creates a very simple interface which can have one switch, you can then use
34
34
 
35
35
  As we've seen above switches are created using #switch. You can provide as little information as you want. `switch(:v) {}` creates a switch that responds only to `-v`, or `switch(:verbose) {}` creates a switch that only responds to `--verbose`.
36
36
 
37
+ ### Boolean
38
+
39
+ Boolean switches allow you to accept arguments like `--no-verbose` and `--verbose`, and deal with both situations in the same block.
40
+
41
+ c = Clive.new do
42
+ boolean(:v, :verbose) {|i| p i}
43
+ end
44
+ c.parse(ARGV)
45
+
46
+ ####
47
+
48
+ my_file --verbose
49
+ #=> true
50
+ my_file -v
51
+ #=> true
52
+ my_file --no-verbose
53
+ #=> false
54
+
55
+ As you can see the true case can be triggered with the short or long form, the false case can be triggered by appending "no-" to the long form, and it can't be triggered with a short form.
56
+
37
57
  ### Flags
38
58
 
39
59
  Flags are like switches but also take an argument:
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.1.1
1
+ 0.2.0
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{clive}
8
- s.version = "0.1.1"
8
+ s.version = "0.2.0"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Joshua Hawxwell"]
@@ -25,6 +25,7 @@ Gem::Specification.new do |s|
25
25
  "VERSION",
26
26
  "clive.gemspec",
27
27
  "lib/clive.rb",
28
+ "lib/clive/booleans.rb",
28
29
  "lib/clive/commands.rb",
29
30
  "lib/clive/ext.rb",
30
31
  "lib/clive/flags.rb",
@@ -5,6 +5,7 @@ require 'clive/ext'
5
5
  require 'clive/switches'
6
6
  require 'clive/flags'
7
7
  require 'clive/commands'
8
+ require 'clive/booleans'
8
9
 
9
10
  # Clive is a simple dsl for creating command line interfaces
10
11
  #
@@ -43,3 +44,4 @@ class Clive
43
44
  end
44
45
 
45
46
  end
47
+
@@ -0,0 +1,35 @@
1
+ class Clive
2
+
3
+ # A switch which can be triggered with either --no-verbose or --verbose
4
+ # for example.
5
+ class Boolean < Switch
6
+ attr_accessor :truth
7
+
8
+ def initialize(short, long, desc, truth, &block)
9
+ @short = short
10
+ @long = long
11
+ @desc = desc
12
+ @truth = truth
13
+ @block = block
14
+ end
15
+
16
+ def run
17
+ @block.call(@truth)
18
+ end
19
+
20
+ # @return [String] summary for help or nil if +@truth = false+
21
+ def summary(width=30, prepend=5)
22
+ return nil unless @truth
23
+ a = ""
24
+ a << "-#{@short}" if @short
25
+ a << ", " if @short && @long
26
+ a << "--[no-]#{@long}" if @long
27
+ b = @desc
28
+ s, p = '', ''
29
+ (0..width-a.length).each {s << ' '}
30
+ (0..prepend).each {p << ' '}
31
+ "#{p}#{a}#{s}#{b}"
32
+ end
33
+
34
+ end
35
+ end
@@ -156,7 +156,7 @@ class Clive
156
156
  tokens
157
157
  end
158
158
 
159
- #### CREATION HELPERS ####
159
+ #### CREATION HELPERS ####
160
160
 
161
161
  # Add a new switch to +@switches+
162
162
  #
@@ -169,7 +169,7 @@ class Clive
169
169
  # @yield A block to run if the switch is triggered
170
170
  #
171
171
  def switch(*args, &block)
172
- short, long, desc = nil, nil, nil
172
+ short, long, desc = nil
173
173
  args.each do |i|
174
174
  if i.is_a? String
175
175
  desc = i
@@ -193,7 +193,7 @@ class Clive
193
193
  # and flags
194
194
  #
195
195
  def command(*args, &block)
196
- name, desc = nil, nil
196
+ name, desc = nil
197
197
  args.each do |i|
198
198
  if i.is_a? String
199
199
  desc = i
@@ -214,7 +214,7 @@ class Clive
214
214
  #
215
215
  # @yield [String] A block to be run if switch is triggered
216
216
  def flag(*args, &block)
217
- short, long, desc = nil, nil, nil
217
+ short, long, desc = nil
218
218
  args.each do |i|
219
219
  if i.is_a? String
220
220
  desc = i
@@ -227,7 +227,34 @@ class Clive
227
227
  @flags << Flag.new(short, long, desc, &block)
228
228
  end
229
229
 
230
- #### HELP STUFF ####
230
+ # Creates a boolean switch. This is done by adding two switches of
231
+ # Boolean type to +@switches+, one is created normally the other has
232
+ # "no-" appended to the long name and has no short name.
233
+ #
234
+ # @overload boolean(short, long, desc, &block)
235
+ # Creates a new boolean switch
236
+ # @param [Symbol] short single character for short label
237
+ # @param [Symbol] long longer name to be used
238
+ # @param [String] desc the description of the boolean
239
+ #
240
+ # @yield [Boolean] A block to be run if switch is triggered
241
+ def boolean(*args, &block)
242
+ short, long, desc = nil
243
+ args.each do |i|
244
+ if i.is_a? String
245
+ desc = i
246
+ elsif i.length == 1
247
+ short = i.to_s
248
+ else
249
+ long = i.to_s
250
+ end
251
+ end
252
+ raise "Boolean switch must have long name" unless long
253
+ @switches << Boolean.new(short, long, desc, true, &block)
254
+ @switches << Boolean.new(nil, "no-#{long}", desc, false, &block)
255
+ end
256
+
257
+ #### HELP STUFF ####
231
258
 
232
259
  # This actually creates a switch with "-h" and "--help" that control
233
260
  # the help on this command. If this is the base class it will also
@@ -259,7 +286,7 @@ class Clive
259
286
  if @switches.length > 0 || @flags.length > 0
260
287
  summary << "\n Options:\n"
261
288
  @switches.each do |i|
262
- summary << i.summary(width, prepend) << "\n"
289
+ summary << i.summary(width, prepend) << "\n" if i.summary
263
290
  end
264
291
  @flags.each do |i|
265
292
  summary << i.summary(width, prepend) << "\n"
@@ -14,7 +14,7 @@ class TestClive < Test::Unit::TestCase
14
14
  c = Clive.new do
15
15
  switch(:v) {}
16
16
  end
17
- assert_equal 1, c.switches.length
17
+ assert_equal 2, c.switches.length # plus help
18
18
  assert_instance_of Clive::Switch, c.switches[0]
19
19
  end
20
20
 
@@ -26,6 +26,14 @@ class TestClive < Test::Unit::TestCase
26
26
  assert_instance_of Clive::Command, c.commands[0]
27
27
  end
28
28
 
29
+ should "create boolean" do
30
+ c = Clive.new do
31
+ boolean(:verbose) {}
32
+ end
33
+ assert_equal 3, c.switches.length # plus help
34
+ assert_instance_of Clive::Boolean, c.switches[0]
35
+ end
36
+
29
37
  context "When parsing input" do
30
38
 
31
39
  should "recognise flags with equals" do
metadata CHANGED
@@ -1,13 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: clive
3
3
  version: !ruby/object:Gem::Version
4
- hash: 25
4
+ hash: 23
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 1
9
- - 1
10
- version: 0.1.1
8
+ - 2
9
+ - 0
10
+ version: 0.2.0
11
11
  platform: ruby
12
12
  authors:
13
13
  - Joshua Hawxwell
@@ -64,6 +64,7 @@ files:
64
64
  - VERSION
65
65
  - clive.gemspec
66
66
  - lib/clive.rb
67
+ - lib/clive/booleans.rb
67
68
  - lib/clive/commands.rb
68
69
  - lib/clive/ext.rb
69
70
  - lib/clive/flags.rb