clive 0.2.2 → 0.2.3

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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.2
1
+ 0.2.3
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{clive}
8
- s.version = "0.2.2"
8
+ s.version = "0.2.3"
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"]
12
- s.date = %q{2010-08-15}
12
+ s.date = %q{2010-08-17}
13
13
  s.description = %q{Clive is a DSL for creating a command line interface. It is for people who, like me, love OptionParser's syntax and love GLI's commands.}
14
14
  s.email = %q{m@hawx.me}
15
15
  s.extra_rdoc_files = [
@@ -33,8 +33,12 @@ Gem::Specification.new do |s|
33
33
  "lib/clive/tokens.rb",
34
34
  "test/bin_test",
35
35
  "test/helper.rb",
36
+ "test/test_boolean.rb",
36
37
  "test/test_clive.rb",
37
- "test/test_exceptions.rb"
38
+ "test/test_exceptions.rb",
39
+ "test/test_flag.rb",
40
+ "test/test_switch.rb",
41
+ "test/test_token.rb"
38
42
  ]
39
43
  s.homepage = %q{http://github.com/hawx/clive}
40
44
  s.rdoc_options = ["--charset=UTF-8"]
@@ -43,8 +47,12 @@ Gem::Specification.new do |s|
43
47
  s.summary = %q{Imagine if optparse and gli had a son called clive.}
44
48
  s.test_files = [
45
49
  "test/helper.rb",
50
+ "test/test_boolean.rb",
46
51
  "test/test_clive.rb",
47
- "test/test_exceptions.rb"
52
+ "test/test_exceptions.rb",
53
+ "test/test_flag.rb",
54
+ "test/test_switch.rb",
55
+ "test/test_token.rb"
48
56
  ]
49
57
 
50
58
  if s.respond_to? :specification_version then
@@ -86,5 +86,9 @@ class Clive
86
86
  @base.flags
87
87
  end
88
88
 
89
+ def booleans
90
+ @base.booleans
91
+ end
92
+
89
93
  end
90
94
 
@@ -26,8 +26,10 @@ class Clive
26
26
  a << "--[no-]#{@long}" if @long
27
27
  b = @desc
28
28
  s, p = '', ''
29
- (0..width-a.length).each {s << ' '}
30
- (0..prepend).each {p << ' '}
29
+ # want at least one space between name and desc
30
+ spaces = width-a.length < 0 ? 1 : width-a.length
31
+ (0...spaces).each {s << ' '}
32
+ (0...prepend).each {p << ' '}
31
33
  "#{p}#{a}#{s}#{b}"
32
34
  end
33
35
 
@@ -53,6 +53,11 @@ class Clive
53
53
  self.build_help
54
54
  end
55
55
 
56
+ # Getter to find booleans
57
+ def booleans
58
+ @switches.find_all {|i| i.is_a?(Boolean)}
59
+ end
60
+
56
61
  # Run the block that was passed to find switches, flags, etc.
57
62
  #
58
63
  # This should only be called if the command has been called
@@ -27,8 +27,10 @@ class Clive
27
27
  a << "--#{@long}" if @long
28
28
  b = @desc
29
29
  s, p = '', ''
30
- (0..width-a.length).each {s << ' '}
31
- (0..prepend).each {p << ' '}
30
+ # want at least one space between name and desc
31
+ spaces = width-a.length < 0 ? 1 : width-a.length
32
+ (0...spaces).each {s << ' '}
33
+ (0...prepend).each {p << ' '}
32
34
  "#{p}#{a}#{s}#{b}"
33
35
  end
34
36
 
@@ -5,75 +5,128 @@ class Clive
5
5
  #
6
6
  # [[:word, 'Value'], [:long, 'verbose'], [:short, 'r']]
7
7
  #
8
- # And converting between these and ordinary arrays.
8
+ # The tokens are not stored like that but as the string
9
+ # representations:
10
+ #
11
+ # ["Value", "--verbose", "-r"]
9
12
  #
10
13
  class Tokens < Array
11
- attr_accessor :tokens, :array
14
+
15
+ TOKEN_KEYS = [:word, :short, :long]
12
16
 
13
- def self.to_tokens(tokens)
14
- Tokens.new.to_tokens(tokens)
17
+ # Create a new Tokens instance. Pass either an array of tokens
18
+ # or a plain array, they will be converted correctly.
19
+ #
20
+ # @param [Array]
21
+ # @return [Tokens]
22
+ #
23
+ def initialize(args=[])
24
+ if token?(args[0])
25
+ r = []
26
+ args.each {|i| r << token_to_string(i)}
27
+ args = r
28
+ end
29
+ super(args)
15
30
  end
16
31
 
17
- def self.to_array(arr)
18
- Tokens.new.to_array(arr)
32
+ # Turn +@tokens+ into an array, this ensures that shorts are split
33
+ # as is expected
34
+ #
35
+ # @return [Array] array representation of tokens held
36
+ def array
37
+ return [] unless self.tokens
38
+ arr = []
39
+ self.tokens.each do |i|
40
+ k, v = i[0], i[1]
41
+ case k
42
+ when :long
43
+ arr << "--#{v}"
44
+ when :short
45
+ arr << "-#{v}"
46
+ when :word
47
+ arr << v
48
+ end
49
+ end
50
+
51
+ arr
19
52
  end
20
53
 
21
- # Turn into simple tokens that have been split up into logical parts
54
+ # Creates an array of tokens based on +self+
22
55
  #
23
- # @example
24
- #
25
- # a = Tokens.new
26
- # a.to_tokens(["add", "-al", "--verbose"])
27
- # #=> [[:word, "add"], [:short, "a"], [:short, "l"], [:long, "verbose"]]
28
- #
29
- def to_tokens(arr=@array)
30
- @tokens = []
31
- arr.each do |i|
56
+ # @return [Array] the tokens that are held
57
+ def tokens
58
+ t = []
59
+ self.each do |i|
32
60
  if i[0..1] == "--"
33
61
  if i.include?('=')
34
62
  a, b = i[2..i.length].split('=')
35
- @tokens << [:long, a] << [:word, b]
63
+ t << [:long, a] << [:word, b]
36
64
  else
37
- @tokens << [:long, i[2..i.length]]
65
+ t << [:long, i[2..i.length]]
38
66
  end
39
-
67
+
40
68
  elsif i[0] == "-"
41
69
  i[1..i.length].split('').each do |j|
42
- tokens << [:short, j]
70
+ t << [:short, j]
43
71
  end
44
72
 
45
73
  else
46
- @tokens << [:word, i]
74
+ t << [:word, i]
47
75
  end
48
76
  end
49
77
 
50
- @tokens
78
+ t
79
+ end
80
+
81
+ def self.to_tokens(arr)
82
+ Tokens.new(arr).tokens
83
+ end
84
+
85
+ def self.to_array(tokens)
86
+ Tokens.new(tokens).array
51
87
  end
52
88
 
53
- # Turn tokens back to a normal array
89
+ # Checks to see if it is a token being added and changes it back
90
+ def <<(val)
91
+ if token?(val)
92
+ super(token_to_string(val))
93
+ else
94
+ super
95
+ end
96
+ end
97
+
98
+ # Test whether an array is a token
99
+ #
100
+ # @param [Array]
101
+ # @return [Boolean]
54
102
  #
55
103
  # @example
56
- #
57
- # a = Tokens.new
58
- # a.to_array([[:word, "add"], [:short, "a"], [:short, "l"],
59
- # [:long, "verbose"]])
60
- # #=> ["add", "-al", "--verbose"]
104
+ #
105
+ # t.token?([:word, "something"]) #=> true
106
+ # t.token?(["a", "normal", "array"]) #=> false
61
107
  #
62
- def to_array(tokens=@tokens)
63
- @array = []
64
- tokens.each do |i|
65
- k, v = i[0], i[1]
66
- case k
67
- when :long
68
- @array << "--#{v}"
69
- when :short
70
- @array << "-#{v}"
71
- when :word
72
- @array << v
73
- end
108
+ def token?(arr)
109
+ return false if arr.nil?
110
+ TOKEN_KEYS.include?(arr[0])
111
+ end
112
+
113
+ # Convert a tokens to its string representation
114
+ def token_to_string(token)
115
+ k, v = token[0], token[1]
116
+ case k
117
+ when :long
118
+ "--#{v}"
119
+ when :short
120
+ "-#{v}"
121
+ when :word
122
+ v
74
123
  end
75
-
76
- @array
124
+ end
125
+
126
+ # This is here to force the use of #tokens and #array when
127
+ # accessing the contents
128
+ def inspect
129
+ "#<Clive::Tokens:0x#{'%x' % (self.object_id << 1)} #{self.tokens}>"
77
130
  end
78
131
 
79
132
  end
@@ -0,0 +1,48 @@
1
+ require 'helper'
2
+
3
+ class TestBoolean < Test::Unit::TestCase
4
+
5
+ context "A new boolean switch" do
6
+
7
+ setup do
8
+ @c = Clive.new do
9
+ boolean(:v, :verbose, "Run verbosely") {|i| $stdout.puts(i)}
10
+ end
11
+ end
12
+
13
+ should "create two switches" do
14
+ assert_equal 2, @c.booleans.length
15
+ end
16
+
17
+ context "the true switch" do
18
+ should "have a short name" do
19
+ assert_equal "v", @c.switches["verbose"].short
20
+ end
21
+
22
+ should "pass true to the block" do
23
+ mock($stdout).puts(true)
24
+ @c.parse(["--verbose"])
25
+ end
26
+
27
+ should "create summary" do
28
+ assert_equal "-v, --[no-]verbose Run verbosely", @c.switches["verbose"].summary(0, 0)
29
+ end
30
+ end
31
+
32
+ context "the false switch" do
33
+ should "not have short name" do
34
+ assert_equal nil, @c.switches["no-verbose"].short
35
+ end
36
+
37
+ should "pass false to the block" do
38
+ mock($stdout).puts(false)
39
+ @c.parse(["--no-verbose"])
40
+ end
41
+
42
+ should "not create summary" do
43
+ assert_equal nil, @c.switches["no-verbose"].summary
44
+ end
45
+ end
46
+
47
+ end
48
+ end
@@ -0,0 +1,19 @@
1
+ require 'helper'
2
+
3
+ class TestFlag < Test::Unit::TestCase
4
+
5
+ context "A new flag" do
6
+
7
+ setup do
8
+ @c = Clive.new do
9
+ flag(:t, :type, "Choose type") {|i| $stdout.puts(i)}
10
+ end
11
+ end
12
+
13
+ should "pass an argument to block" do
14
+ mock($stdout).puts("text")
15
+ @c.parse(["--type", "text"])
16
+ end
17
+
18
+ end
19
+ end
@@ -0,0 +1,18 @@
1
+ require 'helper'
2
+
3
+ class TestSwitch < Test::Unit::TestCase
4
+
5
+ context "A new switch" do
6
+
7
+ setup do
8
+ @c = Clive.new do
9
+ switch(:d, :debug, "Use debug mode") {}
10
+ end
11
+ end
12
+
13
+ should "create summary" do
14
+ assert_equal "-d, --debug Use debug mode", @c.switches["d"].summary(0, 0)
15
+ end
16
+
17
+ end
18
+ end
@@ -0,0 +1,55 @@
1
+ require 'helper'
2
+
3
+ class TestToken < Test::Unit::TestCase
4
+
5
+ context "When converting" do
6
+ setup do
7
+ @array = ["add", "-al", "--verbose"]
8
+ @array_split = ["add", "-a", "-l", "--verbose"]
9
+ @tokens = [[:word, "add"], [:short, "a"], [:short, "l"], [:long, "verbose"]]
10
+ end
11
+
12
+ should "convert an array to tokens" do
13
+ t = Clive::Tokens.new(@array)
14
+ assert_equal @tokens, t.tokens
15
+ end
16
+
17
+ should "convert tokens to an array" do
18
+ assert_equal @array_split, Clive::Tokens.to_array(@tokens)
19
+ end
20
+ end
21
+
22
+ context "A new tokens instance" do
23
+
24
+ setup do
25
+ @array = ["add", "-al", "--verbose"]
26
+ @array_split = ["add", "-a", "-l", "--verbose"]
27
+ @tokens = [[:word, "add"], [:short, "a"], [:short, "l"], [:long, "verbose"]]
28
+
29
+ @t = Clive::Tokens.new
30
+ @t << "add" << "-al" << "--verbose"
31
+ end
32
+
33
+ should "be created from an array" do
34
+ t = Clive::Tokens.new(@tokens)
35
+ assert_equal @tokens, t.tokens
36
+ assert_equal @array_split, t.array
37
+ end
38
+
39
+ should "be created from tokens" do
40
+ t = Clive::Tokens.new(@array)
41
+ assert_equal @tokens, t.tokens
42
+ assert_equal @array_split, t.array
43
+ end
44
+
45
+ should "have tokens" do
46
+
47
+ end
48
+
49
+ should "have an array" do
50
+
51
+ end
52
+
53
+ end
54
+
55
+ end
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: 19
4
+ hash: 17
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
8
  - 2
9
- - 2
10
- version: 0.2.2
9
+ - 3
10
+ version: 0.2.3
11
11
  platform: ruby
12
12
  authors:
13
13
  - Joshua Hawxwell
@@ -15,7 +15,7 @@ autorequire:
15
15
  bindir: bin
16
16
  cert_chain: []
17
17
 
18
- date: 2010-08-15 00:00:00 +01:00
18
+ date: 2010-08-17 00:00:00 +01:00
19
19
  default_executable:
20
20
  dependencies:
21
21
  - !ruby/object:Gem::Dependency
@@ -72,8 +72,12 @@ files:
72
72
  - lib/clive/tokens.rb
73
73
  - test/bin_test
74
74
  - test/helper.rb
75
+ - test/test_boolean.rb
75
76
  - test/test_clive.rb
76
77
  - test/test_exceptions.rb
78
+ - test/test_flag.rb
79
+ - test/test_switch.rb
80
+ - test/test_token.rb
77
81
  has_rdoc: true
78
82
  homepage: http://github.com/hawx/clive
79
83
  licenses: []
@@ -110,5 +114,9 @@ specification_version: 3
110
114
  summary: Imagine if optparse and gli had a son called clive.
111
115
  test_files:
112
116
  - test/helper.rb
117
+ - test/test_boolean.rb
113
118
  - test/test_clive.rb
114
119
  - test/test_exceptions.rb
120
+ - test/test_flag.rb
121
+ - test/test_switch.rb
122
+ - test/test_token.rb