choosy 0.1.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 (42) hide show
  1. data/Gemfile +7 -0
  2. data/Gemfile.lock +25 -0
  3. data/LICENSE +23 -0
  4. data/README.markdown +393 -0
  5. data/Rakefile +57 -0
  6. data/lib/VERSION +1 -0
  7. data/lib/choosy/base_command.rb +59 -0
  8. data/lib/choosy/command.rb +32 -0
  9. data/lib/choosy/converter.rb +115 -0
  10. data/lib/choosy/dsl/base_command_builder.rb +172 -0
  11. data/lib/choosy/dsl/command_builder.rb +43 -0
  12. data/lib/choosy/dsl/option_builder.rb +155 -0
  13. data/lib/choosy/dsl/super_command_builder.rb +41 -0
  14. data/lib/choosy/errors.rb +11 -0
  15. data/lib/choosy/option.rb +22 -0
  16. data/lib/choosy/parse_result.rb +64 -0
  17. data/lib/choosy/parser.rb +184 -0
  18. data/lib/choosy/printing/color.rb +101 -0
  19. data/lib/choosy/printing/erb_printer.rb +23 -0
  20. data/lib/choosy/printing/help_printer.rb +174 -0
  21. data/lib/choosy/super_command.rb +77 -0
  22. data/lib/choosy/super_parser.rb +81 -0
  23. data/lib/choosy/verifier.rb +62 -0
  24. data/lib/choosy/version.rb +12 -0
  25. data/lib/choosy.rb +16 -0
  26. data/spec/choosy/base_command_spec.rb +11 -0
  27. data/spec/choosy/command_spec.rb +51 -0
  28. data/spec/choosy/converter_spec.rb +145 -0
  29. data/spec/choosy/dsl/base_command_builder_spec.rb +328 -0
  30. data/spec/choosy/dsl/commmand_builder_spec.rb +80 -0
  31. data/spec/choosy/dsl/option_builder_spec.rb +386 -0
  32. data/spec/choosy/dsl/super_command_builder_spec.rb +83 -0
  33. data/spec/choosy/parser_spec.rb +275 -0
  34. data/spec/choosy/printing/color_spec.rb +74 -0
  35. data/spec/choosy/printing/help_printer_spec.rb +117 -0
  36. data/spec/choosy/super_command_spec.rb +80 -0
  37. data/spec/choosy/super_parser_spec.rb +106 -0
  38. data/spec/choosy/verifier_spec.rb +180 -0
  39. data/spec/integration/command-A_spec.rb +37 -0
  40. data/spec/integration/supercommand-A_spec.rb +61 -0
  41. data/spec/spec_helpers.rb +30 -0
  42. metadata +150 -0
@@ -0,0 +1,180 @@
1
+ require 'spec_helpers'
2
+ require 'choosy/verifier'
3
+ require 'choosy/errors'
4
+ require 'choosy/parser'
5
+ require 'choosy/command'
6
+
7
+ module Choosy
8
+
9
+ module VerifierHelper
10
+ def reset!
11
+ @c = Command.new(:verifier)
12
+ end
13
+
14
+ def v
15
+ @c.builder.finalize!
16
+ Verifier.new
17
+ end
18
+
19
+ def b
20
+ @c.builder
21
+ end
22
+ end
23
+
24
+ describe Verifier do
25
+ include VerifierHelper
26
+
27
+ before :each do
28
+ reset!
29
+ @res = ParseResult.new(@c)
30
+ end
31
+
32
+ describe :verify_options! do
33
+ it "should not try to validate arguments if not set" do
34
+ b.boolean :debug, "Debug"
35
+ @res.args << "a"
36
+ attempting {
37
+ v.verify_options!(@res)
38
+ }.should_not raise_error
39
+ end
40
+ end
41
+
42
+ describe :verify_arguments! do
43
+ it "should validate arguments if asked" do
44
+ b.arguments do |args|
45
+ raise RuntimeError.new('Called!')
46
+ end
47
+
48
+ attempting {
49
+ v.verify_arguments!(@res)
50
+ }.should raise_error(RuntimeError, 'Called!')
51
+ end
52
+ end
53
+
54
+ describe :populate! do
55
+ it "should fill in default boolean values to false if unset" do
56
+ o = b.boolean :debug, "Debug"
57
+ v.populate!(o, @res)
58
+ @res.options.should eql({:debug => false})
59
+ end
60
+
61
+ it "should fill in the default boolean to true if set to true" do
62
+ o = b.boolean :verbose, "Verbose", :default => true
63
+ v.populate!(o, @res)
64
+ @res.options.should eql({:verbose => true})
65
+ end
66
+
67
+ it "should set the default of multi-arg options to []" do
68
+ o = b.strings :words, "Words"
69
+ v.populate!(o, @res)
70
+ @res.options.should eql({:words => []})
71
+ end
72
+
73
+ it "should set the default value for other options to nil if empty" do
74
+ o = b.string :line, "Line"
75
+ v.populate!(o, @res)
76
+ @res.options.should eql({:line => nil})
77
+ end
78
+
79
+ it "should set the default value for other options if set" do
80
+ o = b.string :line, "Line", :default => "line!"
81
+ v.populate!(o, @res)
82
+ @res.options.should eql({:line => "line!"})
83
+ end
84
+
85
+ it "should not populate the default help option" do
86
+ o = b.help
87
+ v.populate!(o, @res)
88
+ @res.options.should be_empty
89
+ end
90
+
91
+ it "should not populate the default version option" do
92
+ o = b.version "blah"
93
+ v.populate!(o, @res)
94
+ @res.options.should be_empty
95
+ end
96
+ end#populate_defaults!
97
+
98
+ describe :validate! do
99
+ it "should call the validate proc associated with each option" do
100
+ o = b.string :line, "Line" do |l|
101
+ l.validate do |arg|
102
+ l.fail "Validated!"
103
+ end
104
+ end
105
+ @res[:line] = "line"
106
+
107
+ attempting {
108
+ v.validate!(o, @res)
109
+ }.should raise_error(Choosy::ValidationError, /Validated!/)
110
+ end
111
+
112
+ it "should not call the proc on empty arguments" do
113
+ o = b.strings :line, "Line" do |l|
114
+ l.validate do |arg|
115
+ l.fail "Validated!"
116
+ end
117
+ end
118
+ @res[:line] = []
119
+
120
+ v.validate!(o, @res)
121
+ @res[:line].should eql([])
122
+ end
123
+
124
+ it "should not call the proc when the arguments are null" do
125
+ o = b.string :line, "Line" do |l|
126
+ l.validate do |arg|
127
+ l.fail "Validated!"
128
+ end
129
+ end
130
+ @res[:line] = nil
131
+
132
+ v.validate!(o, @res)
133
+ @res[:line].should be(nil)
134
+ end
135
+ end#validate!
136
+
137
+ describe :required? do
138
+ it "should fail when an option is required but not provided" do
139
+ o = b.string :str, "String" do |s|
140
+ s.required
141
+ end
142
+ attempting {
143
+ v.required?(o, @res)
144
+ }.should raise_error(Choosy::ValidationError, /Required/)
145
+ end
146
+
147
+ it "should succeed when nothing is required" do
148
+ o = b.string :str, "String"
149
+ attempting {
150
+ v.required?(o, @res)
151
+ }.should_not raise_error
152
+ end
153
+ end
154
+
155
+ describe :convert! do
156
+ it "should convert files" do
157
+ o = b.file :afile, "A File"
158
+ @res[:afile] = __FILE__
159
+ v.convert!(o, @res)
160
+ @res[:afile].path.should eql(__FILE__)
161
+ end
162
+
163
+ class CustomConverter
164
+ def convert(value)
165
+ value.to_i
166
+ end
167
+ end
168
+
169
+ it "should convert a custom type" do
170
+ o = b.single :an_int, "An int" do |i|
171
+ i.cast CustomConverter.new
172
+ end
173
+ @res[:an_int] = "1"
174
+
175
+ v.convert!(o, @res)
176
+ @res[:an_int].should eql(1)
177
+ end
178
+ end#convert!
179
+ end
180
+ end
@@ -0,0 +1,37 @@
1
+ require 'spec_helpers'
2
+ require 'choosy'
3
+
4
+ describe "Command A" do
5
+
6
+ before :each do
7
+ @cmd = Choosy::Command.new :A do |a|
8
+ a.integer :count, "The count"
9
+ a.boolean :bold, "Bold the output"
10
+ a.version "blah"
11
+ a.help
12
+ end
13
+ end
14
+
15
+ it "should print a help message" do
16
+ o = capture :stdout do
17
+ @cmd.parse! ['--help']
18
+ end
19
+
20
+ o.should match /USAGE:/
21
+ end
22
+
23
+ it "should handle multiple arguments" do
24
+ result = @cmd.parse! ['--bold', '-c', '5']
25
+ result.options.should eql({:bold => true, :count => 5})
26
+ result.args.should be_empty
27
+ result.unparsed.should be_empty
28
+ end
29
+
30
+ it "should print out the version number" do
31
+ o = capture :stdout do
32
+ @cmd.parse! ['--count', '5', '--version']
33
+ end
34
+
35
+ o.should eql("blah\n")
36
+ end
37
+ end
@@ -0,0 +1,61 @@
1
+ require 'spec_helpers'
2
+ require 'choosy'
3
+
4
+ describe "SuperCommand A" do
5
+ before :each do
6
+ @cmd = Choosy::SuperCommand.new :superfoo do |foo|
7
+ foo.desc "This is a supercommand of a definite size"
8
+ foo.separator 'Commands:'
9
+
10
+ foo.command :bar do |bar|
11
+ bar.summary "This command is a bar"
12
+ bar.desc "This is a description of this subcommand"
13
+ bar.string :favorite_pet, "Your favorite pet."
14
+ bar.boolean :Fuzzy, "Is your pet fuzzy?"
15
+ end
16
+
17
+ foo.command :baz do |baz|
18
+ baz.summary "This is a baz command"
19
+ baz.desc "This is a description of baz"
20
+ baz.boolean :accountant, "Your accountant who helps you cheat on your taxes"
21
+ baz.integer :amount, "How much money do you save?"
22
+ end
23
+
24
+ foo.help
25
+
26
+ foo.separator 'Options:'
27
+ foo.version "1.ohyeah"
28
+ end
29
+ end
30
+ =begin
31
+ it "should fail when a command is not set" do
32
+ o = capture :stderr do
33
+ @cmd.parse! ['blah']
34
+ end
35
+
36
+ o.should match(/^superfoo: 'blah' is not a standard command/)
37
+ end
38
+
39
+ it "should fail when parsing a non-existent option" do
40
+ o = capture :stderr do
41
+ @cmd.parse! ['--non-option']
42
+ end
43
+
44
+ o.should match(/^superfoo: '--non-option' is not a standard/)
45
+ end
46
+
47
+ it "should print the version with the given global flag" do
48
+ o = capture :stdout do
49
+ @cmd.parse! ['--version']
50
+ end
51
+
52
+ o.should eql("1.ohyeah\n")
53
+ end
54
+
55
+ it "should correctly parse the bar command" do
56
+ results = @cmd.parse! ['bar', '--favorite-pet', 'Blue']
57
+ results.should have(1).item
58
+ result[0].options.should eql({:favorite_pet => 'Blue', :Fuzzy => false})
59
+ end
60
+ =end
61
+ end
@@ -0,0 +1,30 @@
1
+
2
+ def attempting(&block)
3
+ lambda &block
4
+ end
5
+
6
+ def attempting_to(&block)
7
+ lambda &block
8
+ end
9
+
10
+ # Ammended from: https://github.com/cldwalker/hirb/blob/master/lib/hirb/util.rb
11
+ def capture(kind=nil, &block)
12
+ original_stdout = $stdout
13
+ original_stderr = $stderr
14
+ $stdout = fake_out = StringIO.new
15
+ $stderr = fake_err = StringIO.new
16
+ begin
17
+ yield
18
+ rescue SystemExit => e
19
+ # Skip
20
+ ensure
21
+ $stdout = original_stdout
22
+ $stderr = original_stderr
23
+ end
24
+ res = {:stdout => fake_out.string, :stderr => fake_err.string}
25
+ if kind
26
+ res[kind]
27
+ else
28
+ res
29
+ end
30
+ end
metadata ADDED
@@ -0,0 +1,150 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: choosy
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Gabe McArthur
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-03-01 00:00:00.000000000 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ requirement: &69898880 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: *69898880
26
+ - !ruby/object:Gem::Dependency
27
+ name: autotest
28
+ requirement: &69898640 !ruby/object:Gem::Requirement
29
+ none: false
30
+ requirements:
31
+ - - ! '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: *69898640
37
+ - !ruby/object:Gem::Dependency
38
+ name: autotest-notification
39
+ requirement: &69898400 !ruby/object:Gem::Requirement
40
+ none: false
41
+ requirements:
42
+ - - ! '>='
43
+ - !ruby/object:Gem::Version
44
+ version: '0'
45
+ type: :development
46
+ prerelease: false
47
+ version_requirements: *69898400
48
+ - !ruby/object:Gem::Dependency
49
+ name: rspec
50
+ requirement: &69936700 !ruby/object:Gem::Requirement
51
+ none: false
52
+ requirements:
53
+ - - ~>
54
+ - !ruby/object:Gem::Version
55
+ version: '2.5'
56
+ type: :development
57
+ prerelease: false
58
+ version_requirements: *69936700
59
+ description: This is a DSL for creating more complicated command line tools.
60
+ email:
61
+ - madeonamac@gmail.com
62
+ executables: []
63
+ extensions: []
64
+ extra_rdoc_files:
65
+ - LICENSE
66
+ - README.markdown
67
+ files:
68
+ - Gemfile
69
+ - Gemfile.lock
70
+ - LICENSE
71
+ - README.markdown
72
+ - Rakefile
73
+ - lib/VERSION
74
+ - lib/choosy.rb
75
+ - lib/choosy/base_command.rb
76
+ - lib/choosy/command.rb
77
+ - lib/choosy/converter.rb
78
+ - lib/choosy/dsl/base_command_builder.rb
79
+ - lib/choosy/dsl/command_builder.rb
80
+ - lib/choosy/dsl/option_builder.rb
81
+ - lib/choosy/dsl/super_command_builder.rb
82
+ - lib/choosy/errors.rb
83
+ - lib/choosy/option.rb
84
+ - lib/choosy/parse_result.rb
85
+ - lib/choosy/parser.rb
86
+ - lib/choosy/printing/color.rb
87
+ - lib/choosy/printing/erb_printer.rb
88
+ - lib/choosy/printing/help_printer.rb
89
+ - lib/choosy/super_command.rb
90
+ - lib/choosy/super_parser.rb
91
+ - lib/choosy/verifier.rb
92
+ - lib/choosy/version.rb
93
+ - spec/choosy/base_command_spec.rb
94
+ - spec/choosy/command_spec.rb
95
+ - spec/choosy/converter_spec.rb
96
+ - spec/choosy/dsl/base_command_builder_spec.rb
97
+ - spec/choosy/dsl/commmand_builder_spec.rb
98
+ - spec/choosy/dsl/option_builder_spec.rb
99
+ - spec/choosy/dsl/super_command_builder_spec.rb
100
+ - spec/choosy/parser_spec.rb
101
+ - spec/choosy/printing/color_spec.rb
102
+ - spec/choosy/printing/help_printer_spec.rb
103
+ - spec/choosy/super_command_spec.rb
104
+ - spec/choosy/super_parser_spec.rb
105
+ - spec/choosy/verifier_spec.rb
106
+ - spec/integration/command-A_spec.rb
107
+ - spec/integration/supercommand-A_spec.rb
108
+ - spec/spec_helpers.rb
109
+ has_rdoc: true
110
+ homepage: http://github.com/gabemc/choosy
111
+ licenses: []
112
+ post_install_message:
113
+ rdoc_options: []
114
+ require_paths:
115
+ - lib
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ none: false
118
+ requirements:
119
+ - - ! '>='
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ none: false
124
+ requirements:
125
+ - - ! '>='
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 1.5.2
131
+ signing_key:
132
+ specification_version: 3
133
+ summary: Yet another option parsing library.
134
+ test_files:
135
+ - spec/choosy/base_command_spec.rb
136
+ - spec/choosy/command_spec.rb
137
+ - spec/choosy/converter_spec.rb
138
+ - spec/choosy/dsl/base_command_builder_spec.rb
139
+ - spec/choosy/dsl/commmand_builder_spec.rb
140
+ - spec/choosy/dsl/option_builder_spec.rb
141
+ - spec/choosy/dsl/super_command_builder_spec.rb
142
+ - spec/choosy/parser_spec.rb
143
+ - spec/choosy/printing/color_spec.rb
144
+ - spec/choosy/printing/help_printer_spec.rb
145
+ - spec/choosy/super_command_spec.rb
146
+ - spec/choosy/super_parser_spec.rb
147
+ - spec/choosy/verifier_spec.rb
148
+ - spec/integration/command-A_spec.rb
149
+ - spec/integration/supercommand-A_spec.rb
150
+ - spec/spec_helpers.rb