jls-clamp 0.3.1
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/.gitignore +8 -0
- data/.travis.yml +5 -0
- data/Gemfile +9 -0
- data/README.markdown +274 -0
- data/Rakefile +12 -0
- data/clamp.gemspec +24 -0
- data/examples/flipflop +31 -0
- data/examples/fubar +23 -0
- data/examples/gitdown +61 -0
- data/examples/speak +33 -0
- data/lib/clamp/attribute.rb +40 -0
- data/lib/clamp/attribute_declaration.rb +40 -0
- data/lib/clamp/command.rb +142 -0
- data/lib/clamp/errors.rb +26 -0
- data/lib/clamp/help.rb +100 -0
- data/lib/clamp/option/declaration.rb +57 -0
- data/lib/clamp/option/parsing.rb +59 -0
- data/lib/clamp/option.rb +80 -0
- data/lib/clamp/parameter/declaration.rb +28 -0
- data/lib/clamp/parameter/parsing.rb +24 -0
- data/lib/clamp/parameter.rb +80 -0
- data/lib/clamp/subcommand/declaration.rb +44 -0
- data/lib/clamp/subcommand/parsing.rb +41 -0
- data/lib/clamp/subcommand.rb +23 -0
- data/lib/clamp/version.rb +3 -0
- data/lib/clamp.rb +3 -0
- data/spec/clamp/command_group_spec.rb +267 -0
- data/spec/clamp/command_spec.rb +766 -0
- data/spec/clamp/option_module_spec.rb +37 -0
- data/spec/clamp/option_spec.rb +149 -0
- data/spec/clamp/parameter_spec.rb +201 -0
- data/spec/spec_helper.rb +45 -0
- metadata +84 -0
@@ -0,0 +1,37 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Clamp::Command do
|
4
|
+
|
5
|
+
include OutputCapture
|
6
|
+
|
7
|
+
describe "with included module" do
|
8
|
+
|
9
|
+
before do
|
10
|
+
|
11
|
+
shared_options = Module.new do
|
12
|
+
extend Clamp::Option::Declaration
|
13
|
+
option "--size", "SIZE", :default => 4
|
14
|
+
end
|
15
|
+
|
16
|
+
@command_class = Class.new(Clamp::Command) do
|
17
|
+
|
18
|
+
include shared_options
|
19
|
+
|
20
|
+
def execute
|
21
|
+
puts "size = #{size}"
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
|
26
|
+
@command = @command_class.new("foo")
|
27
|
+
|
28
|
+
end
|
29
|
+
|
30
|
+
it "accepts options from included module" do
|
31
|
+
@command.run(["--size", "42"])
|
32
|
+
stdout.should == "size = 42\n"
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
@@ -0,0 +1,149 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Clamp::Option do
|
4
|
+
|
5
|
+
describe "with String argument" do
|
6
|
+
|
7
|
+
before do
|
8
|
+
@option = Clamp::Option.new("--key-file", "FILE", "SSH identity")
|
9
|
+
end
|
10
|
+
|
11
|
+
it "has a long_switch" do
|
12
|
+
@option.long_switch.should == "--key-file"
|
13
|
+
end
|
14
|
+
|
15
|
+
it "has a type" do
|
16
|
+
@option.type.should == "FILE"
|
17
|
+
end
|
18
|
+
|
19
|
+
it "has a description" do
|
20
|
+
@option.description.should == "SSH identity"
|
21
|
+
end
|
22
|
+
|
23
|
+
describe "#attribute_name" do
|
24
|
+
|
25
|
+
it "is derived from the (long) switch" do
|
26
|
+
@option.attribute_name.should == "key_file"
|
27
|
+
end
|
28
|
+
|
29
|
+
it "can be overridden" do
|
30
|
+
@option = Clamp::Option.new("--key-file", "FILE", "SSH identity", :attribute_name => "ssh_identity")
|
31
|
+
@option.attribute_name.should == "ssh_identity"
|
32
|
+
end
|
33
|
+
|
34
|
+
end
|
35
|
+
|
36
|
+
describe "#default_value" do
|
37
|
+
|
38
|
+
it "defaults to nil" do
|
39
|
+
@option.default_value.should == nil
|
40
|
+
end
|
41
|
+
|
42
|
+
it "can be overridden" do
|
43
|
+
@option = Clamp::Option.new("-n", "N", "iterations", :default => 1)
|
44
|
+
@option.default_value.should == 1
|
45
|
+
end
|
46
|
+
|
47
|
+
end
|
48
|
+
|
49
|
+
describe "#help" do
|
50
|
+
|
51
|
+
it "combines switch, type and description" do
|
52
|
+
@option.help.should == ["--key-file FILE", "SSH identity"]
|
53
|
+
end
|
54
|
+
|
55
|
+
end
|
56
|
+
|
57
|
+
end
|
58
|
+
|
59
|
+
describe "flag" do
|
60
|
+
|
61
|
+
before do
|
62
|
+
@option = Clamp::Option.new("--verbose", :flag, "Blah blah blah")
|
63
|
+
end
|
64
|
+
|
65
|
+
describe "#help" do
|
66
|
+
|
67
|
+
it "excludes option argument" do
|
68
|
+
@option.help.should == ["--verbose", "Blah blah blah"]
|
69
|
+
end
|
70
|
+
|
71
|
+
end
|
72
|
+
|
73
|
+
end
|
74
|
+
|
75
|
+
describe "negatable flag" do
|
76
|
+
|
77
|
+
before do
|
78
|
+
@option = Clamp::Option.new("--[no-]force", :flag, "Force installation")
|
79
|
+
end
|
80
|
+
|
81
|
+
it "handles both positive and negative forms" do
|
82
|
+
@option.handles?("--force").should be_true
|
83
|
+
@option.handles?("--no-force").should be_true
|
84
|
+
end
|
85
|
+
|
86
|
+
describe "#flag_value" do
|
87
|
+
|
88
|
+
it "returns true for the positive variant" do
|
89
|
+
@option.flag_value("--force").should be_true
|
90
|
+
@option.flag_value("--no-force").should be_false
|
91
|
+
end
|
92
|
+
|
93
|
+
end
|
94
|
+
|
95
|
+
describe "#attribute_name" do
|
96
|
+
|
97
|
+
it "is derived from the (long) switch" do
|
98
|
+
@option.attribute_name.should == "force"
|
99
|
+
end
|
100
|
+
|
101
|
+
end
|
102
|
+
|
103
|
+
end
|
104
|
+
|
105
|
+
describe "with both short and long switches" do
|
106
|
+
|
107
|
+
before do
|
108
|
+
@option = Clamp::Option.new(["-k", "--key-file"], "FILE", "SSH identity")
|
109
|
+
end
|
110
|
+
|
111
|
+
it "handles both switches" do
|
112
|
+
@option.handles?("--key-file").should be_true
|
113
|
+
@option.handles?("-k").should be_true
|
114
|
+
end
|
115
|
+
|
116
|
+
describe "#help" do
|
117
|
+
|
118
|
+
it "includes both switches" do
|
119
|
+
@option.help.should == ["-k, --key-file FILE", "SSH identity"]
|
120
|
+
end
|
121
|
+
|
122
|
+
end
|
123
|
+
|
124
|
+
end
|
125
|
+
|
126
|
+
describe "in subcommand" do
|
127
|
+
before do
|
128
|
+
|
129
|
+
@command = Class.new(Clamp::Command) do
|
130
|
+
subcommand "foo", "FOO!" do
|
131
|
+
option "--bar", "BAR", "Bars foo."
|
132
|
+
end
|
133
|
+
end
|
134
|
+
|
135
|
+
end
|
136
|
+
|
137
|
+
describe "Command#help" do
|
138
|
+
|
139
|
+
it "includes help for each option exactly once" do
|
140
|
+
subcommand = @command.new("").send(:find_subcommand, 'foo')
|
141
|
+
subcommand_help = subcommand.subcommand_class.help("")
|
142
|
+
subcommand_help.lines.grep(/--bar BAR/).count.should == 1
|
143
|
+
end
|
144
|
+
|
145
|
+
end
|
146
|
+
|
147
|
+
end
|
148
|
+
|
149
|
+
end
|
@@ -0,0 +1,201 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Clamp::Parameter do
|
4
|
+
|
5
|
+
describe "normal" do
|
6
|
+
|
7
|
+
before do
|
8
|
+
@parameter = Clamp::Parameter.new("COLOR", "hue of choice")
|
9
|
+
end
|
10
|
+
|
11
|
+
it "has a name" do
|
12
|
+
@parameter.name.should == "COLOR"
|
13
|
+
end
|
14
|
+
|
15
|
+
it "has a description" do
|
16
|
+
@parameter.description.should == "hue of choice"
|
17
|
+
end
|
18
|
+
|
19
|
+
describe "#attribute_name" do
|
20
|
+
|
21
|
+
it "is derived from the name" do
|
22
|
+
@parameter.attribute_name.should == "color"
|
23
|
+
end
|
24
|
+
|
25
|
+
it "can be overridden" do
|
26
|
+
@parameter = Clamp::Parameter.new("COLOR", "hue of choice", :attribute_name => "hue")
|
27
|
+
@parameter.attribute_name.should == "hue"
|
28
|
+
end
|
29
|
+
|
30
|
+
end
|
31
|
+
|
32
|
+
describe "#consume" do
|
33
|
+
|
34
|
+
it "consumes one argument" do
|
35
|
+
@arguments = %w(a b c)
|
36
|
+
@parameter.consume(@arguments).should == "a"
|
37
|
+
@arguments.should == %w(b c)
|
38
|
+
end
|
39
|
+
|
40
|
+
describe "with no arguments" do
|
41
|
+
|
42
|
+
it "raises an Argument error" do
|
43
|
+
@arguments = []
|
44
|
+
lambda do
|
45
|
+
@parameter.consume(@arguments)
|
46
|
+
end.should raise_error(ArgumentError)
|
47
|
+
end
|
48
|
+
|
49
|
+
end
|
50
|
+
|
51
|
+
end
|
52
|
+
|
53
|
+
end
|
54
|
+
|
55
|
+
describe "optional (name in square brackets)" do
|
56
|
+
|
57
|
+
before do
|
58
|
+
@parameter = Clamp::Parameter.new("[COLOR]", "hue of choice")
|
59
|
+
end
|
60
|
+
|
61
|
+
describe "#attribute_name" do
|
62
|
+
|
63
|
+
it "omits the brackets" do
|
64
|
+
@parameter.attribute_name.should == "color"
|
65
|
+
end
|
66
|
+
|
67
|
+
end
|
68
|
+
|
69
|
+
describe "#consume" do
|
70
|
+
|
71
|
+
it "consumes one argument" do
|
72
|
+
@arguments = %w(a b c)
|
73
|
+
@parameter.consume(@arguments).should == "a"
|
74
|
+
@arguments.should == %w(b c)
|
75
|
+
end
|
76
|
+
|
77
|
+
describe "with no arguments" do
|
78
|
+
|
79
|
+
it "returns nil" do
|
80
|
+
@arguments = []
|
81
|
+
@parameter.consume(@arguments).should == nil
|
82
|
+
end
|
83
|
+
|
84
|
+
end
|
85
|
+
|
86
|
+
end
|
87
|
+
|
88
|
+
end
|
89
|
+
|
90
|
+
describe "list (name followed by ellipsis)" do
|
91
|
+
|
92
|
+
before do
|
93
|
+
@parameter = Clamp::Parameter.new("FILE ...", "files to process")
|
94
|
+
end
|
95
|
+
|
96
|
+
describe "#attribute_name" do
|
97
|
+
|
98
|
+
it "indicates multiplicity" do
|
99
|
+
@parameter.attribute_name.should == "file_list"
|
100
|
+
end
|
101
|
+
|
102
|
+
end
|
103
|
+
|
104
|
+
describe "#consume" do
|
105
|
+
|
106
|
+
it "consumes all the remaining arguments" do
|
107
|
+
@arguments = %w(a b c)
|
108
|
+
@parameter.consume(@arguments).should == %w(a b c)
|
109
|
+
@arguments.should == []
|
110
|
+
end
|
111
|
+
|
112
|
+
describe "with no arguments" do
|
113
|
+
|
114
|
+
it "raises an Argument error" do
|
115
|
+
@arguments = []
|
116
|
+
lambda do
|
117
|
+
@parameter.consume(@arguments)
|
118
|
+
end.should raise_error(ArgumentError)
|
119
|
+
end
|
120
|
+
|
121
|
+
end
|
122
|
+
|
123
|
+
end
|
124
|
+
end
|
125
|
+
|
126
|
+
describe "optional list" do
|
127
|
+
|
128
|
+
before do
|
129
|
+
@parameter = Clamp::Parameter.new("[FILES] ...", "files to process")
|
130
|
+
end
|
131
|
+
|
132
|
+
describe "#attribute_name" do
|
133
|
+
|
134
|
+
it "indicates multiplicity" do
|
135
|
+
@parameter.attribute_name.should == "files_list"
|
136
|
+
end
|
137
|
+
|
138
|
+
end
|
139
|
+
|
140
|
+
describe "#default_value" do
|
141
|
+
|
142
|
+
it "is an empty list" do
|
143
|
+
@parameter.default_value.should == []
|
144
|
+
end
|
145
|
+
|
146
|
+
end
|
147
|
+
|
148
|
+
describe "#help" do
|
149
|
+
|
150
|
+
it "does not include default" do
|
151
|
+
@parameter.help_rhs.should_not include("default:")
|
152
|
+
end
|
153
|
+
|
154
|
+
end
|
155
|
+
|
156
|
+
describe "with specified default value" do
|
157
|
+
|
158
|
+
before do
|
159
|
+
@parameter = Clamp::Parameter.new("[FILES] ...", "files to process", :default => %w(a b c))
|
160
|
+
end
|
161
|
+
|
162
|
+
describe "#default_value" do
|
163
|
+
|
164
|
+
it "is that specified" do
|
165
|
+
@parameter.default_value.should == %w(a b c)
|
166
|
+
end
|
167
|
+
|
168
|
+
end
|
169
|
+
|
170
|
+
describe "#help" do
|
171
|
+
|
172
|
+
it "includes the default value" do
|
173
|
+
@parameter.help_rhs.should include("default:")
|
174
|
+
end
|
175
|
+
|
176
|
+
end
|
177
|
+
|
178
|
+
describe "#consume" do
|
179
|
+
|
180
|
+
it "consumes all the remaining arguments" do
|
181
|
+
@arguments = %w(a b c)
|
182
|
+
@parameter.consume(@arguments).should == %w(a b c)
|
183
|
+
@arguments.should == []
|
184
|
+
end
|
185
|
+
|
186
|
+
describe "with no arguments" do
|
187
|
+
|
188
|
+
it "don't override defaults" do
|
189
|
+
@arguments = []
|
190
|
+
@parameter.consume(@arguments).should == nil
|
191
|
+
end
|
192
|
+
|
193
|
+
end
|
194
|
+
|
195
|
+
end
|
196
|
+
|
197
|
+
end
|
198
|
+
|
199
|
+
end
|
200
|
+
|
201
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,45 @@
|
|
1
|
+
require "rspec"
|
2
|
+
require "clamp"
|
3
|
+
require 'stringio'
|
4
|
+
|
5
|
+
RSpec.configure do |config|
|
6
|
+
|
7
|
+
config.mock_with :rr
|
8
|
+
|
9
|
+
end
|
10
|
+
|
11
|
+
module OutputCapture
|
12
|
+
|
13
|
+
def self.included(target)
|
14
|
+
|
15
|
+
target.before do
|
16
|
+
$stdout = @out = StringIO.new
|
17
|
+
$stderr = @err = StringIO.new
|
18
|
+
end
|
19
|
+
|
20
|
+
target.after do
|
21
|
+
$stdout = STDOUT
|
22
|
+
$stderr = STDERR
|
23
|
+
end
|
24
|
+
|
25
|
+
end
|
26
|
+
|
27
|
+
def stdout
|
28
|
+
@out.string
|
29
|
+
end
|
30
|
+
|
31
|
+
def stderr
|
32
|
+
@err.string
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
module CommandFactory
|
38
|
+
|
39
|
+
def given_command(name, &block)
|
40
|
+
before do
|
41
|
+
@command = Class.new(Clamp::Command, &block).new(name)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
end
|
metadata
ADDED
@@ -0,0 +1,84 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jls-clamp
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.3.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Mike Williams
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-04-25 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
14
|
+
description: ! "Clamp provides an object-model for command-line utilities. \nIt handles
|
15
|
+
parsing of command-line options, and generation of usage help.\n"
|
16
|
+
email: mdub@dogbiscuit.org
|
17
|
+
executables: []
|
18
|
+
extensions: []
|
19
|
+
extra_rdoc_files: []
|
20
|
+
files:
|
21
|
+
- .gitignore
|
22
|
+
- .travis.yml
|
23
|
+
- Gemfile
|
24
|
+
- README.markdown
|
25
|
+
- Rakefile
|
26
|
+
- clamp.gemspec
|
27
|
+
- examples/flipflop
|
28
|
+
- examples/fubar
|
29
|
+
- examples/gitdown
|
30
|
+
- examples/speak
|
31
|
+
- lib/clamp.rb
|
32
|
+
- lib/clamp/attribute.rb
|
33
|
+
- lib/clamp/attribute_declaration.rb
|
34
|
+
- lib/clamp/command.rb
|
35
|
+
- lib/clamp/errors.rb
|
36
|
+
- lib/clamp/help.rb
|
37
|
+
- lib/clamp/option.rb
|
38
|
+
- lib/clamp/option/declaration.rb
|
39
|
+
- lib/clamp/option/parsing.rb
|
40
|
+
- lib/clamp/parameter.rb
|
41
|
+
- lib/clamp/parameter/declaration.rb
|
42
|
+
- lib/clamp/parameter/parsing.rb
|
43
|
+
- lib/clamp/subcommand.rb
|
44
|
+
- lib/clamp/subcommand/declaration.rb
|
45
|
+
- lib/clamp/subcommand/parsing.rb
|
46
|
+
- lib/clamp/version.rb
|
47
|
+
- spec/clamp/command_group_spec.rb
|
48
|
+
- spec/clamp/command_spec.rb
|
49
|
+
- spec/clamp/option_module_spec.rb
|
50
|
+
- spec/clamp/option_spec.rb
|
51
|
+
- spec/clamp/parameter_spec.rb
|
52
|
+
- spec/spec_helper.rb
|
53
|
+
homepage: http://github.com/mdub/clamp
|
54
|
+
licenses: []
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options: []
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
none: false
|
61
|
+
requirements:
|
62
|
+
- - ! '>='
|
63
|
+
- !ruby/object:Gem::Version
|
64
|
+
version: '0'
|
65
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
66
|
+
none: false
|
67
|
+
requirements:
|
68
|
+
- - ! '>='
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: '0'
|
71
|
+
requirements: []
|
72
|
+
rubyforge_project:
|
73
|
+
rubygems_version: 1.8.21
|
74
|
+
signing_key:
|
75
|
+
specification_version: 3
|
76
|
+
summary: a minimal framework for command-line utilities
|
77
|
+
test_files:
|
78
|
+
- spec/clamp/command_group_spec.rb
|
79
|
+
- spec/clamp/command_spec.rb
|
80
|
+
- spec/clamp/option_module_spec.rb
|
81
|
+
- spec/clamp/option_spec.rb
|
82
|
+
- spec/clamp/parameter_spec.rb
|
83
|
+
- spec/spec_helper.rb
|
84
|
+
has_rdoc:
|