choice 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,182 @@
1
+ $:.unshift "../lib:lib"
2
+ require 'test/unit'
3
+ require 'choice/option'
4
+ require 'choice/parser'
5
+
6
+ class TestParser < Test::Unit::TestCase
7
+ def setup
8
+ @options = {}
9
+ end
10
+
11
+ def test_parse_options
12
+ @options['band'] = Choice::Option.new do
13
+ short '-b'
14
+ long '--band=BAND'
15
+ cast String
16
+ desc 'Your favorite band.'
17
+ end
18
+ @options['animal'] = Choice::Option.new do
19
+ short '-a'
20
+ long '--animal=ANIMAL'
21
+ cast String
22
+ desc 'Your favorite animal.'
23
+ end
24
+ band = 'Led Zeppelin'
25
+ animal = 'Reindeer'
26
+
27
+ args = ['-b', band, "--animal=#{animal}"]
28
+
29
+ choices = Choice::Parser.parse(@options, args)
30
+
31
+ assert_equal band, choices['band']
32
+ assert_equal animal, choices['animal']
33
+ end
34
+
35
+ def test_parse_no_options
36
+ assert_equal Hash.new, Choice::Parser.parse(nil, nil)
37
+ end
38
+
39
+ def test_parse_default
40
+ @options['soda'] = Choice::Option.new do
41
+ short '-s'
42
+ long '--soda=SODA'
43
+ default 'PibbJr'
44
+ end
45
+
46
+ args = []
47
+
48
+ choices = Choice::Parser.parse(@options, args)
49
+
50
+ assert_equal 'PibbJr', choices['soda']
51
+ end
52
+
53
+ def test_parse_options_with_filters
54
+ @options['host'] = Choice::Option.new do
55
+ short '-h'
56
+ filter do |opt|
57
+ opt.gsub!(/[^\w]/, '')
58
+ opt = opt.sub(/k/, 'c')
59
+ end
60
+ end
61
+ host = 'de.fun.kt'
62
+ args = ['-h', host]
63
+ choices = Choice::Parser.parse(@options, args)
64
+
65
+ assert_equal 'defunct', choices['host']
66
+ end
67
+
68
+ def test_casting
69
+ @options['port'] = Choice::Option.new do
70
+ short '-p'
71
+ cast Integer
72
+ end
73
+
74
+ port = '3000'
75
+ args = ['-p', port]
76
+ choices = Choice::Parser.parse(@options, args)
77
+
78
+ assert_equal port.to_i, choices['port']
79
+ end
80
+
81
+ def test_text_required
82
+ @options['name'] = Choice::Option.new do
83
+ short '-n'
84
+ long '--name=NAME'
85
+ end
86
+ @options['age'] = Choice::Option.new do
87
+ short '-a'
88
+ long 'age=[AGE]'
89
+ cast Integer
90
+ end
91
+
92
+ args = ['-n', '-a', '21']
93
+
94
+ assert_raise(Choice::Parser::ArgumentRequired) do
95
+ choices = Choice::Parser.parse(@options, args)
96
+ end
97
+ end
98
+
99
+ def test_text_optional
100
+ @options['color'] = Choice::Option.new do
101
+ short '-c'
102
+ long '--color=[COLOR]'
103
+ end
104
+
105
+ args = ['-c']
106
+ choices = Choice::Parser.parse(@options, args)
107
+
108
+ assert choices['color']
109
+
110
+ color = 'ladyblue'
111
+ args = ['-c', color]
112
+ choices = Choice::Parser.parse(@options, args)
113
+
114
+ assert_equal color, choices['color']
115
+ end
116
+
117
+ def test_ignore_separator
118
+ options = []
119
+ options << ['keyboard', Choice::Option.new do
120
+ short '-k'
121
+ long '--keyboard=BOARD'
122
+ end]
123
+
124
+ options << ['mouse', Choice::Option.new do
125
+ short '-m'
126
+ long '--mouse=MOUSE'
127
+ end]
128
+
129
+ args = ['-m', 'onebutton']
130
+ choices = Choice::Parser.parse([options.first, '----', options.last], args)
131
+
132
+ assert choices['mouse']
133
+ assert_equal 1, choices.size
134
+ end
135
+
136
+ def test_long_as_switch
137
+ @options['chunky'] = Choice::Option.new do
138
+ short '-b'
139
+ long '--bacon'
140
+ end
141
+
142
+ args = ['--bacon']
143
+ choices = Choice::Parser.parse(@options, args)
144
+
145
+ assert choices['chunky']
146
+ end
147
+
148
+ def test_validate
149
+ @options['email'] = Choice::Option.new do
150
+ short '-e'
151
+ long '--email=EMAIL'
152
+ desc 'Your valid email addy.'
153
+ validate /^[a-z0-9_.-]+@[a-z0-9_.-]+\.[a-z]{2,4}$/i
154
+ end
155
+
156
+ email_bad = 'this will@neverwork'
157
+ email_good = 'chris@ozmm.org'
158
+
159
+ args = ['-e', email_bad]
160
+ assert_raise(Choice::Parser::ArgumentValidationFails) do
161
+ choices = Choice::Parser.parse(@options, args)
162
+ end
163
+
164
+ args = ['-e', email_good]
165
+ choices = Choice::Parser.parse(@options, args)
166
+
167
+ assert_equal email_good, choices['email']
168
+ end
169
+
170
+ def test_unknown_argument
171
+ @options['cd'] = Choice::Option.new do
172
+ short '-c'
173
+ long '--cd=CD'
174
+ desc 'A CD you like.'
175
+ end
176
+
177
+ args = ['-c', 'BestOfYanni', '--grace']
178
+ assert_raise(Choice::Parser::UnknownArgument) do
179
+ choices = Choice::Parser.parse(@options, args)
180
+ end
181
+ end
182
+ end
@@ -0,0 +1,103 @@
1
+ $:.unshift "../lib:lib"
2
+ require 'test/unit'
3
+ require 'choice'
4
+
5
+ class TestWriter < Test::Unit::TestCase
6
+
7
+ def setup
8
+ Choice.reset
9
+ end
10
+
11
+ HELP_OUT = ''
12
+ def test_help
13
+ song = Choice::Option.new do
14
+ short '-s'
15
+ long '--song=SONG'
16
+ cast String
17
+ desc 'Your favorite GNR song.'
18
+ desc '(Default: MyMichelle)'
19
+ default 'MyMichelle'
20
+ end
21
+ dude = Choice::Option.new do
22
+ short '-d'
23
+ long '--dude=DUDE'
24
+ cast String
25
+ desc 'Your favorite GNR dude.'
26
+ desc '(Default: Slash)'
27
+ default 'Slash'
28
+ end
29
+
30
+ options = [[:song, song], [:dude, dude]]
31
+ args = { :banner => "Welcome to the jungle",
32
+ :header => [""],
33
+ :options => options,
34
+ :footer => ["", "Wake up."] }
35
+
36
+ help_string = <<-HELP
37
+ Welcome to the jungle
38
+
39
+ -s, --song=SONG Your favorite GNR song.
40
+ (Default: MyMichelle)
41
+ -d, --dude=DUDE Your favorite GNR dude.
42
+ (Default: Slash)
43
+
44
+ Wake up.
45
+ HELP
46
+
47
+ Choice::Writer.help(args, HELP_OUT, true)
48
+
49
+ assert_equal help_string, HELP_OUT
50
+ end
51
+
52
+ BANNER_OUT = ''
53
+ def test_banner
54
+ media = Choice::Option.new do
55
+ short '-m'
56
+ long '--media=MEDIA'
57
+ end
58
+ rom = Choice::Option.new do
59
+ short '-r'
60
+ long '--rom=ROM'
61
+ end
62
+
63
+ options = [[:media, media], [:rom, rom]]
64
+ args = { :header => [""],
65
+ :options => options }
66
+
67
+ help_string = <<-HELP
68
+ Usage: #{program} [-mr]
69
+
70
+ -m, --media=MEDIA
71
+ -r, --rom=ROM
72
+ HELP
73
+
74
+ Choice::Writer.help(args, BANNER_OUT, true)
75
+
76
+ assert_equal help_string, BANNER_OUT
77
+ end
78
+
79
+ SPILLOVER_OUT = ''
80
+ def test_desc_spillover
81
+ toolong = Choice::Option.new do
82
+ long '--thisisgonnabewaytoolongiswear=STRING'
83
+ desc 'Way too long, boy wonder.'
84
+ end
85
+
86
+ options = [[:toolong, toolong]]
87
+
88
+ help_string = <<-HELP
89
+ Usage: #{program}
90
+ --thisisgonnabewaytoolongiswear=STRING
91
+ Way too long, boy wonder.
92
+ HELP
93
+
94
+
95
+ Choice::Writer.help({:options => options}, SPILLOVER_OUT, true)
96
+
97
+ assert_equal help_string, SPILLOVER_OUT
98
+ end
99
+
100
+ def program
101
+ if (/(\/|\\)/ =~ $0) then File.basename($0) else $0 end
102
+ end
103
+ end
metadata ADDED
@@ -0,0 +1,60 @@
1
+ --- !ruby/object:Gem::Specification
2
+ rubygems_version: 0.8.11
3
+ specification_version: 1
4
+ name: choice
5
+ version: !ruby/object:Gem::Version
6
+ version: 0.1.0
7
+ date: 2006-04-30 00:00:00 -07:00
8
+ summary: Choice is a command line option parser.
9
+ require_paths:
10
+ - lib
11
+ email: chris@ozmm.org
12
+ homepage: http://choice.rubyforge.org/
13
+ rubyforge_project:
14
+ description: Choice is a simple little gem for easily defining and parsing command line options with a friendly DSL.
15
+ autorequire: choice
16
+ default_executable:
17
+ bindir: bin
18
+ has_rdoc: false
19
+ required_ruby_version: !ruby/object:Gem::Version::Requirement
20
+ requirements:
21
+ - - ">"
22
+ - !ruby/object:Gem::Version
23
+ version: 0.0.0
24
+ version:
25
+ platform: ruby
26
+ signing_key:
27
+ cert_chain:
28
+ authors:
29
+ - Chris Wanstrath
30
+ files:
31
+ - README
32
+ - CHANGELOG
33
+ - LICENSE
34
+ - lib/choice
35
+ - lib/choice.rb
36
+ - lib/choice/lazyhash.rb
37
+ - lib/choice/option.rb
38
+ - lib/choice/parser.rb
39
+ - lib/choice/version.rb
40
+ - lib/choice/writer.rb
41
+ - test/test_choice.rb
42
+ - test/test_lazyhash.rb
43
+ - test/test_option.rb
44
+ - test/test_parser.rb
45
+ - test/test_writer.rb
46
+ - examples/ftpd.rb
47
+ test_files: []
48
+
49
+ rdoc_options: []
50
+
51
+ extra_rdoc_files: []
52
+
53
+ executables: []
54
+
55
+ extensions: []
56
+
57
+ requirements: []
58
+
59
+ dependencies: []
60
+