trollop 2.1.2 → 2.1.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.
- checksums.yaml +4 -4
- data/.travis.yml +7 -3
- data/Gemfile.travis +3 -0
- data/History.txt +16 -2
- data/README.md +6 -3
- data/Rakefile +1 -1
- data/lib/trollop.rb +501 -325
- data/test/support/assert_helpers.rb +46 -0
- data/test/test_helper.rb +5 -1
- data/test/trollop/command_line_error_test.rb +27 -0
- data/test/trollop/help_needed_test.rb +19 -0
- data/test/trollop/parser_educate_test.rb +175 -0
- data/test/trollop/parser_opt_test.rb +14 -0
- data/test/trollop/parser_parse_test.rb +79 -0
- data/test/{test_trollop.rb → trollop/parser_test.rb} +155 -198
- data/test/trollop/version_needed_test.rb +19 -0
- data/test/trollop_test.rb +190 -0
- data/trollop.gemspec +7 -6
- metadata +27 -50
@@ -0,0 +1,46 @@
|
|
1
|
+
module Minitest::Assertions
|
2
|
+
def assert_parses_correctly(parser, commandline, expected_opts,
|
3
|
+
expected_leftovers)
|
4
|
+
opts = parser.parse commandline
|
5
|
+
assert_equal expected_opts, opts
|
6
|
+
assert_equal expected_leftovers, parser.leftovers
|
7
|
+
end
|
8
|
+
|
9
|
+
def assert_stderr(str = nil, msg = nil)
|
10
|
+
msg = "#{msg}.\n" if msg
|
11
|
+
|
12
|
+
old_stderr, $stderr = $stderr, StringIO.new('')
|
13
|
+
yield
|
14
|
+
assert_match str, $stderr.string, msg if str
|
15
|
+
ensure
|
16
|
+
$stderr = old_stderr
|
17
|
+
end
|
18
|
+
|
19
|
+
def assert_stdout(str = nil, msg = nil)
|
20
|
+
msg = "#{msg}.\n" if msg
|
21
|
+
|
22
|
+
old_stdout, $stdout = $stdout, StringIO.new('')
|
23
|
+
yield
|
24
|
+
assert_match str, $stdout.string, msg if str
|
25
|
+
ensure
|
26
|
+
$stdout = old_stdout
|
27
|
+
end
|
28
|
+
|
29
|
+
# like assert raises, but if it does raise, it checks status
|
30
|
+
# NOTE: this does not ensure the exception is raised
|
31
|
+
def assert_system_exit *exp
|
32
|
+
msg = "#{exp.pop}.\n" if String === exp.last
|
33
|
+
status = exp.first
|
34
|
+
|
35
|
+
begin
|
36
|
+
yield
|
37
|
+
rescue SystemExit => e
|
38
|
+
assert_equal status, e.status {
|
39
|
+
exception_details(e, "#{msg}#{mu_pp(exp)} exception expected, not")
|
40
|
+
} if status
|
41
|
+
return true
|
42
|
+
end
|
43
|
+
flunk "#{msg}#{mu_pp(exp)} SystemExit expected but nothing was raised."
|
44
|
+
end
|
45
|
+
end
|
46
|
+
|
data/test/test_helper.rb
CHANGED
@@ -3,7 +3,9 @@ $LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
|
3
3
|
unless ENV['MUTANT']
|
4
4
|
begin
|
5
5
|
require "coveralls"
|
6
|
-
Coveralls.wear!
|
6
|
+
Coveralls.wear! do
|
7
|
+
add_filter '/test/'
|
8
|
+
end
|
7
9
|
rescue LoadError
|
8
10
|
end
|
9
11
|
end
|
@@ -15,4 +17,6 @@ end
|
|
15
17
|
|
16
18
|
require 'minitest/autorun'
|
17
19
|
|
20
|
+
Dir[File.expand_path('../support/**/*.rb', __FILE__)].each { |f| require f }
|
21
|
+
|
18
22
|
require 'trollop'
|
@@ -0,0 +1,27 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Trollop
|
4
|
+
class CommandlineErrorTest < ::MiniTest::Test
|
5
|
+
def test_class
|
6
|
+
assert_kind_of Exception, cle("message")
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_message
|
10
|
+
assert "message", cle("message").message
|
11
|
+
end
|
12
|
+
|
13
|
+
def test_error_code_default
|
14
|
+
assert_nil cle("message").error_code
|
15
|
+
end
|
16
|
+
|
17
|
+
def test_error_code_custom
|
18
|
+
assert_equal(-3, cle("message", -3).error_code)
|
19
|
+
end
|
20
|
+
|
21
|
+
private
|
22
|
+
|
23
|
+
def cle(*args)
|
24
|
+
CommandlineError.new(*args)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Trollop
|
4
|
+
class HelpNeededTest < ::MiniTest::Test
|
5
|
+
def test_class
|
6
|
+
assert_kind_of Exception, hn("message")
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_message
|
10
|
+
assert "message", hn("message").message
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def hn(*args)
|
16
|
+
HelpNeeded.new(*args)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,175 @@
|
|
1
|
+
require 'stringio'
|
2
|
+
require 'test_helper'
|
3
|
+
|
4
|
+
module Trollop
|
5
|
+
class ParserEduateTest < ::MiniTest::Test
|
6
|
+
def setup
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_no_arguments_to_stdout
|
10
|
+
assert_stdout(/Options:/) do
|
11
|
+
parser.educate
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_argument_to_stringio
|
16
|
+
assert_educates(/Options:/)
|
17
|
+
end
|
18
|
+
|
19
|
+
def test_no_headers
|
20
|
+
assert_educates(/^Options:/)
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_usage
|
24
|
+
parser.usage("usage string")
|
25
|
+
assert_educates(/^Usage: \w* usage string\n\nOptions:/)
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_usage_synopsis_version
|
29
|
+
end
|
30
|
+
|
31
|
+
# def test_banner
|
32
|
+
# def test_text
|
33
|
+
|
34
|
+
# width, legacy_width
|
35
|
+
# wrap
|
36
|
+
# wrap_lines
|
37
|
+
|
38
|
+
############
|
39
|
+
# convert these into multiple tests
|
40
|
+
# pulled out of trollop_test for now
|
41
|
+
def test_help_has_default_banner
|
42
|
+
@p = Parser.new
|
43
|
+
sio = StringIO.new "w"
|
44
|
+
@p.parse []
|
45
|
+
@p.educate sio
|
46
|
+
help = sio.string.split "\n"
|
47
|
+
assert help[0] =~ /options/i
|
48
|
+
assert_equal 2, help.length # options, then -h
|
49
|
+
|
50
|
+
@p = Parser.new
|
51
|
+
@p.version "my version"
|
52
|
+
sio = StringIO.new "w"
|
53
|
+
@p.parse []
|
54
|
+
@p.educate sio
|
55
|
+
help = sio.string.split "\n"
|
56
|
+
assert help[0] =~ /my version/i
|
57
|
+
assert_equal 4, help.length # version, options, -h, -v
|
58
|
+
|
59
|
+
@p = Parser.new
|
60
|
+
@p.banner "my own banner"
|
61
|
+
sio = StringIO.new "w"
|
62
|
+
@p.parse []
|
63
|
+
@p.educate sio
|
64
|
+
help = sio.string.split "\n"
|
65
|
+
assert help[0] =~ /my own banner/i
|
66
|
+
assert_equal 2, help.length # banner, -h
|
67
|
+
|
68
|
+
@p = Parser.new
|
69
|
+
@p.text "my own text banner"
|
70
|
+
sio = StringIO.new "w"
|
71
|
+
@p.parse []
|
72
|
+
@p.educate sio
|
73
|
+
help = sio.string.split "\n"
|
74
|
+
assert help[0] =~ /my own text banner/i
|
75
|
+
assert_equal 2, help.length # banner, -h
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_help_has_optional_usage
|
79
|
+
@p = Parser.new
|
80
|
+
@p.usage "OPTIONS FILES"
|
81
|
+
sio = StringIO.new "w"
|
82
|
+
@p.parse []
|
83
|
+
@p.educate sio
|
84
|
+
help = sio.string.split "\n"
|
85
|
+
assert help[0] =~ /OPTIONS FILES/i
|
86
|
+
assert_equal 4, help.length # line break, options, then -h
|
87
|
+
end
|
88
|
+
|
89
|
+
def test_help_has_optional_synopsis
|
90
|
+
@p = Parser.new
|
91
|
+
@p.synopsis "About this program"
|
92
|
+
sio = StringIO.new "w"
|
93
|
+
@p.parse []
|
94
|
+
@p.educate sio
|
95
|
+
help = sio.string.split "\n"
|
96
|
+
assert help[0] =~ /About this program/i
|
97
|
+
assert_equal 4, help.length # line break, options, then -h
|
98
|
+
end
|
99
|
+
|
100
|
+
def test_help_has_specific_order_for_usage_and_synopsis
|
101
|
+
@p = Parser.new
|
102
|
+
@p.usage "OPTIONS FILES"
|
103
|
+
@p.synopsis "About this program"
|
104
|
+
sio = StringIO.new "w"
|
105
|
+
@p.parse []
|
106
|
+
@p.educate sio
|
107
|
+
help = sio.string.split "\n"
|
108
|
+
assert help[0] =~ /OPTIONS FILES/i
|
109
|
+
assert help[1] =~ /About this program/i
|
110
|
+
assert_equal 5, help.length # line break, options, then -h
|
111
|
+
end
|
112
|
+
|
113
|
+
def test_help_preserves_positions
|
114
|
+
parser.opt :zzz, "zzz"
|
115
|
+
parser.opt :aaa, "aaa"
|
116
|
+
sio = StringIO.new "w"
|
117
|
+
parser.educate sio
|
118
|
+
|
119
|
+
help = sio.string.split "\n"
|
120
|
+
assert help[1] =~ /zzz/
|
121
|
+
assert help[2] =~ /aaa/
|
122
|
+
end
|
123
|
+
|
124
|
+
def test_help_includes_option_types
|
125
|
+
parser.opt :arg1, 'arg', :type => :int
|
126
|
+
parser.opt :arg2, 'arg', :type => :ints
|
127
|
+
parser.opt :arg3, 'arg', :type => :string
|
128
|
+
parser.opt :arg4, 'arg', :type => :strings
|
129
|
+
parser.opt :arg5, 'arg', :type => :float
|
130
|
+
parser.opt :arg6, 'arg', :type => :floats
|
131
|
+
parser.opt :arg7, 'arg', :type => :io
|
132
|
+
parser.opt :arg8, 'arg', :type => :ios
|
133
|
+
parser.opt :arg9, 'arg', :type => :date
|
134
|
+
parser.opt :arg10, 'arg', :type => :dates
|
135
|
+
sio = StringIO.new "w"
|
136
|
+
parser.educate sio
|
137
|
+
|
138
|
+
help = sio.string.split "\n"
|
139
|
+
assert help[1] =~ /<i>/
|
140
|
+
assert help[2] =~ /<i\+>/
|
141
|
+
assert help[3] =~ /<s>/
|
142
|
+
assert help[4] =~ /<s\+>/
|
143
|
+
assert help[5] =~ /<f>/
|
144
|
+
assert help[6] =~ /<f\+>/
|
145
|
+
assert help[7] =~ /<filename\/uri>/
|
146
|
+
assert help[8] =~ /<filename\/uri\+>/
|
147
|
+
assert help[9] =~ /<date>/
|
148
|
+
assert help[10] =~ /<date\+>/
|
149
|
+
end
|
150
|
+
|
151
|
+
def test_help_has_grammatical_default_text
|
152
|
+
parser.opt :arg1, 'description with period.', :default => 'hello'
|
153
|
+
parser.opt :arg2, 'description without period', :default => 'world'
|
154
|
+
sio = StringIO.new 'w'
|
155
|
+
parser.educate sio
|
156
|
+
|
157
|
+
help = sio.string.split "\n"
|
158
|
+
assert help[1] =~ /Default/
|
159
|
+
assert help[2] =~ /default/
|
160
|
+
end
|
161
|
+
############
|
162
|
+
|
163
|
+
private
|
164
|
+
|
165
|
+
def parser
|
166
|
+
@p ||= Parser.new
|
167
|
+
end
|
168
|
+
|
169
|
+
def assert_educates(output)
|
170
|
+
str = StringIO.new
|
171
|
+
parser.educate str
|
172
|
+
assert_match output, str.string
|
173
|
+
end
|
174
|
+
end
|
175
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
require 'stringio'
|
2
|
+
require 'test_helper'
|
3
|
+
|
4
|
+
module Trollop
|
5
|
+
class ParserParseTest < ::MiniTest::Test
|
6
|
+
|
7
|
+
# TODO: parse
|
8
|
+
# resolve_default_short_options!
|
9
|
+
# parse_date_parameter
|
10
|
+
# parse_integer_parameter(param, arg)
|
11
|
+
# parse_float_parameter(param, arg)
|
12
|
+
# parse_io_parameter(param, arg)
|
13
|
+
# each_arg
|
14
|
+
# collect_argument_parameters
|
15
|
+
|
16
|
+
def test_help_needed
|
17
|
+
parser.opt "arg"
|
18
|
+
assert_raises(HelpNeeded) { parser.parse %w(-h) }
|
19
|
+
assert_raises(HelpNeeded) { parser.parse %w(--help) }
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_help_overridden
|
23
|
+
parser.opt :arg1, "desc", :long => "help"
|
24
|
+
assert parser.parse(%w(-h))[:arg1]
|
25
|
+
assert parser.parse(%w(--help))[:arg1]
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_help_with_other_args
|
29
|
+
parser.opt :arg1
|
30
|
+
assert_raises(HelpNeeded) { @p.parse %w(--arg1 --help) }
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_help_with_arg_error
|
34
|
+
parser.opt :arg1, :type => String
|
35
|
+
assert_raises(HelpNeeded) { @p.parse %w(--arg1 --help) }
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_version_needed_unset
|
39
|
+
parser.opt "arg"
|
40
|
+
assert_raises(CommandlineError) { parser.parse %w(-v) }
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_version_needed
|
44
|
+
parser.version "trollop 5.2.3"
|
45
|
+
assert_raises(VersionNeeded) { parser.parse %w(-v) }
|
46
|
+
assert_raises(VersionNeeded) { parser.parse %w(--version) }
|
47
|
+
end
|
48
|
+
|
49
|
+
def test_version_overridden
|
50
|
+
parser.opt "version"
|
51
|
+
assert parser.parse(%w(-v))["version"]
|
52
|
+
assert parser.parse(%w(-v))[:version_given]
|
53
|
+
end
|
54
|
+
|
55
|
+
def test_version_only_appears_if_set
|
56
|
+
parser.opt "arg"
|
57
|
+
assert_raises(CommandlineError) { parser.parse %w(-v) }
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_version_with_other_args
|
61
|
+
parser.opt :arg1
|
62
|
+
parser.version "1.1"
|
63
|
+
assert_raises(VersionNeeded) { parser.parse %w(--arg1 --version) }
|
64
|
+
end
|
65
|
+
|
66
|
+
def test_version_with_arg_error
|
67
|
+
parser.opt :arg1, :type => String
|
68
|
+
parser.version "1.1"
|
69
|
+
assert_raises(VersionNeeded) { parser.parse %w(--arg1 --version) }
|
70
|
+
end
|
71
|
+
|
72
|
+
|
73
|
+
private
|
74
|
+
|
75
|
+
def parser
|
76
|
+
@p ||= Parser.new
|
77
|
+
end
|
78
|
+
end
|
79
|
+
end
|
@@ -2,18 +2,48 @@ require 'stringio'
|
|
2
2
|
require 'test_helper'
|
3
3
|
|
4
4
|
module Trollop
|
5
|
-
module Test
|
6
5
|
|
7
|
-
class
|
6
|
+
class ParserTest < ::MiniTest::Test
|
8
7
|
def setup
|
9
8
|
@p = Parser.new
|
10
9
|
end
|
11
10
|
|
12
|
-
def
|
13
|
-
|
14
|
-
assert_raises(ArgumentError) { ::Trollop.die 'hello' }
|
11
|
+
def parser
|
12
|
+
@p ||= Parser.new
|
15
13
|
end
|
16
14
|
|
15
|
+
# initialize
|
16
|
+
# cloaker
|
17
|
+
|
18
|
+
def test_version
|
19
|
+
assert_nil parser.version
|
20
|
+
assert_equal "trollop 5.2.3", parser.version("trollop 5.2.3")
|
21
|
+
assert_equal "trollop 5.2.3", parser.version
|
22
|
+
end
|
23
|
+
|
24
|
+
def test_usage
|
25
|
+
assert_nil parser.usage
|
26
|
+
|
27
|
+
assert_equal "usage string", parser.usage("usage string")
|
28
|
+
assert_equal "usage string", parser.usage
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_synopsis
|
32
|
+
assert_nil parser.synopsis
|
33
|
+
|
34
|
+
assert_equal "synopsis string", parser.synopsis("synopsis string")
|
35
|
+
assert_equal "synopsis string", parser.synopsis
|
36
|
+
end
|
37
|
+
|
38
|
+
# def test_depends
|
39
|
+
# def test_conflicts
|
40
|
+
# def test_stop_on
|
41
|
+
# def test_stop_on_unknown
|
42
|
+
|
43
|
+
# die
|
44
|
+
# def test_die_educate_on_error
|
45
|
+
|
46
|
+
|
17
47
|
def test_unknown_arguments
|
18
48
|
assert_raises(CommandlineError) { @p.parse(%w(--arg)) }
|
19
49
|
@p.opt "arg"
|
@@ -88,7 +118,7 @@ class Trollop < ::MiniTest::Unit::TestCase
|
|
88
118
|
opts = @p.parse(%w(--argsi=4))
|
89
119
|
assert_equal 4, opts["argsi"]
|
90
120
|
opts = @p.parse(%w(--argsi=-4))
|
91
|
-
assert_equal -4, opts["argsi"]
|
121
|
+
assert_equal( -4, opts["argsi"])
|
92
122
|
|
93
123
|
assert_raises(CommandlineError) { @p.parse(%w(--argsi 4.2)) }
|
94
124
|
assert_raises(CommandlineError) { @p.parse(%w(--argsi hello)) }
|
@@ -338,7 +368,6 @@ class Trollop < ::MiniTest::Unit::TestCase
|
|
338
368
|
|
339
369
|
## specifying turns them all on, regardless of default
|
340
370
|
opts = @p.parse %w(--no-default-false --no-default-true --no-default-none)
|
341
|
-
p opts
|
342
371
|
assert_equal true, opts[:no_default_none]
|
343
372
|
assert_equal true, opts[:no_default_false]
|
344
373
|
assert_equal true, opts[:no_default_true]
|
@@ -353,12 +382,6 @@ class Trollop < ::MiniTest::Unit::TestCase
|
|
353
382
|
assert_raises(CommandlineError) { @p.parse %w(--no-no-default-true) }
|
354
383
|
end
|
355
384
|
|
356
|
-
def test_special_flags_work
|
357
|
-
@p.version "asdf fdas"
|
358
|
-
assert_raises(VersionNeeded) { @p.parse(%w(-v)) }
|
359
|
-
assert_raises(HelpNeeded) { @p.parse(%w(-h)) }
|
360
|
-
end
|
361
|
-
|
362
385
|
def test_short_options_combine
|
363
386
|
@p.opt :arg1, "desc", :short => "a"
|
364
387
|
@p.opt :arg2, "desc", :short => "b"
|
@@ -383,13 +406,6 @@ class Trollop < ::MiniTest::Unit::TestCase
|
|
383
406
|
assert_raises(CommandlineError) { @p.parse %w(-cba 4) }
|
384
407
|
end
|
385
408
|
|
386
|
-
def test_version_only_appears_if_set
|
387
|
-
@p.opt "arg"
|
388
|
-
assert_raises(CommandlineError) { @p.parse %w(-v) }
|
389
|
-
@p.version "trollop 1.2.3.4"
|
390
|
-
assert_raises(VersionNeeded) { @p.parse %w(-v) }
|
391
|
-
end
|
392
|
-
|
393
409
|
def test_doubledash_ends_option_processing
|
394
410
|
@p.opt :arg1, "desc", :short => "a", :default => 0
|
395
411
|
@p.opt :arg2, "desc", :short => "b", :default => 0
|
@@ -442,6 +458,18 @@ Options:
|
|
442
458
|
EOM
|
443
459
|
end
|
444
460
|
|
461
|
+
def test_integer_formatting
|
462
|
+
@p.opt :arg, "desc", :type => :integer, :short => "i"
|
463
|
+
opts = @p.parse %w(-i 5)
|
464
|
+
assert_equal 5, opts[:arg]
|
465
|
+
end
|
466
|
+
|
467
|
+
def test_integer_formatting
|
468
|
+
@p.opt :arg, "desc", :type => :integer, :short => "i", :default => 3
|
469
|
+
opts = @p.parse %w(-i)
|
470
|
+
assert_equal 3, opts[:arg]
|
471
|
+
end
|
472
|
+
|
445
473
|
def test_floating_point_formatting
|
446
474
|
@p.opt :arg, "desc", :type => :float, :short => "f"
|
447
475
|
opts = @p.parse %w(-f 1)
|
@@ -471,6 +499,12 @@ Options:
|
|
471
499
|
assert_raises(CommandlineError) { @p.parse %w(-f -.) }
|
472
500
|
end
|
473
501
|
|
502
|
+
def test_floating_point_formatting_default
|
503
|
+
@p.opt :arg, "desc", :type => :float, :short => "f", :default => 5.5
|
504
|
+
opts = @p.parse %w(-f)
|
505
|
+
assert_equal 5.5, opts[:arg]
|
506
|
+
end
|
507
|
+
|
474
508
|
def test_date_formatting
|
475
509
|
@p.opt :arg, "desc", :type => :date, :short => 'd'
|
476
510
|
opts = @p.parse(['-d', 'Jan 4, 2007'])
|
@@ -483,7 +517,7 @@ Options:
|
|
483
517
|
assert_raises(ArgumentError) { @p.opt :arg, "desc", :short => "-1" }
|
484
518
|
@p.opt :a1b, "desc"
|
485
519
|
@p.opt :a2b, "desc"
|
486
|
-
assert @p.specs[:a2b]
|
520
|
+
assert @p.specs[:a2b].short.to_i == 0
|
487
521
|
end
|
488
522
|
|
489
523
|
def test_short_options_can_be_weird
|
@@ -660,7 +694,7 @@ Options:
|
|
660
694
|
|
661
695
|
def test_auto_generated_long_names_convert_underscores_to_hyphens
|
662
696
|
@p.opt :hello_there
|
663
|
-
assert_equal "hello-there", @p.specs[:hello_there]
|
697
|
+
assert_equal "hello-there", @p.specs[:hello_there].long
|
664
698
|
end
|
665
699
|
|
666
700
|
def test_arguments_passed_through_block
|
@@ -672,139 +706,6 @@ Options:
|
|
672
706
|
assert_equal @goat, boat
|
673
707
|
end
|
674
708
|
|
675
|
-
def test_help_has_default_banner
|
676
|
-
@p = Parser.new
|
677
|
-
sio = StringIO.new "w"
|
678
|
-
@p.parse []
|
679
|
-
@p.educate sio
|
680
|
-
help = sio.string.split "\n"
|
681
|
-
assert help[0] =~ /options/i
|
682
|
-
assert_equal 2, help.length # options, then -h
|
683
|
-
|
684
|
-
@p = Parser.new
|
685
|
-
@p.version "my version"
|
686
|
-
sio = StringIO.new "w"
|
687
|
-
@p.parse []
|
688
|
-
@p.educate sio
|
689
|
-
help = sio.string.split "\n"
|
690
|
-
assert help[0] =~ /my version/i
|
691
|
-
assert_equal 4, help.length # version, options, -h, -v
|
692
|
-
|
693
|
-
@p = Parser.new
|
694
|
-
@p.banner "my own banner"
|
695
|
-
sio = StringIO.new "w"
|
696
|
-
@p.parse []
|
697
|
-
@p.educate sio
|
698
|
-
help = sio.string.split "\n"
|
699
|
-
assert help[0] =~ /my own banner/i
|
700
|
-
assert_equal 2, help.length # banner, -h
|
701
|
-
end
|
702
|
-
|
703
|
-
def test_help_has_optional_usage
|
704
|
-
@p = Parser.new
|
705
|
-
@p.usage "OPTIONS FILES"
|
706
|
-
sio = StringIO.new "w"
|
707
|
-
@p.parse []
|
708
|
-
@p.educate sio
|
709
|
-
help = sio.string.split "\n"
|
710
|
-
assert help[0] =~ /OPTIONS FILES/i
|
711
|
-
assert_equal 4, help.length # line break, options, then -h
|
712
|
-
end
|
713
|
-
|
714
|
-
def test_help_has_optional_synopsis
|
715
|
-
@p = Parser.new
|
716
|
-
@p.synopsis "About this program"
|
717
|
-
sio = StringIO.new "w"
|
718
|
-
@p.parse []
|
719
|
-
@p.educate sio
|
720
|
-
help = sio.string.split "\n"
|
721
|
-
assert help[0] =~ /About this program/i
|
722
|
-
assert_equal 4, help.length # line break, options, then -h
|
723
|
-
end
|
724
|
-
|
725
|
-
def test_help_has_specific_order_for_usage_and_synopsis
|
726
|
-
@p = Parser.new
|
727
|
-
@p.usage "OPTIONS FILES"
|
728
|
-
@p.synopsis "About this program"
|
729
|
-
sio = StringIO.new "w"
|
730
|
-
@p.parse []
|
731
|
-
@p.educate sio
|
732
|
-
help = sio.string.split "\n"
|
733
|
-
assert help[0] =~ /OPTIONS FILES/i
|
734
|
-
assert help[1] =~ /About this program/i
|
735
|
-
assert_equal 5, help.length # line break, options, then -h
|
736
|
-
end
|
737
|
-
|
738
|
-
def test_help_preserves_positions
|
739
|
-
@p.opt :zzz, "zzz"
|
740
|
-
@p.opt :aaa, "aaa"
|
741
|
-
sio = StringIO.new "w"
|
742
|
-
@p.educate sio
|
743
|
-
|
744
|
-
help = sio.string.split "\n"
|
745
|
-
assert help[1] =~ /zzz/
|
746
|
-
assert help[2] =~ /aaa/
|
747
|
-
end
|
748
|
-
|
749
|
-
def test_help_includes_option_types
|
750
|
-
@p.opt :arg1, 'arg', :type => :int
|
751
|
-
@p.opt :arg2, 'arg', :type => :ints
|
752
|
-
@p.opt :arg3, 'arg', :type => :string
|
753
|
-
@p.opt :arg4, 'arg', :type => :strings
|
754
|
-
@p.opt :arg5, 'arg', :type => :float
|
755
|
-
@p.opt :arg6, 'arg', :type => :floats
|
756
|
-
@p.opt :arg7, 'arg', :type => :io
|
757
|
-
@p.opt :arg8, 'arg', :type => :ios
|
758
|
-
@p.opt :arg9, 'arg', :type => :date
|
759
|
-
@p.opt :arg10, 'arg', :type => :dates
|
760
|
-
sio = StringIO.new "w"
|
761
|
-
@p.educate sio
|
762
|
-
|
763
|
-
help = sio.string.split "\n"
|
764
|
-
assert help[1] =~ /<i>/
|
765
|
-
assert help[2] =~ /<i\+>/
|
766
|
-
assert help[3] =~ /<s>/
|
767
|
-
assert help[4] =~ /<s\+>/
|
768
|
-
assert help[5] =~ /<f>/
|
769
|
-
assert help[6] =~ /<f\+>/
|
770
|
-
assert help[7] =~ /<filename\/uri>/
|
771
|
-
assert help[8] =~ /<filename\/uri\+>/
|
772
|
-
assert help[9] =~ /<date>/
|
773
|
-
assert help[10] =~ /<date\+>/
|
774
|
-
end
|
775
|
-
|
776
|
-
def test_help_has_grammatical_default_text
|
777
|
-
@p.opt :arg1, 'description with period.', :default => 'hello'
|
778
|
-
@p.opt :arg2, 'description without period', :default => 'world'
|
779
|
-
sio = StringIO.new 'w'
|
780
|
-
@p.educate sio
|
781
|
-
|
782
|
-
help = sio.string.split "\n"
|
783
|
-
assert help[1] =~ /Default/
|
784
|
-
assert help[2] =~ /default/
|
785
|
-
end
|
786
|
-
|
787
|
-
def test_version_and_help_short_args_can_be_overridden
|
788
|
-
@p.opt :verbose, "desc", :short => "-v"
|
789
|
-
@p.opt :hello, "desc", :short => "-h"
|
790
|
-
@p.version "version"
|
791
|
-
|
792
|
-
@p.parse(%w(-v))
|
793
|
-
assert_raises(VersionNeeded) { @p.parse(%w(--version)) }
|
794
|
-
@p.parse(%w(-h))
|
795
|
-
assert_raises(HelpNeeded) { @p.parse(%w(--help)) }
|
796
|
-
end
|
797
|
-
|
798
|
-
def test_version_and_help_long_args_can_be_overridden
|
799
|
-
@p.opt :asdf, "desc", :long => "help"
|
800
|
-
@p.opt :asdf2, "desc2", :long => "version"
|
801
|
-
@p.parse %w()
|
802
|
-
@p.parse %w(--help)
|
803
|
-
@p.parse %w(--version)
|
804
|
-
@p.parse %w(-h)
|
805
|
-
@p.parse %w(-v)
|
806
|
-
end
|
807
|
-
|
808
709
|
def test_version_and_help_override_errors
|
809
710
|
@p.opt :asdf, "desc", :type => String
|
810
711
|
@p.version "version"
|
@@ -975,13 +876,6 @@ Options:
|
|
975
876
|
assert_equal @q.leftovers, []
|
976
877
|
end
|
977
878
|
|
978
|
-
def assert_parses_correctly(parser, commandline, expected_opts,
|
979
|
-
expected_leftovers)
|
980
|
-
opts = parser.parse commandline
|
981
|
-
assert_equal expected_opts, opts
|
982
|
-
assert_equal expected_leftovers, parser.leftovers
|
983
|
-
end
|
984
|
-
|
985
879
|
def test_unknown_subcommand
|
986
880
|
@p.opt :global_flag, "Global flag", :short => "-g", :type => :flag
|
987
881
|
@p.opt :global_param, "Global parameter", :short => "-p", :default => 5
|
@@ -1035,6 +929,9 @@ Options:
|
|
1035
929
|
opts = @p.parse %w(--arg2 5/1/2010)
|
1036
930
|
assert_kind_of Date, opts[:arg2]
|
1037
931
|
assert_equal Date.new(2010, 5, 1), opts[:arg2]
|
932
|
+
|
933
|
+
opts = @p.parse %w(--arg3)
|
934
|
+
assert_equal temp, opts[:arg3]
|
1038
935
|
end
|
1039
936
|
|
1040
937
|
def test_unknown_arg_class_type
|
@@ -1180,64 +1077,75 @@ Options:
|
|
1180
1077
|
end
|
1181
1078
|
|
1182
1079
|
def test_simple_interface_handles_help
|
1183
|
-
|
1184
|
-
|
1185
|
-
|
1186
|
-
|
1187
|
-
|
1080
|
+
assert_stdout(/Options:/) do
|
1081
|
+
assert_raises(SystemExit) do
|
1082
|
+
::Trollop::options(%w(-h)) do
|
1083
|
+
opt :potato
|
1084
|
+
end
|
1085
|
+
end
|
1086
|
+
end
|
1087
|
+
|
1088
|
+
# ensure regular status is returned
|
1089
|
+
|
1090
|
+
assert_stdout do
|
1091
|
+
begin
|
1092
|
+
::Trollop::options(%w(-h)) do
|
1093
|
+
opt :potato
|
1094
|
+
end
|
1095
|
+
rescue SystemExit => e
|
1096
|
+
assert_equal 0, e.status
|
1188
1097
|
end
|
1189
1098
|
end
|
1190
1099
|
end
|
1191
1100
|
|
1192
1101
|
def test_simple_interface_handles_version
|
1193
|
-
|
1194
|
-
|
1195
|
-
|
1196
|
-
|
1197
|
-
|
1198
|
-
|
1102
|
+
assert_stdout(/1.2/) do
|
1103
|
+
assert_raises(SystemExit) do
|
1104
|
+
::Trollop::options(%w(-v)) do
|
1105
|
+
version "1.2"
|
1106
|
+
opt :potato
|
1107
|
+
end
|
1199
1108
|
end
|
1200
1109
|
end
|
1201
1110
|
end
|
1202
1111
|
|
1203
1112
|
def test_simple_interface_handles_regular_usage
|
1204
|
-
|
1205
|
-
ARGV.unshift "--potato"
|
1206
|
-
opts = ::Trollop::options do
|
1113
|
+
opts = ::Trollop::options(%w(--potato)) do
|
1207
1114
|
opt :potato
|
1208
1115
|
end
|
1209
1116
|
assert opts[:potato]
|
1210
1117
|
end
|
1211
1118
|
|
1212
1119
|
def test_simple_interface_handles_die
|
1213
|
-
|
1214
|
-
|
1215
|
-
|
1216
|
-
|
1217
|
-
|
1120
|
+
assert_stderr do
|
1121
|
+
::Trollop::options(%w(--potato)) do
|
1122
|
+
opt :potato
|
1123
|
+
end
|
1124
|
+
assert_raises(SystemExit) { ::Trollop::die :potato, "is invalid" }
|
1218
1125
|
end
|
1219
|
-
assert_raises(SystemExit) { ::Trollop::die :potato, "is invalid" }
|
1220
|
-
ensure
|
1221
|
-
$stderr = old_stderr
|
1222
1126
|
end
|
1223
1127
|
|
1224
1128
|
def test_simple_interface_handles_die_without_message
|
1225
|
-
|
1226
|
-
|
1227
|
-
|
1228
|
-
|
1229
|
-
|
1129
|
+
assert_stderr(/Error:/) do
|
1130
|
+
::Trollop::options(%w(--potato)) do
|
1131
|
+
opt :potato
|
1132
|
+
end
|
1133
|
+
assert_raises(SystemExit) { ::Trollop::die :potato }
|
1230
1134
|
end
|
1231
|
-
assert_raises(SystemExit) { ::Trollop::die :potato }
|
1232
|
-
assert $stderr.string =~ /Error:/
|
1233
|
-
ensure
|
1234
|
-
$stderr = old_stderr
|
1235
1135
|
end
|
1236
1136
|
|
1237
|
-
def
|
1238
|
-
|
1239
|
-
|
1240
|
-
|
1137
|
+
def test_invalid_option_with_simple_interface
|
1138
|
+
assert_stderr do
|
1139
|
+
assert_raises(SystemExit) do
|
1140
|
+
::Trollop.options(%w(--potato))
|
1141
|
+
end
|
1142
|
+
end
|
1143
|
+
|
1144
|
+
assert_stderr do
|
1145
|
+
begin
|
1146
|
+
::Trollop.options(%w(--potato))
|
1147
|
+
rescue SystemExit => e
|
1148
|
+
assert_equal(-1, e.status)
|
1241
1149
|
end
|
1242
1150
|
end
|
1243
1151
|
end
|
@@ -1257,7 +1165,56 @@ Options:
|
|
1257
1165
|
@p.parse(%w(--cb1))
|
1258
1166
|
end
|
1259
1167
|
end
|
1260
|
-
end
|
1261
1168
|
|
1169
|
+
def test_ignore_invalid_options
|
1170
|
+
@p.opt :arg1, "desc", :type => String
|
1171
|
+
@p.opt :b, "desc", :type => String
|
1172
|
+
@p.opt :c, "desc", :type => :flag
|
1173
|
+
@p.opt :d, "desc", :type => :flag
|
1174
|
+
@p.ignore_invalid_options = true
|
1175
|
+
opts = @p.parse %w{unknown -S param --arg1 potato -fb daisy --foo -ecg --bar baz -z}
|
1176
|
+
assert_equal "potato", opts[:arg1]
|
1177
|
+
assert_equal "daisy", opts[:b]
|
1178
|
+
assert opts[:c]
|
1179
|
+
refute opts[:d]
|
1180
|
+
assert_equal %w{unknown -S param -f --foo -eg --bar baz -z}, @p.leftovers
|
1181
|
+
end
|
1182
|
+
|
1183
|
+
def test_ignore_invalid_options_stop_on_unknown_long
|
1184
|
+
@p.opt :arg1, "desc", :type => String
|
1185
|
+
@p.ignore_invalid_options = true
|
1186
|
+
@p.stop_on_unknown
|
1187
|
+
opts = @p.parse %w{--unk --arg1 potato}
|
1188
|
+
refute opts[:arg1]
|
1189
|
+
assert_equal %w{--unk --arg1 potato}, @p.leftovers
|
1190
|
+
end
|
1191
|
+
|
1192
|
+
def test_ignore_invalid_options_stop_on_unknown_short
|
1193
|
+
@p.opt :arg1, "desc", :type => String
|
1194
|
+
@p.ignore_invalid_options = true
|
1195
|
+
@p.stop_on_unknown
|
1196
|
+
opts = @p.parse %w{-ua potato}
|
1197
|
+
refute opts[:arg1]
|
1198
|
+
assert_equal %w{-ua potato}, @p.leftovers
|
1199
|
+
end
|
1200
|
+
|
1201
|
+
def test_ignore_invalid_options_stop_on_unknown_partial_end_short
|
1202
|
+
@p.opt :arg1, "desc", :type => :flag
|
1203
|
+
@p.ignore_invalid_options = true
|
1204
|
+
@p.stop_on_unknown
|
1205
|
+
opts = @p.parse %w{-au potato}
|
1206
|
+
assert opts[:arg1]
|
1207
|
+
assert_equal %w{-u potato}, @p.leftovers
|
1208
|
+
end
|
1209
|
+
|
1210
|
+
def test_ignore_invalid_options_stop_on_unknown_partial_mid_short
|
1211
|
+
@p.opt :arg1, "desc", :type => :flag
|
1212
|
+
@p.ignore_invalid_options = true
|
1213
|
+
@p.stop_on_unknown
|
1214
|
+
opts = @p.parse %w{-abu potato}
|
1215
|
+
assert opts[:arg1]
|
1216
|
+
assert_equal %w{-bu potato}, @p.leftovers
|
1217
|
+
end
|
1262
1218
|
end
|
1219
|
+
|
1263
1220
|
end
|