choosy 0.2.5 → 0.3.0

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.
Files changed (58) hide show
  1. data/CHANGELOG.md +8 -0
  2. data/LICENSE +2 -0
  3. data/README.markdown +102 -54
  4. data/Rakefile +27 -5
  5. data/TODO.md +5 -0
  6. data/examples/bar.rb +9 -7
  7. data/examples/foo.rb +9 -7
  8. data/examples/superfoo.rb +48 -36
  9. data/lib/VERSION.yml +6 -0
  10. data/lib/choosy/argument.rb +5 -0
  11. data/lib/choosy/base_command.rb +11 -10
  12. data/lib/choosy/command.rb +17 -1
  13. data/lib/choosy/converter.rb +5 -1
  14. data/lib/choosy/dsl/argument_builder.rb +33 -21
  15. data/lib/choosy/dsl/base_builder.rb +26 -0
  16. data/lib/choosy/dsl/base_command_builder.rb +23 -30
  17. data/lib/choosy/dsl/command_builder.rb +8 -12
  18. data/lib/choosy/dsl/option_builder.rb +14 -45
  19. data/lib/choosy/dsl/super_command_builder.rb +17 -20
  20. data/lib/choosy/errors.rb +1 -0
  21. data/lib/choosy/option.rb +19 -0
  22. data/lib/choosy/printing/base_printer.rb +231 -0
  23. data/lib/choosy/printing/color.rb +8 -5
  24. data/lib/choosy/printing/erb_printer.rb +1 -1
  25. data/lib/choosy/printing/help_printer.rb +49 -163
  26. data/lib/choosy/printing/manpage.rb +235 -0
  27. data/lib/choosy/printing/manpage_printer.rb +95 -0
  28. data/lib/choosy/printing/terminal.rb +39 -8
  29. data/lib/choosy/printing.rb +1 -0
  30. data/lib/choosy/super_command.rb +13 -4
  31. data/lib/choosy/super_parser.rb +5 -1
  32. data/lib/choosy/verifier.rb +8 -0
  33. data/lib/choosy/version.rb +64 -5
  34. data/spec/choosy/argument_spec.rb +28 -0
  35. data/spec/choosy/base_command_spec.rb +7 -3
  36. data/spec/choosy/command_spec.rb +2 -2
  37. data/spec/choosy/converter_spec.rb +3 -2
  38. data/spec/choosy/dsl/argument_builder_spec.rb +19 -8
  39. data/spec/choosy/dsl/base_builder_spec.rb +43 -0
  40. data/spec/choosy/dsl/base_command_builder_spec.rb +7 -9
  41. data/spec/choosy/dsl/commmand_builder_spec.rb +9 -1
  42. data/spec/choosy/dsl/option_builder_spec.rb +1 -65
  43. data/spec/choosy/dsl/super_command_builder_spec.rb +19 -8
  44. data/spec/choosy/option_spec.rb +68 -0
  45. data/spec/choosy/printing/base_printer_spec.rb +155 -0
  46. data/spec/choosy/printing/color_spec.rb +4 -0
  47. data/spec/choosy/printing/help_printer_spec.rb +15 -109
  48. data/spec/choosy/printing/manpage_printer_spec.rb +95 -0
  49. data/spec/choosy/printing/manpage_spec.rb +206 -0
  50. data/spec/choosy/super_command_spec.rb +7 -0
  51. data/spec/choosy/super_parser_spec.rb +9 -0
  52. data/spec/choosy/verifier_spec.rb +31 -1
  53. data/spec/choosy/version.yml +5 -0
  54. data/spec/choosy/version_spec.rb +87 -0
  55. data/spec/integration/command-C_spec.rb +23 -0
  56. data/spec/integration/supercommand-C_spec.rb +45 -0
  57. metadata +81 -78
  58. data/lib/VERSION +0 -1
@@ -0,0 +1,95 @@
1
+ require 'spec_helpers'
2
+ require 'choosy/command'
3
+ require 'choosy/printing/manpage_printer'
4
+
5
+ module Choosy::Printing
6
+ describe ManpagePrinter do
7
+ before :each do
8
+ @man = ManpagePrinter.new
9
+ @cmd = Choosy::Command.new(:manpage)
10
+ end
11
+
12
+ def output
13
+ @man.manpage.buffer.join("\n")
14
+ end
15
+
16
+ describe "for the name segment" do
17
+ it "should not print anything if no summary is present" do
18
+ @man.format_name(@cmd)
19
+ output.should eql('')
20
+ end
21
+
22
+ it "should print out the summary" do
23
+ @cmd.alter do
24
+ summary 'summary goes here'
25
+ end
26
+ @man.format_name(@cmd)
27
+ output.should eql('.SH "NAME"
28
+ manpage \\- summary goes here')
29
+ end
30
+ end
31
+
32
+ describe "for the synopsis" do
33
+ it "should add the synopsis correctly" do
34
+ @cmd.alter do
35
+ boolean :bold, "bold"
36
+ integer :long_option_goes_here, "long"
37
+ integer :shorter_option, "short"
38
+ arguments do
39
+ metaname '[ARGS+++]'
40
+ end
41
+ end
42
+ @man.columns = 60
43
+ @man.format_synopsis(@cmd)
44
+ output.should eql('.SH "SYNOPSIS"
45
+ .nf
46
+ manpage [\\-b|\\-\\-bold]
47
+ [\\-l|\\-\\-long\\-option\\-goes\\-here=LONG_OPTION_GOES_HERE]
48
+ [\\-s|\\-\\-shorter\\-option=SHORTER_OPTION] [ARGS+++]
49
+ .fi')
50
+ end
51
+ end
52
+
53
+ it "should format an option" do
54
+ o = @cmd.builder.integer :opt, 'option line here.'
55
+ @man.format_option(o, @man.regular_option(o), ' ')
56
+ output.should eql('.TP 8
57
+ \\fI\\-o\\fP, \\fI\\-\\-opt\\fP OPT
58
+ option line here&.')
59
+ end
60
+
61
+ it "should format a command" do
62
+ @cmd.alter do
63
+ summary 'this is a summary'
64
+ end
65
+ @man.format_command(@cmd, 'cmd', ' ')
66
+ output.should eql('.TP 4
67
+ cmd
68
+ this is a summary')
69
+ end
70
+
71
+ it "should format a heading correctly" do
72
+ @cmd.alter do
73
+ header 'here'
74
+ end
75
+ @man.format_element(@cmd.listing[0])
76
+ output.should eql('.SH "HERE"')
77
+ end
78
+
79
+ it "should format a regular paragraph" do
80
+ @cmd.alter do
81
+ para 'paragraph'
82
+ end
83
+ @man.format_element(@cmd.listing[0])
84
+ output.should eql(".P\nparagraph")
85
+ end
86
+
87
+ it "should format an empty paragraph" do
88
+ @cmd.alter do
89
+ para
90
+ end
91
+ @man.format_element(@cmd.listing[0])
92
+ output.should eql('.P')
93
+ end
94
+ end
95
+ end
@@ -0,0 +1,206 @@
1
+ require 'choosy/printing/manpage'
2
+ require 'spec_helpers'
3
+
4
+ module Choosy::Printing
5
+ describe ManpageFormatter do
6
+ before :each do
7
+ @format = ManpageFormatter.new
8
+ end
9
+
10
+ it "should format bold" do
11
+ @format.bold("this").should eql('\\fBthis\\fP')
12
+ end
13
+
14
+ it "should format simple bold" do
15
+ @format.bold.should eql('\\fB')
16
+ end
17
+
18
+ it "should format italics" do
19
+ @format.italics('this').should eql('\\fIthis\\fP')
20
+ end
21
+
22
+ it "should format simple italics" do
23
+ @format.italics.should eql('\\fI')
24
+ end
25
+
26
+ it "should format roman" do
27
+ @format.roman('this').should eql('\\fRthis\\fP')
28
+ end
29
+
30
+ it "should format simple roman" do
31
+ @format.roman.should eql('\\fR')
32
+ end
33
+ end
34
+
35
+ describe Manpage do
36
+ before :each do
37
+ @man = Manpage.new
38
+ end
39
+
40
+ it "should format the frame outlines correctly" do
41
+ @man.name = 'named'
42
+ @man.date = 'today'
43
+ @man.version = 'version'
44
+ @man.manual = 'manual'
45
+
46
+ @man.frame_outline.should eql('.TH "named" "1" "today" "version" "manual"')
47
+ end
48
+
49
+ it "should format a section heading correctly" do
50
+ @man.section_heading("Header:").should eql(%Q{.SH "HEADER"})
51
+ end
52
+
53
+ it "should format a sub-section heading correctly" do
54
+ @man.subsection_heading('Sub-Section:').should eql(%Q{.SS "SUB\\-SECTION"})
55
+ end
56
+
57
+ it "should add a paragraph break" do
58
+ @man.paragraph.should eql(".P")
59
+ end
60
+
61
+ it "should attach the line after the paragraph" do
62
+ @man.paragraph("some text").should eql(".P\nsome text")
63
+ end
64
+
65
+ it "should handle indented paragraphs" do
66
+ @man.indented_paragraph("this is a", "goes here").should eql(%Q{.IP "this is a"\ngoes here})
67
+ end
68
+
69
+ it "should handle hanging paragraphs" do
70
+ @man.hanging_paragraph("this is a paragraph").should eql(%Q{.HP\nthis is a paragraph})
71
+ end
72
+
73
+ it "should handle an indented region" do
74
+ @man.indented_region.should eql('.RE')
75
+ end
76
+
77
+ describe :bold do
78
+ it "should allow for bolded text" do
79
+ @man.bold("line").should eql('.B "line"')
80
+ end
81
+
82
+ it "should allow for bold alternating with italics" do
83
+ @man.bold("line", :italics).should eql('.BI "line"')
84
+ end
85
+
86
+ it "should allow for bold alternating with roman" do
87
+ @man.bold("line", :roman).should eql('.BR "line"')
88
+ end
89
+
90
+ it "should fail on unrecognized type" do
91
+ attempting {
92
+ @man.bold("line", :blah)
93
+ }.should raise_error(Choosy::ConfigurationError, /bold/)
94
+ end
95
+ end
96
+
97
+ describe :italics do
98
+ it "should allow for italicized text" do
99
+ @man.italics("line").should eql('.I "line"')
100
+ end
101
+
102
+ it "should allow for alternating bold" do
103
+ @man.italics("line", :bold).should eql('.IB "line"')
104
+ end
105
+
106
+ it "should allow for alternating roman" do
107
+ @man.italics("line", :roman).should eql('.IR "line"')
108
+ end
109
+
110
+ it "should fail on unrecognized type" do
111
+ attempting {
112
+ @man.italics("line", :blah)
113
+ }.should raise_error(Choosy::ConfigurationError, /italics/)
114
+ end
115
+ end
116
+
117
+ describe :roman do
118
+ it "should allow for alternating bold text" do
119
+ @man.roman("line", :bold).should eql('.RB "line"')
120
+ end
121
+
122
+ it 'should allow for alternating italics text' do
123
+ @man.roman("line", :italics).should eql('.RI "line"')
124
+ end
125
+
126
+ it "should fail when the type isn't given" do
127
+ attempting {
128
+ @man.roman("line", nil)
129
+ }.should raise_error(Choosy::ConfigurationError, /roman/)
130
+ end
131
+ end
132
+
133
+ describe :small do
134
+ it "should allow for small text" do
135
+ @man.small("this").should eql('.SM "this"')
136
+ end
137
+
138
+ it "should allow for small bold text" do
139
+ @man.small("this", :bold).should eql('.SB "this"')
140
+ end
141
+
142
+ it "should fail when not bold" do
143
+ attempting {
144
+ @man.small("this", :blah)
145
+ }.should raise_error(Choosy::ConfigurationError, /small/)
146
+ end
147
+ end
148
+
149
+ it "should allow for comments" do
150
+ @man.comment("here").should eql('./" here')
151
+ end
152
+
153
+ it "should allow for line breaks" do
154
+ @man.line_break.should eql('.br')
155
+ end
156
+
157
+ it "should allow for nofill" do
158
+ @man.nofill.should eql('.nf')
159
+ end
160
+
161
+ it "should allow for blocks of no-fill" do
162
+ @man.nofill do |man|
163
+ man.text 'here'
164
+ end
165
+ @man.buffer.join("\n").should eql(".nf\nhere\n.fi")
166
+ end
167
+
168
+ it "should allow for fill" do
169
+ @man.fill.should eql('.fi')
170
+ end
171
+
172
+ describe :term_paragraph do
173
+ it "should allow for a default width within the term" do
174
+ @man.term_paragraph("this", "that").should eql(%Q{.TP 5\nthis\nthat})
175
+ end
176
+
177
+ it "should allow you to set the first column width" do
178
+ @man.term_paragraph("this", "that", 10).should eql(%Q{.TP 10\nthis\nthat})
179
+ end
180
+ end
181
+
182
+ describe :to_s do
183
+ it "should write out everything to a string" do
184
+ @man.name = "blah"
185
+ @man.date = 'today'
186
+
187
+ @man.section_heading('description')
188
+ @man.paragraph('this is a line of text')
189
+
190
+ @man.to_s.should eql(<<EOF
191
+ .TH "blah" "1" "today" " " " "
192
+ .ie \\n(.g .ds Aq \\(aq
193
+ .el .ds Aq '
194
+ .\\" disable hyphenation
195
+ .nh
196
+ .\\" disable justification (adjust text to left margin only)
197
+ .ad l
198
+ .SH "DESCRIPTION"
199
+ .P
200
+ this is a line of text
201
+ EOF
202
+ )
203
+ end
204
+ end
205
+ end
206
+ end
@@ -76,5 +76,12 @@ module Choosy
76
76
  count.should eql(5)
77
77
  end
78
78
  end#execute!
79
+
80
+ describe :finalize! do
81
+ it "should set the metaname if not already set" do
82
+ @c.finalize!
83
+ @c.metaname.should eql('COMMAND')
84
+ end
85
+ end#finalize!
79
86
  end
80
87
  end
@@ -73,6 +73,15 @@ module Choosy
73
73
  @p.parse!()
74
74
  }.should raise_error(Choosy::HelpCalled, Choosy::DSL::SuperCommandBuilder::SUPER)
75
75
  end
76
+
77
+ it "should push the default command on the stack to parse" do
78
+ @p.command :bar
79
+ @p.super.alter do
80
+ default :bar
81
+ end
82
+
83
+ @p.parse!().subresults.should have(1).item
84
+ end
76
85
  end
77
86
 
78
87
  describe "with subcommands" do
@@ -12,7 +12,7 @@ module Choosy
12
12
  end
13
13
 
14
14
  def v
15
- @c.builder.finalize!
15
+ @c.finalize!
16
16
  Verifier.new
17
17
  end
18
18
 
@@ -233,6 +233,16 @@ module Choosy
233
233
  }.should raise_error(RuntimeError, 'Called!')
234
234
  end
235
235
 
236
+ it "should convert the arguments to the requested type" do
237
+ b.arguments do
238
+ cast :integer
239
+ end
240
+ @res.args.push("1", "2", "3")
241
+
242
+ v.verify_arguments!(@res)
243
+ @res.args.should eql([1, 2, 3])
244
+ end
245
+
236
246
  it "should validate that the argument count isn't too few" do
237
247
  b.arguments do
238
248
  count 2
@@ -273,6 +283,26 @@ module Choosy
273
283
  v.verify_arguments!(@res)
274
284
  }.should raise_error(Choosy::ValidationError, /no arguments allowed/)
275
285
  end
286
+
287
+ it "should fail when the the arguments aren't in the list" do
288
+ b.arguments do
289
+ only :tomcat, :apache
290
+ end
291
+ @res.args << "this"
292
+ attempting {
293
+ v.verify_arguments!(@res)
294
+ }.should raise_error(Choosy::ValidationError, /unrecognized value/)
295
+ end
296
+
297
+ it "should allow multiple of the same command" do
298
+ b.arguments do
299
+ only :tomcat, :apache
300
+ end
301
+ @res.args.push(:apache, :tomcat, :apache)
302
+ attempting {
303
+ v.verify_arguments!(@res)
304
+ }.should_not raise_error
305
+ end
276
306
  end
277
307
  end
278
308
  end
@@ -0,0 +1,5 @@
1
+ version:
2
+ major: 5
3
+ minor: 4
4
+ tiny: 3
5
+ date: 'today'
@@ -0,0 +1,87 @@
1
+ require 'spec_helpers'
2
+ require 'tempfile'
3
+ require 'choosy/version'
4
+
5
+ module Choosy
6
+ describe Version do
7
+ before :each do
8
+ version_file = File.join(File.dirname(__FILE__), 'version.yml')
9
+ @version = Version.new(version_file)
10
+ end
11
+
12
+ describe "should be able to read" do
13
+ it "the major version" do
14
+ @version.major.should eql(5)
15
+ end
16
+
17
+ it "the minor version" do
18
+ @version.minor.should eql(4)
19
+ end
20
+
21
+ it "the tiny verison" do
22
+ @version.tiny.should eql(3)
23
+ end
24
+
25
+ it "the date" do
26
+ @version.date.should eql('today')
27
+ end
28
+ end
29
+
30
+ describe "while altering the config file" do
31
+ before :each do
32
+ @tmp = Tempfile.new('version.yml')
33
+ @version.version_file = @tmp.path
34
+ end
35
+
36
+ after :each do
37
+ @tmp.delete
38
+ end
39
+
40
+ it "should bump the major rev" do
41
+ @version.version!(:major)
42
+ @version.reload!
43
+ @version.major.should eql(6)
44
+ end
45
+
46
+ it "should bump the minor rev" do
47
+ @version.version!(:minor)
48
+ @version.reload!
49
+ @version.major.should eql(5)
50
+ end
51
+
52
+ it "should bump the tiny rev" do
53
+ @version.version!(:tiny)
54
+ @version.reload!
55
+ @version.tiny.should eql(4)
56
+ end
57
+
58
+ it "should reset the tiny version when minor is bumped" do
59
+ @version.version!(:minor)
60
+ @version.reload!
61
+ @version.tiny.should eql(0)
62
+ end
63
+
64
+ it "should reset the minor version when major is bumped" do
65
+ @version.version!(:major)
66
+ @version.reload!
67
+ @version.minor.should eql(0)
68
+ end
69
+
70
+ it "should reset the tiny version when major is bumped" do
71
+ @version.version!(:major)
72
+ @version.reload!
73
+ @version.tiny.should eql(0)
74
+ end
75
+
76
+ it "should reset the date" do
77
+ now = Time.now
78
+ @version.version!(:major)
79
+ @version.reload!
80
+ day, month, year = @version.date.split(/\//).map{|i|i.to_i}
81
+ now.day.should be(day)
82
+ now.month.should eql(month)
83
+ now.year.should eql(year)
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,23 @@
1
+ require 'choosy'
2
+ require 'spec_helpers'
3
+
4
+ describe "Command C" do
5
+ before :each do
6
+ @cmd = Choosy::Command.new :foo do
7
+ arguments do
8
+ only :tomcat, :apache
9
+ end
10
+ end
11
+ end
12
+
13
+ it "should only allow for given arguments" do
14
+ attempting {
15
+ @cmd.parse!(['tomcat', 'no-arg'], true)
16
+ }.should raise_error(Choosy::ValidationError, /unrecognized value/)
17
+ end
18
+
19
+ it "should allow a number of arguments" do
20
+ result = @cmd.parse!(['tomcat', 'apache', 'tomcat'])
21
+ result.args.should eql([:tomcat, :apache, :tomcat])
22
+ end
23
+ end
@@ -0,0 +1,45 @@
1
+ require 'spec_helpers'
2
+ require 'choosy'
3
+
4
+ describe "SuperCommand C" do
5
+ class SuperCommandC
6
+ def super_command
7
+ Choosy::SuperCommand.new :super do
8
+
9
+ command subA
10
+ command subB
11
+
12
+ boolean :option, "Option" do
13
+ desc option_name
14
+ end
15
+ end
16
+ end
17
+
18
+ def subA
19
+ Choosy::Command.new :subA do
20
+ string :sub_option, "Sub option" do
21
+ desc option_name
22
+ end
23
+ end
24
+ end
25
+
26
+ def subB
27
+ Choosy::Command.new :subB do
28
+ end
29
+ end
30
+
31
+ def option_name
32
+ "an option name"
33
+ end
34
+ end
35
+
36
+ it "should set the option name correctly" do
37
+ sup = SuperCommandC.new.super_command
38
+ sup.option_builders[:option].entity.description.should eql("an option name")
39
+ end
40
+
41
+ it "should set the sub-option name correctly" do
42
+ sup = SuperCommandC.new.super_command
43
+ sup.command_builders[:subA].entity.option_builders[:sub_option].entity.description.should eql("an option name")
44
+ end
45
+ end