cmdopt 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- data/MIT-LICENSE +21 -0
- data/README.txt +138 -0
- data/cmdopt.gemspec +34 -0
- data/lib/cmdopt.rb +256 -0
- data/setup.rb +1585 -0
- data/test/cmdopt_test.rb +377 -0
- metadata +66 -0
data/test/cmdopt_test.rb
ADDED
@@ -0,0 +1,377 @@
|
|
1
|
+
###
|
2
|
+
### $Release: 0.2.0 $
|
3
|
+
### $Copyright: copyright(c) 2011 kuwata-lab.com all rights reserved $
|
4
|
+
### $License: MIT License $
|
5
|
+
###
|
6
|
+
|
7
|
+
require 'test/unit'
|
8
|
+
require 'section9/unittest'
|
9
|
+
|
10
|
+
require 'cmdopt'
|
11
|
+
|
12
|
+
|
13
|
+
##
|
14
|
+
## option definitions
|
15
|
+
##
|
16
|
+
$cmdopt_parser = Cmdopt::Parser.new()
|
17
|
+
# short and long options
|
18
|
+
$cmdopt_parser.option("-h, --help", "show help")
|
19
|
+
# short option only, but specify attribute name
|
20
|
+
$cmdopt_parser.option("-v #version", "print version")
|
21
|
+
# validation example
|
22
|
+
$cmdopt_parser.option("-m, --mode=MODE", "set mode ('verbose' or 'quiet')")\
|
23
|
+
.validation {|val| val !~ /^(verbose|quiet)$/ and "'verbose' or 'quiet' expected." }
|
24
|
+
# action example
|
25
|
+
$cmdopt_parser.option(" --quiet", "quiet mode")\
|
26
|
+
.action {|val, opts| opts.mode = "quiet" }
|
27
|
+
# optional argument
|
28
|
+
$cmdopt_parser.option("-i, --indent[=N]", "indent (default 2)")\
|
29
|
+
.validation {|val| !(val == true || val =~ /^\d+$/) and "integer expected." }
|
30
|
+
# multiple option
|
31
|
+
$cmdopt_parser.option("-I PATH #paths", "include path (multiple OK)")\
|
32
|
+
.action {|val, opts| (opts.paths ||= []) << val }
|
33
|
+
# private option (not displayed in help message)
|
34
|
+
$cmdopt_parser.option("-D", nil)
|
35
|
+
|
36
|
+
|
37
|
+
class Cmdopt_Schema_TC < TC
|
38
|
+
|
39
|
+
def schema_
|
40
|
+
@schema_ ||= Cmdopt::Schema.new
|
41
|
+
end
|
42
|
+
|
43
|
+
def item_
|
44
|
+
@item_ ||= schema_.add("-m, --mode=MODE", "set mode")
|
45
|
+
end
|
46
|
+
|
47
|
+
describe "#add()" do
|
48
|
+
it "returns item object." do
|
49
|
+
verify_(item_).is_a?(Cmdopt::SchemaItem)
|
50
|
+
end
|
51
|
+
it "parses command-option definition." do
|
52
|
+
item = item_
|
53
|
+
verify_(item.short) == "m"
|
54
|
+
verify_(item.long) == "mode"
|
55
|
+
verify_(item.arg) == "MODE"
|
56
|
+
verify_(item.required) == true
|
57
|
+
verify_(item.attr) == "mode"
|
58
|
+
verify_(item.desc) == "set mode"
|
59
|
+
verify_(item.validator) == nil
|
60
|
+
end
|
61
|
+
it "sets default handler function." do
|
62
|
+
verify_(item_.handler).is_a?(Proc)
|
63
|
+
opts = Cmdopt::Options.new(:mode=>nil)
|
64
|
+
item_.handler.call("verbose", opts)
|
65
|
+
verify_(opts.mode) == "verbose"
|
66
|
+
end
|
67
|
+
it "register item object." do
|
68
|
+
item_ # don't remove this!
|
69
|
+
verify_(schema_.get('-m')).same?(item_)
|
70
|
+
verify_(schema_.get('--mode')).same?(item_)
|
71
|
+
end
|
72
|
+
it "throws error when option definition is invalid." do
|
73
|
+
pr = proc { schema_.add("-m MODE, --mode", "set mode") }
|
74
|
+
verify_(pr).raise?(ArgumentError, "'-m MODE, --mode': invalid option definition.")
|
75
|
+
end
|
76
|
+
it "recognizes '#name' as attribute name." do
|
77
|
+
item = schema_.add('-q #quiet', nil)
|
78
|
+
verify_(item.attr) == 'quiet'
|
79
|
+
verify_(item.short) == 'q'
|
80
|
+
verify_(item.long) == nil
|
81
|
+
#
|
82
|
+
item = schema_.add('-q value #quiet', nil)
|
83
|
+
verify_(item.attr) == 'quiet'
|
84
|
+
verify_(item.short) == 'q'
|
85
|
+
verify_(item.long) == nil
|
86
|
+
#
|
87
|
+
item = schema_.add('--silent #quiet', nil)
|
88
|
+
verify_(item.attr) == 'quiet'
|
89
|
+
verify_(item.short) == nil
|
90
|
+
verify_(item.long) == 'silent'
|
91
|
+
end
|
92
|
+
end
|
93
|
+
|
94
|
+
def parser_
|
95
|
+
@parser_ ||= $cmdopt_parser
|
96
|
+
end
|
97
|
+
|
98
|
+
describe "#help()" do
|
99
|
+
it "returns help message of command-line options." do
|
100
|
+
expected = [
|
101
|
+
" -h, --help : show help\n",
|
102
|
+
" -v : print version\n",
|
103
|
+
" -m, --mode=MODE : set mode ('verbose' or 'quiet')\n",
|
104
|
+
" --quiet : quiet mode\n",
|
105
|
+
" -i, --indent[=N] : indent (default 2)\n",
|
106
|
+
" -I PATH : include path (multiple OK)\n",
|
107
|
+
#" -D : enable debug mode\n",
|
108
|
+
].join
|
109
|
+
verify_(parser_.schema.help(20)) == expected
|
110
|
+
verify_(parser_ .help(20)) == expected
|
111
|
+
end
|
112
|
+
it "takes width and indent arguments." do
|
113
|
+
expected = [
|
114
|
+
" -h, --help: show help\n",
|
115
|
+
" -v : print version\n",
|
116
|
+
" -m, --mode=MODE: set mode ('verbose' or 'quiet')\n",
|
117
|
+
" --quiet: quiet mode\n",
|
118
|
+
" -i, --indent[=N]: indent (default 2)\n",
|
119
|
+
" -I PATH : include path (multiple OK)\n",
|
120
|
+
#" -D : enable debug mode\n",
|
121
|
+
].join
|
122
|
+
verify_(parser_.schema.help(10, " ")) == expected
|
123
|
+
verify_(parser_ .help(10, " ")) == expected
|
124
|
+
end
|
125
|
+
it "calculates width when not specified." do
|
126
|
+
expected = [
|
127
|
+
" -h, --help : show help\n",
|
128
|
+
" -v : print version\n",
|
129
|
+
" -m, --mode=MODE : set mode ('verbose' or 'quiet')\n",
|
130
|
+
" --quiet : quiet mode\n",
|
131
|
+
" -i, --indent[=N] : indent (default 2)\n",
|
132
|
+
" -I PATH : include path (multiple OK)\n",
|
133
|
+
#" -D : enable debug mode\n"
|
134
|
+
].join
|
135
|
+
verify_(parser_.schema.help()) == expected
|
136
|
+
verify_(parser_ .help()) == expected
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
end
|
141
|
+
|
142
|
+
|
143
|
+
class Cmdopt_Builder_TC < TC
|
144
|
+
|
145
|
+
def item_
|
146
|
+
@item_ ||= Cmdopt::SchemaItem.new
|
147
|
+
end
|
148
|
+
|
149
|
+
def builder_
|
150
|
+
@builder_ ||= Cmdopt::Builder.new(item_)
|
151
|
+
end
|
152
|
+
|
153
|
+
describe "#get_item()" do
|
154
|
+
it "returns item object." do
|
155
|
+
verify_(builder_.get_item()).same?(item_)
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
159
|
+
describe "#validation()" do
|
160
|
+
it "sets validator function." do
|
161
|
+
builder_.validation {|va| "OK" }
|
162
|
+
verify_(builder_.get_item.validator.call(nil)) == "OK"
|
163
|
+
end
|
164
|
+
it "returns self." do
|
165
|
+
ret = builder_.validation {|val| "OK" }
|
166
|
+
verify_(ret).same?(builder_)
|
167
|
+
end
|
168
|
+
end
|
169
|
+
|
170
|
+
describe "#action()" do
|
171
|
+
it "sets handler function." do
|
172
|
+
builder_.action {|val, opts| "YES" }
|
173
|
+
verify_(builder_.get_item.handler.call(nil, {})) == "YES"
|
174
|
+
end
|
175
|
+
it "returns self." do
|
176
|
+
ret = builder_.action {|val, opts| "YES" }
|
177
|
+
verify_(ret).same?(builder_)
|
178
|
+
end
|
179
|
+
end
|
180
|
+
|
181
|
+
end
|
182
|
+
|
183
|
+
|
184
|
+
class Cmdopt_Parser_TC < TC
|
185
|
+
|
186
|
+
describe "#option()" do
|
187
|
+
it "registers option definition." do
|
188
|
+
parser = Cmdopt::Parser.new
|
189
|
+
parser.option("-h, --help", "show help")
|
190
|
+
item = parser.schema.get("-h")
|
191
|
+
verify_(item).is_a?(Cmdopt::SchemaItem)
|
192
|
+
verify_(item.short) == "h"
|
193
|
+
verify_(item.long) == "help"
|
194
|
+
verify_(item.desc) == "show help"
|
195
|
+
end
|
196
|
+
it "returns builder object." do
|
197
|
+
parser = Cmdopt::Parser.new
|
198
|
+
ret = parser.option("-h, --help", "show help")
|
199
|
+
verify_(ret).is_a?(Cmdopt::Builder)
|
200
|
+
end
|
201
|
+
end
|
202
|
+
|
203
|
+
describe "#help()" do
|
204
|
+
it "calls #schema.help()." do
|
205
|
+
parser = Cmdopt::Parser.new
|
206
|
+
rc = recorder()
|
207
|
+
rc.record_method(parser.schema, :help)
|
208
|
+
ret = parser.help(19, " ")
|
209
|
+
verify_(rc[0].name) == :help
|
210
|
+
verify_(rc[0].args) == [19, " "]
|
211
|
+
end
|
212
|
+
end
|
213
|
+
|
214
|
+
def parser_
|
215
|
+
@parser_ ||= $cmdopt_parser
|
216
|
+
end
|
217
|
+
|
218
|
+
describe "#parse()" do
|
219
|
+
it "parses args." do
|
220
|
+
args = ["-vmquiet", "--help", "foo", "bar"]
|
221
|
+
defaults = {:mode=>"verbose", :file=>"config.json"}
|
222
|
+
opts = parser_.parse(args, defaults)
|
223
|
+
verify_(opts).NOT.same?(defaults)
|
224
|
+
verify_(opts.help) == true
|
225
|
+
verify_(opts.version) == true
|
226
|
+
verify_(opts.mode) == "quiet" # overrided
|
227
|
+
verify_(opts.file) == "config.json" # not overrided
|
228
|
+
verify_(args) == ["foo", "bar"]
|
229
|
+
end
|
230
|
+
it "parses long options." do
|
231
|
+
# no argument
|
232
|
+
args = ["--quiet", "foo", "bar"]
|
233
|
+
opts = parser_.parse(args)
|
234
|
+
verify_(opts.mode) == "quiet"
|
235
|
+
verify_(args) == ["foo", "bar"]
|
236
|
+
# required argument
|
237
|
+
args = ["--mode=verbose", "foo", "bar"]
|
238
|
+
opts = parser_.parse(args)
|
239
|
+
verify_(opts.mode) == "verbose"
|
240
|
+
verify_(args) == ["foo", "bar"]
|
241
|
+
end
|
242
|
+
it "parses short options." do
|
243
|
+
args = ["-h", "-m", "quiet", "foo", "bar"]
|
244
|
+
opts = parser_.parse(args)
|
245
|
+
verify_(opts.help) == true # no argument
|
246
|
+
verify_(opts.mode) == "quiet" # required argument
|
247
|
+
verify_(args) == ["foo", "bar"];
|
248
|
+
end
|
249
|
+
it "short options can be conbined." do
|
250
|
+
args = ["-hmquiet", "foo", "bar"]
|
251
|
+
opts = parser_.parse(args)
|
252
|
+
verify_(opts.help) == true
|
253
|
+
verify_(opts.mode) == "quiet"
|
254
|
+
verify_(args) == ["foo", "bar"]
|
255
|
+
end
|
256
|
+
it "optional argument is available for short option." do
|
257
|
+
## when optional argument is not passed
|
258
|
+
args = ["-i", "foo", "bar"]
|
259
|
+
opts = parser_.parse(args)
|
260
|
+
verify_(opts.indent) == true
|
261
|
+
verify_(args) == ["foo", "bar"]
|
262
|
+
## when optional argument is passed
|
263
|
+
args = ["-i3", "foo", "bar"]
|
264
|
+
opts = parser_.parse(args)
|
265
|
+
verify_(opts.indent) == '3'
|
266
|
+
verify_(args) == ["foo", "bar"]
|
267
|
+
end
|
268
|
+
it "optional argument is available for long option." do
|
269
|
+
## when optional argument is not passed
|
270
|
+
args = ["--indent", "foo", "bar"]
|
271
|
+
opts = parser_.parse(args)
|
272
|
+
verify_(opts.indent) == true
|
273
|
+
verify_(args) == ["foo", "bar"]
|
274
|
+
## when optional argument is passed
|
275
|
+
args = ["--indent=3", "foo", "bar"]
|
276
|
+
opts = parser_.parse(args)
|
277
|
+
verify_(opts.indent) == '3'
|
278
|
+
verify_(args) == ["foo", "bar"]
|
279
|
+
end
|
280
|
+
it "throws error when unknown short option specified." do
|
281
|
+
args = ["-xD", "foo", "bar"]
|
282
|
+
pr = proc { parser_.parse(args, {}) }
|
283
|
+
verify_(pr).raise?(Cmdopt::ParseError, "-x: unknown option.")
|
284
|
+
end
|
285
|
+
it "throws error when unknown long option specified." do
|
286
|
+
## when argument not passed
|
287
|
+
args = ["--SOS"]
|
288
|
+
pr = proc { parser_.parse(args) }
|
289
|
+
verify_(pr).raise?(Cmdopt::ParseError, "--SOS: unknown option.")
|
290
|
+
## with argument passed
|
291
|
+
args = ["--sos=SOS"]
|
292
|
+
pr = proc { parser_.parse(args, {}) }
|
293
|
+
verify_(pr).raise?(Cmdopt::ParseError, "--sos=SOS: unknown option.")
|
294
|
+
end
|
295
|
+
it "calls handler function." do
|
296
|
+
args = ["--quiet", "foo", "bar"]
|
297
|
+
opts = parser_.parse(args)
|
298
|
+
verify_(opts.quiet) == nil
|
299
|
+
verify_(opts.mode) == "quiet"
|
300
|
+
## multiple option
|
301
|
+
args = ["-Ipath1", "-Ipath2", "-Ipath3"]
|
302
|
+
opts = parser_.parse(args)
|
303
|
+
verify_(opts.paths) == ["path1", "path2", "path3"]
|
304
|
+
end
|
305
|
+
it "throws error when validator returns error message." do
|
306
|
+
## when short option
|
307
|
+
args = ["-msilent", "foo", "bar"]
|
308
|
+
pr = proc { parser_.parse(args) }
|
309
|
+
verify_(pr).raise?(Cmdopt::ParseError, "-m silent: 'verbose' or 'quiet' expected.")
|
310
|
+
## when short option (optional)
|
311
|
+
args = ["-i9.99", "foo", "bar"]
|
312
|
+
pr = proc { parser_.parse(args) }
|
313
|
+
verify_(pr).raise?(Cmdopt::ParseError, "-i9.99: integer expected.")
|
314
|
+
## when long option
|
315
|
+
args = ["--indent=zero", "foo", "bar"]
|
316
|
+
pr = proc { parser_.parse(args) }
|
317
|
+
verify_(pr).raise?(Cmdopt::ParseError, "--indent=zero: integer expected.")
|
318
|
+
end
|
319
|
+
it "stop parsing when '--' found." do
|
320
|
+
args = ["-h", "--", "-m", "verbose"]
|
321
|
+
opts = parser_.parse(args)
|
322
|
+
verify_(args) == ["-m", "verbose"]
|
323
|
+
verify_(opts.help) == true
|
324
|
+
verify_(opts.mode) == nil
|
325
|
+
end
|
326
|
+
it "returns different object from defaults." do
|
327
|
+
args = ["-Dm", "verbose", "foo", "bar"]
|
328
|
+
defaults = {}
|
329
|
+
opts = parser_.parse(args, defaults)
|
330
|
+
verify_(opts).NOT.same? defaults
|
331
|
+
verify_(opts).NOT == defaults
|
332
|
+
verify_(defaults.empty?) == true
|
333
|
+
end
|
334
|
+
end
|
335
|
+
|
336
|
+
end
|
337
|
+
|
338
|
+
|
339
|
+
class Cmdopt_ParseError_TC < TC
|
340
|
+
|
341
|
+
def err_
|
342
|
+
@err ||= Cmdopt::ParseError.new("SOS")
|
343
|
+
end
|
344
|
+
it "subclass of Error" do
|
345
|
+
verify_(err_).is_a?(Exception)
|
346
|
+
end
|
347
|
+
it "#message is set." do
|
348
|
+
verify_(err_.message) == "SOS"
|
349
|
+
end
|
350
|
+
|
351
|
+
end
|
352
|
+
|
353
|
+
|
354
|
+
class Cmdopt_Options_TC < TC
|
355
|
+
|
356
|
+
def opts_
|
357
|
+
@opts_ ||= Cmdopt::Options.new(:indent=>2, :paths=>['path1'], :help=>true, :mode=>"verbose")
|
358
|
+
end
|
359
|
+
|
360
|
+
describe "#__init__()" do
|
361
|
+
it "takes keyword arguments." do
|
362
|
+
verify_(opts_.indent) == 2
|
363
|
+
verify_(opts_.paths) == ['path1']
|
364
|
+
verify_(opts_.help) == true
|
365
|
+
verify_(opts_.mode) == "verbose"
|
366
|
+
end
|
367
|
+
end
|
368
|
+
|
369
|
+
describe "#__repr__()" do
|
370
|
+
it "prints key and values in order of key." do
|
371
|
+
expected = %Q`<Cmdopt::Options help=true, indent=2, mode="verbose", paths=["path1"]>`
|
372
|
+
verify_(opts_.inspect) == expected
|
373
|
+
#verify_("Haruhi\nMikuru\nYuki\n") == "Haruhi\nMichiru\nYuki\n"
|
374
|
+
end
|
375
|
+
end
|
376
|
+
|
377
|
+
end
|
metadata
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cmdopt
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.2.0
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- makoto kuwata
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2011-12-22 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: section9-unittest
|
16
|
+
requirement: &2160150160 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *2160150160
|
25
|
+
description: ! 'Cmdopt.rb is a command-line option parser.
|
26
|
+
|
27
|
+
It is easier to use than optparse.rb (which is a standard library of Ruby).
|
28
|
+
|
29
|
+
'
|
30
|
+
email: kwa(at)kuwata-lab.com
|
31
|
+
executables: []
|
32
|
+
extensions: []
|
33
|
+
extra_rdoc_files: []
|
34
|
+
files:
|
35
|
+
- README.txt
|
36
|
+
- MIT-LICENSE
|
37
|
+
- setup.rb
|
38
|
+
- cmdopt.gemspec
|
39
|
+
- lib/cmdopt.rb
|
40
|
+
- test/cmdopt_test.rb
|
41
|
+
homepage: https://bitbucket.org/kwatch/cmdopt/wiki/Cmdopt.rb
|
42
|
+
licenses: []
|
43
|
+
post_install_message:
|
44
|
+
rdoc_options: []
|
45
|
+
require_paths:
|
46
|
+
- lib
|
47
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
48
|
+
none: false
|
49
|
+
requirements:
|
50
|
+
- - ! '>='
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: '0'
|
53
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ! '>='
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
version: '0'
|
59
|
+
requirements: []
|
60
|
+
rubyforge_project: cmdopt
|
61
|
+
rubygems_version: 1.8.11
|
62
|
+
signing_key:
|
63
|
+
specification_version: 3
|
64
|
+
summary: easy-to-use command option parser
|
65
|
+
test_files:
|
66
|
+
- test/cmdopt_test.rb
|