clamp 0.0.7 → 0.0.9
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.markdown +9 -9
- data/examples/rename +2 -2
- data/examples/speak +1 -1
- data/lib/clamp/attribute_declaration.rb +38 -0
- data/lib/clamp/command.rb +15 -75
- data/lib/clamp/command/declaration.rb +15 -0
- data/lib/clamp/errors.rb +26 -0
- data/lib/clamp/{help_support.rb → help.rb} +6 -22
- data/lib/clamp/option.rb +14 -6
- data/lib/clamp/option/declaration.rb +56 -0
- data/lib/clamp/option/parsing.rb +36 -0
- data/lib/clamp/parameter.rb +63 -0
- data/lib/clamp/parameter/declaration.rb +24 -0
- data/lib/clamp/parameter/parsing.rb +30 -0
- data/lib/clamp/subcommand/declaration.rb +35 -0
- data/lib/clamp/subcommand/execution.rb +32 -0
- data/lib/clamp/version.rb +1 -1
- data/spec/clamp/command_group_spec.rb +5 -0
- data/spec/clamp/command_spec.rb +170 -92
- data/spec/clamp/option_spec.rb +3 -3
- data/spec/clamp/parameter_spec.rb +126 -0
- metadata +17 -7
- data/lib/clamp/option_support.rb +0 -75
- data/lib/clamp/subcommand_support.rb +0 -35
data/spec/clamp/option_spec.rb
CHANGED
@@ -12,8 +12,8 @@ describe Clamp::Option do
|
|
12
12
|
@option.long_switch.should == "--key-file"
|
13
13
|
end
|
14
14
|
|
15
|
-
it "has
|
16
|
-
@option.
|
15
|
+
it "has a type" do
|
16
|
+
@option.type.should == "FILE"
|
17
17
|
end
|
18
18
|
|
19
19
|
it "has a description" do
|
@@ -48,7 +48,7 @@ describe Clamp::Option do
|
|
48
48
|
|
49
49
|
describe "#help" do
|
50
50
|
|
51
|
-
it "combines switch,
|
51
|
+
it "combines switch, type and description" do
|
52
52
|
@option.help.should == ["--key-file FILE", "SSH identity"]
|
53
53
|
end
|
54
54
|
|
@@ -0,0 +1,126 @@
|
|
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
|
+
end
|
metadata
CHANGED
@@ -1,13 +1,13 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: clamp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 13
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 9
|
10
|
+
version: 0.0.9
|
11
11
|
platform: ruby
|
12
12
|
authors:
|
13
13
|
- Mike Williams
|
@@ -15,7 +15,7 @@ autorequire:
|
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-11-
|
18
|
+
date: 2010-11-11 00:00:00 +11:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
@@ -41,15 +41,24 @@ files:
|
|
41
41
|
- examples/rename
|
42
42
|
- examples/speak
|
43
43
|
- lib/clamp.rb
|
44
|
+
- lib/clamp/attribute_declaration.rb
|
44
45
|
- lib/clamp/command.rb
|
45
|
-
- lib/clamp/
|
46
|
+
- lib/clamp/command/declaration.rb
|
47
|
+
- lib/clamp/errors.rb
|
48
|
+
- lib/clamp/help.rb
|
46
49
|
- lib/clamp/option.rb
|
47
|
-
- lib/clamp/
|
48
|
-
- lib/clamp/
|
50
|
+
- lib/clamp/option/declaration.rb
|
51
|
+
- lib/clamp/option/parsing.rb
|
52
|
+
- lib/clamp/parameter.rb
|
53
|
+
- lib/clamp/parameter/declaration.rb
|
54
|
+
- lib/clamp/parameter/parsing.rb
|
55
|
+
- lib/clamp/subcommand/declaration.rb
|
56
|
+
- lib/clamp/subcommand/execution.rb
|
49
57
|
- lib/clamp/version.rb
|
50
58
|
- spec/clamp/command_group_spec.rb
|
51
59
|
- spec/clamp/command_spec.rb
|
52
60
|
- spec/clamp/option_spec.rb
|
61
|
+
- spec/clamp/parameter_spec.rb
|
53
62
|
- spec/spec_helper.rb
|
54
63
|
has_rdoc: true
|
55
64
|
homepage: http://github.com/mdub/clamp
|
@@ -89,4 +98,5 @@ test_files:
|
|
89
98
|
- spec/clamp/command_group_spec.rb
|
90
99
|
- spec/clamp/command_spec.rb
|
91
100
|
- spec/clamp/option_spec.rb
|
101
|
+
- spec/clamp/parameter_spec.rb
|
92
102
|
- spec/spec_helper.rb
|
data/lib/clamp/option_support.rb
DELETED
@@ -1,75 +0,0 @@
|
|
1
|
-
require 'clamp/option'
|
2
|
-
|
3
|
-
module Clamp
|
4
|
-
|
5
|
-
module OptionSupport
|
6
|
-
|
7
|
-
def option(switches, argument_type, description, opts = {}, &block)
|
8
|
-
option = Clamp::Option.new(switches, argument_type, description, opts)
|
9
|
-
declare_option(option, &block)
|
10
|
-
end
|
11
|
-
|
12
|
-
def has_options?
|
13
|
-
!declared_options.empty?
|
14
|
-
end
|
15
|
-
|
16
|
-
def declared_options
|
17
|
-
my_declared_options + inherited_declared_options
|
18
|
-
end
|
19
|
-
|
20
|
-
def recognised_options
|
21
|
-
declared_options + standard_options
|
22
|
-
end
|
23
|
-
|
24
|
-
def find_option(switch)
|
25
|
-
recognised_options.find { |o| o.handles?(switch) }
|
26
|
-
end
|
27
|
-
|
28
|
-
private
|
29
|
-
|
30
|
-
def my_declared_options
|
31
|
-
@my_declared_options ||= []
|
32
|
-
end
|
33
|
-
|
34
|
-
def declare_option(option, &block)
|
35
|
-
my_declared_options << option
|
36
|
-
declare_option_reader(option)
|
37
|
-
declare_option_writer(option, &block)
|
38
|
-
end
|
39
|
-
|
40
|
-
def inherited_declared_options
|
41
|
-
if superclass.respond_to?(:declared_options)
|
42
|
-
superclass.declared_options
|
43
|
-
else
|
44
|
-
[]
|
45
|
-
end
|
46
|
-
end
|
47
|
-
|
48
|
-
HELP_OPTION = Clamp::Option.new("--help", :flag, "print help", :attribute_name => :help_requested)
|
49
|
-
|
50
|
-
def standard_options
|
51
|
-
[HELP_OPTION]
|
52
|
-
end
|
53
|
-
|
54
|
-
def declare_option_reader(option)
|
55
|
-
reader_name = option.attribute_name
|
56
|
-
reader_name += "?" if option.flag?
|
57
|
-
define_method(reader_name) do
|
58
|
-
value = instance_variable_get("@#{option.attribute_name}")
|
59
|
-
value = option.default_value if value.nil?
|
60
|
-
value
|
61
|
-
end
|
62
|
-
end
|
63
|
-
|
64
|
-
def declare_option_writer(option, &block)
|
65
|
-
define_method("#{option.attribute_name}=") do |value|
|
66
|
-
if block
|
67
|
-
value = instance_exec(value, &block)
|
68
|
-
end
|
69
|
-
instance_variable_set("@#{option.attribute_name}", value)
|
70
|
-
end
|
71
|
-
end
|
72
|
-
|
73
|
-
end
|
74
|
-
|
75
|
-
end
|
@@ -1,35 +0,0 @@
|
|
1
|
-
module Clamp
|
2
|
-
|
3
|
-
class Subcommand < Struct.new(:name, :description, :subcommand_class)
|
4
|
-
|
5
|
-
def help
|
6
|
-
[name, description]
|
7
|
-
end
|
8
|
-
|
9
|
-
end
|
10
|
-
|
11
|
-
module SubcommandSupport
|
12
|
-
|
13
|
-
def recognised_subcommands
|
14
|
-
@recognised_subcommands ||= []
|
15
|
-
end
|
16
|
-
|
17
|
-
def subcommand(name, description, subcommand_class = self, &block)
|
18
|
-
if block
|
19
|
-
# generate a anonymous sub-class
|
20
|
-
subcommand_class = Class.new(subcommand_class, &block)
|
21
|
-
end
|
22
|
-
recognised_subcommands << Subcommand.new(name, description, subcommand_class)
|
23
|
-
end
|
24
|
-
|
25
|
-
def has_subcommands?
|
26
|
-
!recognised_subcommands.empty?
|
27
|
-
end
|
28
|
-
|
29
|
-
def find_subcommand(name)
|
30
|
-
recognised_subcommands.find { |sc| sc.name == name }
|
31
|
-
end
|
32
|
-
|
33
|
-
end
|
34
|
-
|
35
|
-
end
|