clive 0.6.2 → 0.7.0
Sign up to get free protection for your applications and to get access to all the features.
- data/README.md +129 -126
- data/lib/clive.rb +30 -119
- data/lib/clive/bool.rb +32 -34
- data/lib/clive/command.rb +171 -54
- data/lib/clive/exceptions.rb +2 -2
- data/lib/clive/ext.rb +21 -3
- data/lib/clive/flag.rb +80 -67
- data/lib/clive/formatter.rb +180 -0
- data/lib/clive/option.rb +41 -25
- data/lib/clive/output.rb +14 -18
- data/lib/clive/parser.rb +79 -16
- data/lib/clive/switch.rb +8 -17
- data/lib/clive/tokens.rb +1 -1
- data/lib/clive/version.rb +2 -2
- data/spec/clive/bool_spec.rb +54 -0
- data/spec/clive/command_spec.rb +260 -0
- data/spec/clive/exceptions_spec.rb +1 -0
- data/spec/clive/ext_spec.rb +1 -0
- data/spec/clive/flag_spec.rb +84 -0
- data/spec/clive/formatter_spec.rb +108 -0
- data/spec/clive/option_spec.rb +34 -0
- data/spec/clive/output_spec.rb +5 -0
- data/spec/clive/parser_spec.rb +106 -0
- data/spec/clive/switch_spec.rb +14 -0
- data/spec/clive/tokens_spec.rb +38 -0
- data/spec/shared_specs.rb +16 -0
- data/spec/spec_helper.rb +12 -0
- metadata +34 -8
@@ -0,0 +1 @@
|
|
1
|
+
require 'spec_helper'
|
@@ -0,0 +1 @@
|
|
1
|
+
require 'spec_helper'
|
@@ -0,0 +1,84 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Clive::Flag do
|
4
|
+
|
5
|
+
subject { Clive::Flag.new([:S, :say], "Say something", ["WORD(S)"]) {|i| puts i } }
|
6
|
+
|
7
|
+
it_behaves_like "an option"
|
8
|
+
|
9
|
+
describe "#args" do
|
10
|
+
it "returns a hash with argument(s)" do
|
11
|
+
subject.args.should == [{:name => "WORD(S)", :optional => false}]
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "#run" do
|
16
|
+
it "calls the block with the argument" do
|
17
|
+
$stdout.should_receive(:puts).with("hey")
|
18
|
+
subject.run(["hey"])
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "#arg_num" do
|
23
|
+
it "returns the number of arguments" do
|
24
|
+
subject.arg_num(false).should == 1
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "#args_to_strings" do
|
29
|
+
it "converts the arguments to strings" do
|
30
|
+
subject.args_to_strings.should == ["WORD(S)"]
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe "#arg_size" do
|
35
|
+
context "when choice is available" do
|
36
|
+
it "returns 1" do
|
37
|
+
subject.args = 1..5
|
38
|
+
subject.arg_size.should == 1
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
context "when arguments are required" do
|
43
|
+
it "returns the number of arguments" do
|
44
|
+
subject.args = [{:name => "ARG", :optional => false}]
|
45
|
+
subject.arg_size.should == 1
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
describe "#options_to_strings" do
|
51
|
+
context "when @args is a range" do
|
52
|
+
it "returns array with range as string" do
|
53
|
+
subject.args = 1..4
|
54
|
+
subject.options_to_strings.should == ["1..4"]
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
context "when @args is a hash" do
|
59
|
+
it "converts the options to strings" do
|
60
|
+
subject.options_to_strings.should == [""]
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
context "when @args is an array" do
|
65
|
+
it "returns the array" do
|
66
|
+
subject.args = %w(1 2 3 4)
|
67
|
+
subject.options_to_strings.should == %w(1 2 3 4)
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
describe "#to_h" do
|
73
|
+
it "returns a hash" do
|
74
|
+
hsh = {
|
75
|
+
"names" => Clive::Array.new(%w(-S --say)),
|
76
|
+
"desc" => "Say something",
|
77
|
+
"args" => Clive::Array.new(["WORD(S)"]),
|
78
|
+
"options" => Clive::Array.new([""])
|
79
|
+
}
|
80
|
+
subject.to_h.should == hsh
|
81
|
+
end
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
@@ -0,0 +1,108 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Clive::Formatter do
|
4
|
+
|
5
|
+
describe Clive::Formatter::Obj do
|
6
|
+
subject { Clive::Formatter::Obj.new({:test => 5}) }
|
7
|
+
|
8
|
+
describe "#initialize" do
|
9
|
+
it "defines methods given by hash" do
|
10
|
+
subject.test.should == 5
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "#evaluate" do
|
15
|
+
it "evaluates code within the object" do
|
16
|
+
$stdout.should_receive(:puts).with(5)
|
17
|
+
subject.evaluate("puts test")
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
|
23
|
+
subject { Clive::Formatter.new(30, 5) }
|
24
|
+
|
25
|
+
|
26
|
+
describe "#switch" do
|
27
|
+
it "sets format for switches" do
|
28
|
+
subject.switch("{desc}")
|
29
|
+
subject.instance_variable_get("@switch").should == "{desc}"
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
describe "#bool" do
|
34
|
+
it "sets format for bools" do
|
35
|
+
subject.bool "{desc}"
|
36
|
+
subject.instance_variable_get("@bool").should == "{desc}"
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "#flag" do
|
41
|
+
it "sets format for flags" do
|
42
|
+
subject.flag "{desc}"
|
43
|
+
subject.instance_variable_get("@flag").should == "{desc}"
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
47
|
+
describe "#command" do
|
48
|
+
it "sets format for commands" do
|
49
|
+
subject.command "{desc}"
|
50
|
+
subject.instance_variable_get("@command").should == "{desc}"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
|
55
|
+
describe "#format" do
|
56
|
+
it "generates the help" do
|
57
|
+
formatter = Clive::Command.new(true).help_formatter(:white)
|
58
|
+
options = [
|
59
|
+
Clive::Switch.new([:t, :test], "A test switch"),
|
60
|
+
Clive::Bool.new([:boolean], "A bool", true),
|
61
|
+
Clive::Bool.new([:boolean], "A bool", false),
|
62
|
+
Clive::Flag.new([:args], "With args", ["ARG [OPT]"]),
|
63
|
+
Clive::Flag.new([:choose], "With options", [["a", "b", "c"]])
|
64
|
+
]
|
65
|
+
command = Clive::Command.new(:command, "A command")
|
66
|
+
result = <<EOS
|
67
|
+
head
|
68
|
+
|
69
|
+
Commands:
|
70
|
+
command A command
|
71
|
+
|
72
|
+
Options:
|
73
|
+
-t, --test A test switch
|
74
|
+
--[no-]boolean A bool
|
75
|
+
--args ARG [OPT] With args \e[1m\e[0m
|
76
|
+
--choose With options \e[1m(a, b, c)\e[0m
|
77
|
+
|
78
|
+
foot
|
79
|
+
EOS
|
80
|
+
|
81
|
+
formatter.format("head", "foot", [command], options).should == result
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
|
86
|
+
describe "#parse" do
|
87
|
+
it "parses before and after '{spaces}' separately" do
|
88
|
+
args = {"name" => "a", "desc" => "something"}
|
89
|
+
subject.should_receive(:parse_format).with("{name}", args).and_return("")
|
90
|
+
subject.should_receive(:parse_format).with("{desc}", args).and_return("")
|
91
|
+
subject.parse("{name}{spaces}{desc}", args)
|
92
|
+
end
|
93
|
+
|
94
|
+
it "calculates the correct number of spaces" do
|
95
|
+
args = {"name" => "a long name", "desc" => "|a short desc"}
|
96
|
+
subject.parse("{name}{spaces}{desc}", args).split('|')[0].size.should == 30
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
describe "#parse_format" do
|
101
|
+
it "inserts the arguments into the format" do
|
102
|
+
subject.parse_format(
|
103
|
+
"{desc}{name}", {'desc' => 'a', 'name' => 'b'}
|
104
|
+
).should == "ab"
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
end
|
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Clive::Option do
|
4
|
+
subject { Clive::Option.new([:n, :names], "A test option") { puts "hi" } }
|
5
|
+
|
6
|
+
it_behaves_like "an option"
|
7
|
+
|
8
|
+
describe "#run" do
|
9
|
+
it "calls the block" do
|
10
|
+
$stdout.should_receive(:puts).with("hi")
|
11
|
+
subject.run
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
describe "#sort_name" do
|
16
|
+
it "returns the name to sort by" do
|
17
|
+
subject.sort_name.should == 'n'
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
describe "#<=>" do
|
22
|
+
it "sorts by #sort_name" do
|
23
|
+
other = Clive::Option.new([:z, :apple], "Another") {}
|
24
|
+
(subject <=> other).should == -1
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
describe "#to_h" do
|
29
|
+
it "returns a hash for help" do
|
30
|
+
hsh = {"names" => subject.names_to_strings, "desc" => subject.desc}
|
31
|
+
subject.to_h.should == hsh
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
@@ -0,0 +1,106 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Clive::Parser do
|
4
|
+
|
5
|
+
subject {
|
6
|
+
class CLI
|
7
|
+
include Clive::Parser
|
8
|
+
end
|
9
|
+
CLI
|
10
|
+
}
|
11
|
+
|
12
|
+
it "keeps track of its class" do
|
13
|
+
subject.instance_variable_get("@klass").should == CLI
|
14
|
+
end
|
15
|
+
|
16
|
+
describe "#base" do
|
17
|
+
it "has a 'base' command" do
|
18
|
+
subject.base.should be_kind_of Clive::Command
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "#parse" do
|
23
|
+
it "should call #run on 'base'" do
|
24
|
+
subject.base.should_receive(:run).with([])
|
25
|
+
subject.parse([])
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
describe "#flag" do
|
30
|
+
it "adds a new flag to 'base'" do
|
31
|
+
expect {
|
32
|
+
subject.flag(:t, :test, :arg => "NAME") {|i| puts i }
|
33
|
+
}.should change {subject.base.flags.size}.by(1)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe "#switch" do
|
38
|
+
it "adds a new switch to 'base'" do
|
39
|
+
expect {
|
40
|
+
subject.switch(:t, :test) {}
|
41
|
+
}.should change {subject.base.switches.size}.by(1)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
describe "#command" do
|
46
|
+
it "adds a new command to 'base'" do
|
47
|
+
expect {
|
48
|
+
subject.command(:command) {}
|
49
|
+
}.should change {subject.base.commands.size}.by(1)
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
describe "#bool" do
|
54
|
+
it "adds a new bool to 'base'" do
|
55
|
+
expect {
|
56
|
+
subject.bool(:boo) {}
|
57
|
+
}.should change {subject.base.bools.size}.by(2)
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
describe "#desc" do
|
62
|
+
it "sets current desc for 'base'" do
|
63
|
+
subject.desc "test"
|
64
|
+
subject.base.instance_variable_get("@current_desc").should == "test"
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
describe "#help_formatter" do
|
69
|
+
it "sets the help formatter for 'base'" do
|
70
|
+
subject.base.should_receive(:help_formatter).with(:white)
|
71
|
+
subject.help_formatter(:white)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
|
75
|
+
describe "#option_var" do
|
76
|
+
before { subject.option_var(:test, 1) }
|
77
|
+
|
78
|
+
it "creates a getter" do
|
79
|
+
subject.should respond_to :test
|
80
|
+
end
|
81
|
+
|
82
|
+
it "creates a setter" do
|
83
|
+
subject.should respond_to :test=
|
84
|
+
subject.test = 2
|
85
|
+
end
|
86
|
+
|
87
|
+
it "sets a default value" do
|
88
|
+
subject.test.should == 1
|
89
|
+
end
|
90
|
+
end
|
91
|
+
|
92
|
+
describe "#option_hash" do
|
93
|
+
it "uses #option_var with empty hash" do
|
94
|
+
subject.should_receive(:option_var).with(:test, {})
|
95
|
+
subject.option_hash(:test)
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
describe "#option_array" do
|
100
|
+
it "uses #option_var with empty array" do
|
101
|
+
subject.should_receive(:option_var).with(:test, [])
|
102
|
+
subject.option_array(:test)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Clive::Switch do
|
4
|
+
subject { Clive::Switch.new([:n, :names], "A description") { puts "hi" } }
|
5
|
+
|
6
|
+
it_behaves_like "an option"
|
7
|
+
|
8
|
+
describe "#run" do
|
9
|
+
it "calls the block" do
|
10
|
+
$stdout.should_receive(:puts).with("hi")
|
11
|
+
subject.run
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Clive::Tokens do
|
4
|
+
|
5
|
+
subject { Clive::Tokens.new %w(word --long -sa) }
|
6
|
+
|
7
|
+
|
8
|
+
describe "#array" do
|
9
|
+
it "returns array of strings" do
|
10
|
+
subject.array.should == ["word", "--long", "-s", "-a"]
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "#tokens" do
|
15
|
+
it "returns array of tokens" do
|
16
|
+
subject.tokens.should == [[:word, "word"], [:long, "long"], [:short, "s"], [:short, "a"]]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "#<<" do
|
21
|
+
subject { Clive::Tokens.new }
|
22
|
+
|
23
|
+
context "when adding token" do
|
24
|
+
it "adds as a string" do
|
25
|
+
subject << [:long, "test"]
|
26
|
+
subject.tokens.should == [[:long, "test"]]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context "when adding string" do
|
31
|
+
it "adds normally" do
|
32
|
+
subject << "--test"
|
33
|
+
subject.tokens.should == [[:long, "test"]]
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
end
|
@@ -0,0 +1,16 @@
|
|
1
|
+
shared_examples_for "an option" do
|
2
|
+
|
3
|
+
describe "#names" do
|
4
|
+
specify { subject.names.should be_kind_of(Array) }
|
5
|
+
specify { subject.names.each {|i| i.should be_kind_of(String) } }
|
6
|
+
end
|
7
|
+
|
8
|
+
describe "#desc" do
|
9
|
+
specify { subject.desc.should be_kind_of(String) }
|
10
|
+
end
|
11
|
+
|
12
|
+
describe "#block" do
|
13
|
+
specify { subject.block.should be_kind_of(Proc) }
|
14
|
+
end
|
15
|
+
|
16
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
# $: << File.join(File.dirname(__FILE__), '..')
|
2
|
+
# $: << File.dirname(__FILE__)
|
3
|
+
require 'duvet'
|
4
|
+
Duvet.start :filter => 'clive/lib'
|
5
|
+
|
6
|
+
require 'rspec'
|
7
|
+
require 'clive'
|
8
|
+
require 'shared_specs'
|
9
|
+
|
10
|
+
RSpec.configure do |config|
|
11
|
+
config.color_enabled = true
|
12
|
+
end
|
metadata
CHANGED
@@ -4,9 +4,9 @@ version: !ruby/object:Gem::Version
|
|
4
4
|
prerelease: false
|
5
5
|
segments:
|
6
6
|
- 0
|
7
|
-
-
|
8
|
-
-
|
9
|
-
version: 0.
|
7
|
+
- 7
|
8
|
+
- 0
|
9
|
+
version: 0.7.0
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Joshua Hawxwell
|
@@ -14,11 +14,11 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date:
|
17
|
+
date: 2011-01-06 00:00:00 +00:00
|
18
18
|
default_executable:
|
19
19
|
dependencies: []
|
20
20
|
|
21
|
-
description: " Clive
|
21
|
+
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
22
|
email: m@hawx.me
|
23
23
|
executables: []
|
24
24
|
|
@@ -35,6 +35,7 @@ files:
|
|
35
35
|
- lib/clive/exceptions.rb
|
36
36
|
- lib/clive/ext.rb
|
37
37
|
- lib/clive/flag.rb
|
38
|
+
- lib/clive/formatter.rb
|
38
39
|
- lib/clive/option.rb
|
39
40
|
- lib/clive/output.rb
|
40
41
|
- lib/clive/parser.rb
|
@@ -42,6 +43,19 @@ files:
|
|
42
43
|
- lib/clive/tokens.rb
|
43
44
|
- lib/clive/version.rb
|
44
45
|
- lib/clive.rb
|
46
|
+
- spec/clive/bool_spec.rb
|
47
|
+
- spec/clive/command_spec.rb
|
48
|
+
- spec/clive/exceptions_spec.rb
|
49
|
+
- spec/clive/ext_spec.rb
|
50
|
+
- spec/clive/flag_spec.rb
|
51
|
+
- spec/clive/formatter_spec.rb
|
52
|
+
- spec/clive/option_spec.rb
|
53
|
+
- spec/clive/output_spec.rb
|
54
|
+
- spec/clive/parser_spec.rb
|
55
|
+
- spec/clive/switch_spec.rb
|
56
|
+
- spec/clive/tokens_spec.rb
|
57
|
+
- spec/shared_specs.rb
|
58
|
+
- spec/spec_helper.rb
|
45
59
|
has_rdoc: false
|
46
60
|
homepage: http://github.com/hawx/clive
|
47
61
|
licenses: []
|
@@ -73,6 +87,18 @@ rubyforge_project:
|
|
73
87
|
rubygems_version: 1.3.7
|
74
88
|
signing_key:
|
75
89
|
specification_version: 3
|
76
|
-
summary:
|
77
|
-
test_files:
|
78
|
-
|
90
|
+
summary: A DSL for building command line interfaces.
|
91
|
+
test_files:
|
92
|
+
- spec/clive/bool_spec.rb
|
93
|
+
- spec/clive/command_spec.rb
|
94
|
+
- spec/clive/exceptions_spec.rb
|
95
|
+
- spec/clive/ext_spec.rb
|
96
|
+
- spec/clive/flag_spec.rb
|
97
|
+
- spec/clive/formatter_spec.rb
|
98
|
+
- spec/clive/option_spec.rb
|
99
|
+
- spec/clive/output_spec.rb
|
100
|
+
- spec/clive/parser_spec.rb
|
101
|
+
- spec/clive/switch_spec.rb
|
102
|
+
- spec/clive/tokens_spec.rb
|
103
|
+
- spec/shared_specs.rb
|
104
|
+
- spec/spec_helper.rb
|