clive 0.7.0 → 0.7.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/clive.rb +14 -0
- data/lib/clive/bool.rb +1 -1
- data/lib/clive/command.rb +4 -4
- data/lib/clive/formatter.rb +13 -47
- data/lib/clive/parser.rb +0 -14
- data/lib/clive/tokens.rb +3 -4
- data/lib/clive/version.rb +1 -1
- data/spec/clive/bool_spec.rb +2 -2
- data/spec/clive/command_spec.rb +2 -2
- data/spec/clive/flag_spec.rb +1 -1
- data/spec/clive/formatter_spec.rb +2 -2
- data/spec/clive/option_spec.rb +1 -1
- data/spec/clive/switch_spec.rb +1 -1
- data/spec/spec_helper.rb +4 -4
- metadata +19 -5
data/lib/clive.rb
CHANGED
@@ -1,4 +1,18 @@
|
|
1
|
+
require 'ast_ast'
|
2
|
+
|
1
3
|
require 'clive/parser'
|
4
|
+
require 'clive/exceptions'
|
5
|
+
require 'clive/tokens'
|
6
|
+
require 'clive/ext'
|
7
|
+
|
8
|
+
require 'clive/option'
|
9
|
+
require 'clive/command'
|
10
|
+
require 'clive/switch'
|
11
|
+
require 'clive/flag'
|
12
|
+
require 'clive/bool'
|
13
|
+
|
14
|
+
require 'clive/output'
|
15
|
+
require 'clive/formatter'
|
2
16
|
|
3
17
|
# Clive is a simple dsl for creating command line interfaces
|
4
18
|
#
|
data/lib/clive/bool.rb
CHANGED
data/lib/clive/command.rb
CHANGED
@@ -38,8 +38,8 @@ module Clive
|
|
38
38
|
else
|
39
39
|
args.each do |i|
|
40
40
|
case i
|
41
|
-
when
|
42
|
-
@names
|
41
|
+
when ::Array
|
42
|
+
@names = i.map {|i| i.to_s }
|
43
43
|
when String
|
44
44
|
@desc = i
|
45
45
|
end
|
@@ -160,7 +160,7 @@ module Clive
|
|
160
160
|
t = commands[command].tokenize(post_command)
|
161
161
|
r << [:command, commands[command], t]
|
162
162
|
end
|
163
|
-
|
163
|
+
|
164
164
|
r
|
165
165
|
end
|
166
166
|
|
@@ -219,7 +219,7 @@ module Clive
|
|
219
219
|
# and flags
|
220
220
|
#
|
221
221
|
def command(*args, &block)
|
222
|
-
@commands << Command.new(
|
222
|
+
@commands << Command.new(args, @current_desc, &block)
|
223
223
|
@current_desc = ""
|
224
224
|
end
|
225
225
|
|
data/lib/clive/formatter.rb
CHANGED
@@ -1,5 +1,3 @@
|
|
1
|
-
require 'strscan'
|
2
|
-
|
3
1
|
module Clive
|
4
2
|
|
5
3
|
# The formatter controls formatting of help. It can be configured
|
@@ -113,23 +111,14 @@ module Clive
|
|
113
111
|
|
114
112
|
def parse_format(format, args)
|
115
113
|
if format
|
116
|
-
|
117
|
-
result = []
|
118
|
-
|
119
|
-
# Create object to eval in
|
114
|
+
tokens = Lexer.tokenise(format).to_a
|
120
115
|
obj = Obj.new(args)
|
121
|
-
|
122
|
-
until @scanner.eos?
|
123
|
-
a = scan_block || a = scan_text
|
124
|
-
result << a
|
125
|
-
end
|
126
|
-
|
127
116
|
r = ""
|
128
|
-
|
117
|
+
tokens.each do |(t,v)|
|
129
118
|
case t
|
130
|
-
when :block
|
119
|
+
when :block
|
131
120
|
r << obj.evaluate(v)
|
132
|
-
when :text
|
121
|
+
when :text
|
133
122
|
r << v
|
134
123
|
end
|
135
124
|
end
|
@@ -139,42 +128,19 @@ module Clive
|
|
139
128
|
end
|
140
129
|
end
|
141
130
|
|
131
|
+
class Lexer < Ast::Tokeniser
|
132
|
+
rule :text, /%(.)/ do |i|
|
133
|
+
i[1]
|
134
|
+
end
|
142
135
|
|
143
|
-
|
144
|
-
|
145
|
-
return unless @scanner.scan /\{/
|
146
|
-
|
147
|
-
pos = @scanner.pos
|
148
|
-
if @scanner.scan_until /\}/
|
149
|
-
@scanner.pos -= @scanner.matched.size
|
150
|
-
[:block, @scanner.pre_match[pos..-1]]
|
151
|
-
end
|
136
|
+
rule :block, /\{(.*?)\}/ do |i|
|
137
|
+
i[1]
|
152
138
|
end
|
153
139
|
|
154
|
-
|
155
|
-
text
|
156
|
-
|
157
|
-
pos = @scanner.pos
|
158
|
-
if @scanner.scan_until /(?<=[^\\])\{/
|
159
|
-
@scanner.pos -= @scanner.matched.size
|
160
|
-
text = @scanner.pre_match[pos..-1]
|
161
|
-
end
|
162
|
-
|
163
|
-
if text.nil?
|
164
|
-
text = @scanner.rest
|
165
|
-
@scanner.clear
|
166
|
-
end
|
167
|
-
|
168
|
-
# Remove }s from text
|
169
|
-
if text[0] == "}"
|
170
|
-
text = text[1..-1]
|
171
|
-
end
|
172
|
-
|
173
|
-
text.gsub!(/\\(.)/) {|m| m[1] }
|
174
|
-
|
175
|
-
[:text, text]
|
140
|
+
missing do |i|
|
141
|
+
Ast::Token.new(:text, i)
|
176
142
|
end
|
177
|
-
|
143
|
+
end
|
178
144
|
|
179
145
|
end
|
180
146
|
end
|
data/lib/clive/parser.rb
CHANGED
@@ -1,17 +1,3 @@
|
|
1
|
-
require 'clive/exceptions'
|
2
|
-
require 'clive/tokens'
|
3
|
-
require 'clive/ext'
|
4
|
-
|
5
|
-
require 'clive/option'
|
6
|
-
require 'clive/command'
|
7
|
-
require 'clive/switch'
|
8
|
-
require 'clive/flag'
|
9
|
-
require 'clive/bool'
|
10
|
-
|
11
|
-
require 'clive/output'
|
12
|
-
require 'clive/formatter'
|
13
|
-
|
14
|
-
|
15
1
|
module Clive
|
16
2
|
|
17
3
|
# A module wrapping the command line parsing of clive. In the future this
|
data/lib/clive/tokens.rb
CHANGED
@@ -64,19 +64,18 @@ module Clive
|
|
64
64
|
def tokens
|
65
65
|
t = []
|
66
66
|
self.each do |i|
|
67
|
-
|
67
|
+
case i
|
68
|
+
when /\-\-.+/
|
68
69
|
if i.include?('=')
|
69
70
|
a, b = i[2..i.length].split('=')
|
70
71
|
t << [:long, a] << [:word, b]
|
71
72
|
else
|
72
73
|
t << [:long, i[2..i.length]]
|
73
74
|
end
|
74
|
-
|
75
|
-
elsif i[0] == "-"
|
75
|
+
when /\-.+/
|
76
76
|
i[1..i.length].split('').each do |j|
|
77
77
|
t << [:short, j]
|
78
78
|
end
|
79
|
-
|
80
79
|
else
|
81
80
|
t << [:word, i]
|
82
81
|
end
|
data/lib/clive/version.rb
CHANGED
data/spec/clive/bool_spec.rb
CHANGED
@@ -2,8 +2,8 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe Clive::Bool do
|
4
4
|
|
5
|
-
subject { Clive::Bool.new([:n, :name], "A test", true) {|arg| puts arg } }
|
6
|
-
let(:falsey) { Clive::Bool.new([:n, :name], "A test", false) {|arg| puts arg } }
|
5
|
+
subject { Clive::Bool.new([:n, :name], "A test", true) {|arg| $stdout.puts arg } }
|
6
|
+
let(:falsey) { Clive::Bool.new([:n, :name], "A test", false) {|arg| $stdout.puts arg } }
|
7
7
|
|
8
8
|
describe "#truth" do
|
9
9
|
it "returns the truth" do
|
data/spec/clive/command_spec.rb
CHANGED
@@ -7,7 +7,7 @@ describe Clive::Command do
|
|
7
7
|
end
|
8
8
|
|
9
9
|
subject {
|
10
|
-
Clive::Command.new(:co, :comm, "A command") do
|
10
|
+
Clive::Command.new([:co, :comm], "A command") do
|
11
11
|
bool(:boo) {}
|
12
12
|
switch(:swi) {}
|
13
13
|
flag(:fla) {}
|
@@ -19,7 +19,7 @@ describe Clive::Command do
|
|
19
19
|
|
20
20
|
describe "#initialize" do
|
21
21
|
subject {
|
22
|
-
Clive::Command.new(:com, "A command") do
|
22
|
+
Clive::Command.new([:com], "A command") do
|
23
23
|
flag(:test)
|
24
24
|
end
|
25
25
|
}
|
data/spec/clive/flag_spec.rb
CHANGED
@@ -14,7 +14,7 @@ describe Clive::Formatter do
|
|
14
14
|
describe "#evaluate" do
|
15
15
|
it "evaluates code within the object" do
|
16
16
|
$stdout.should_receive(:puts).with(5)
|
17
|
-
subject.evaluate("puts test")
|
17
|
+
subject.evaluate("$stdout.puts test")
|
18
18
|
end
|
19
19
|
end
|
20
20
|
end
|
@@ -62,7 +62,7 @@ describe Clive::Formatter do
|
|
62
62
|
Clive::Flag.new([:args], "With args", ["ARG [OPT]"]),
|
63
63
|
Clive::Flag.new([:choose], "With options", [["a", "b", "c"]])
|
64
64
|
]
|
65
|
-
command = Clive::Command.new(:command, "A command")
|
65
|
+
command = Clive::Command.new([:command], "A command")
|
66
66
|
result = <<EOS
|
67
67
|
head
|
68
68
|
|
data/spec/clive/option_spec.rb
CHANGED
data/spec/clive/switch_spec.rb
CHANGED
data/spec/spec_helper.rb
CHANGED
@@ -1,7 +1,7 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
|
4
|
-
|
1
|
+
if RUBY_VERSION >= "1.9"
|
2
|
+
require 'duvet'
|
3
|
+
Duvet.start :filter => 'clive/lib'
|
4
|
+
end
|
5
5
|
|
6
6
|
require 'rspec'
|
7
7
|
require 'clive'
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 7
|
8
|
-
-
|
9
|
-
version: 0.7.
|
8
|
+
- 1
|
9
|
+
version: 0.7.1
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Joshua Hawxwell
|
@@ -14,10 +14,24 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-01-
|
17
|
+
date: 2011-01-07 00:00:00 +00:00
|
18
18
|
default_executable:
|
19
|
-
dependencies:
|
20
|
-
|
19
|
+
dependencies:
|
20
|
+
- !ruby/object:Gem::Dependency
|
21
|
+
name: ast_ast
|
22
|
+
prerelease: false
|
23
|
+
requirement: &id001 !ruby/object:Gem::Requirement
|
24
|
+
none: false
|
25
|
+
requirements:
|
26
|
+
- - ~>
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
segments:
|
29
|
+
- 0
|
30
|
+
- 2
|
31
|
+
- 1
|
32
|
+
version: 0.2.1
|
33
|
+
type: :runtime
|
34
|
+
version_requirements: *id001
|
21
35
|
description: " Clive provides a DSL for building command line interfaces. It allows \n you to define commands, switches, flags (switches with options) and \n boolean switches, it then parses the input and runs the correct blocks.\n"
|
22
36
|
email: m@hawx.me
|
23
37
|
executables: []
|