clamp 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/lib/clamp/help.rb +49 -20
- data/lib/clamp/version.rb +1 -1
- data/spec/clamp/command_group_spec.rb +6 -1
- data/spec/clamp/command_spec.rb +25 -2
- metadata +4 -4
data/lib/clamp/help.rb
CHANGED
@@ -33,33 +33,62 @@ module Clamp
|
|
33
33
|
end
|
34
34
|
|
35
35
|
def help(invocation_path)
|
36
|
-
help =
|
37
|
-
help.
|
38
|
-
|
39
|
-
|
36
|
+
help = Builder.new
|
37
|
+
help.add_usage(invocation_path, usage_descriptions)
|
38
|
+
help.add_description(description)
|
39
|
+
if has_parameters?
|
40
|
+
help.add_list("Parameters", parameters)
|
40
41
|
end
|
41
|
-
if
|
42
|
-
help.
|
43
|
-
help.puts description.gsub(/^/, " ")
|
42
|
+
if has_subcommands?
|
43
|
+
help.add_list("Subcommands", recognised_subcommands)
|
44
44
|
end
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
45
|
+
help.add_list("Options", recognised_options)
|
46
|
+
help.string
|
47
|
+
end
|
48
|
+
|
49
|
+
class Builder
|
50
|
+
|
51
|
+
def initialize
|
52
|
+
@out = StringIO.new
|
53
|
+
end
|
54
|
+
|
55
|
+
def string
|
56
|
+
@out.string
|
57
|
+
end
|
58
|
+
|
59
|
+
def add_usage(invocation_path, usage_descriptions)
|
60
|
+
puts "Usage:"
|
61
|
+
usage_descriptions.each do |usage|
|
62
|
+
puts " #{invocation_path} #{usage}".rstrip
|
50
63
|
end
|
51
64
|
end
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
65
|
+
|
66
|
+
def add_description(description)
|
67
|
+
if description
|
68
|
+
puts ""
|
69
|
+
puts description.gsub(/^/, " ")
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
DETAIL_FORMAT = " %-29s %s"
|
74
|
+
|
75
|
+
def add_list(heading, items)
|
76
|
+
puts "\n#{heading}:"
|
77
|
+
items.each do |item|
|
78
|
+
label, description = item.help
|
79
|
+
description.each_line do |line|
|
80
|
+
puts DETAIL_FORMAT % [label, line]
|
81
|
+
label = ''
|
82
|
+
end
|
56
83
|
end
|
57
84
|
end
|
58
|
-
|
59
|
-
|
60
|
-
|
85
|
+
|
86
|
+
private
|
87
|
+
|
88
|
+
def puts(*args)
|
89
|
+
@out.puts(*args)
|
61
90
|
end
|
62
|
-
|
91
|
+
|
63
92
|
end
|
64
93
|
|
65
94
|
end
|
data/lib/clamp/version.rb
CHANGED
@@ -15,7 +15,7 @@ describe Clamp::Command do
|
|
15
15
|
end
|
16
16
|
end
|
17
17
|
|
18
|
-
subcommand "flop", "flop it" do
|
18
|
+
subcommand "flop", "flop it\nfor extra flop" do
|
19
19
|
def execute
|
20
20
|
puts "FLOPPED"
|
21
21
|
end
|
@@ -42,6 +42,11 @@ describe Clamp::Command do
|
|
42
42
|
@help.should =~ /flop +flop it/
|
43
43
|
end
|
44
44
|
|
45
|
+
it "handles new lines in subcommand descriptions" do
|
46
|
+
@help = @command.help
|
47
|
+
@help.should =~ /flop +flop it\n +for extra flop/
|
48
|
+
end
|
49
|
+
|
45
50
|
end
|
46
51
|
|
47
52
|
end
|
data/spec/clamp/command_spec.rb
CHANGED
@@ -135,7 +135,7 @@ describe Clamp::Command do
|
|
135
135
|
before do
|
136
136
|
@command.class.option ["-f", "--flavour"], "FLAVOUR", "Flavour of the month"
|
137
137
|
@command.class.option ["-c", "--color"], "COLOR", "Preferred hue"
|
138
|
-
@command.class.option ["-n", "--[no-]nuts"], :flag, "Nuts (or not)"
|
138
|
+
@command.class.option ["-n", "--[no-]nuts"], :flag, "Nuts (or not)\nMay include nuts"
|
139
139
|
@command.class.parameter "[ARG] ...", "extra arguments", :attribute_name => :arguments
|
140
140
|
end
|
141
141
|
|
@@ -301,6 +301,10 @@ describe Clamp::Command do
|
|
301
301
|
@command.help.should =~ %r(--flavour FLAVOUR +Flavour of the month)
|
302
302
|
@command.help.should =~ %r(--color COLOR +Preferred hue)
|
303
303
|
end
|
304
|
+
|
305
|
+
it "handles new lines in option descriptions" do
|
306
|
+
@command.help.should =~ %r(--\[no-\]nuts +Nuts \(or not\)\n +May include nuts)
|
307
|
+
end
|
304
308
|
|
305
309
|
end
|
306
310
|
|
@@ -430,7 +434,7 @@ describe Clamp::Command do
|
|
430
434
|
describe "with parameters declared" do
|
431
435
|
|
432
436
|
before do
|
433
|
-
@command.class.parameter "X", "x"
|
437
|
+
@command.class.parameter "X", "x\nxx"
|
434
438
|
@command.class.parameter "Y", "y"
|
435
439
|
@command.class.parameter "[Z]", "z", :default => "ZZZ"
|
436
440
|
end
|
@@ -484,6 +488,25 @@ describe Clamp::Command do
|
|
484
488
|
|
485
489
|
end
|
486
490
|
|
491
|
+
describe "#help" do
|
492
|
+
|
493
|
+
it "indicates that there are parameters" do
|
494
|
+
@command.help.should include("cmd [OPTIONS] X Y [Z]")
|
495
|
+
end
|
496
|
+
|
497
|
+
it "includes parameter details" do
|
498
|
+
@command.help.should =~ %r(X +x)
|
499
|
+
@command.help.should =~ %r(Y +y)
|
500
|
+
@command.help.should =~ %r(\[Z\] +z \(default: "ZZZ"\))
|
501
|
+
end
|
502
|
+
|
503
|
+
it "handles new lines in option descriptions" do
|
504
|
+
@command.help.should =~ %r(X +x\n +xx)
|
505
|
+
end
|
506
|
+
|
507
|
+
end
|
508
|
+
|
509
|
+
|
487
510
|
end
|
488
511
|
|
489
512
|
describe "with explicit usage" do
|
metadata
CHANGED
@@ -2,7 +2,7 @@
|
|
2
2
|
name: clamp
|
3
3
|
version: !ruby/object:Gem::Version
|
4
4
|
prerelease:
|
5
|
-
version: 0.2.
|
5
|
+
version: 0.2.3
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
8
8
|
- Mike Williams
|
@@ -10,7 +10,7 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
|
13
|
-
date: 2011-07-
|
13
|
+
date: 2011-07-31 00:00:00 Z
|
14
14
|
dependencies: []
|
15
15
|
|
16
16
|
description: |
|
@@ -69,7 +69,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
69
69
|
requirements:
|
70
70
|
- - ">="
|
71
71
|
- !ruby/object:Gem::Version
|
72
|
-
hash:
|
72
|
+
hash: 1591530698992906617
|
73
73
|
segments:
|
74
74
|
- 0
|
75
75
|
version: "0"
|
@@ -78,7 +78,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
78
78
|
requirements:
|
79
79
|
- - ">="
|
80
80
|
- !ruby/object:Gem::Version
|
81
|
-
hash:
|
81
|
+
hash: 1591530698992906617
|
82
82
|
segments:
|
83
83
|
- 0
|
84
84
|
version: "0"
|