optimist 3.0.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +15 -0
- data/.travis.yml +11 -0
- data/FAQ.txt +92 -0
- data/Gemfile +4 -0
- data/History.txt +168 -0
- data/README.md +60 -0
- data/Rakefile +15 -0
- data/lib/optimist.rb +1014 -0
- data/optimist.gemspec +35 -0
- data/test/optimist/command_line_error_test.rb +27 -0
- data/test/optimist/help_needed_test.rb +19 -0
- data/test/optimist/parser_educate_test.rb +175 -0
- data/test/optimist/parser_opt_test.rb +14 -0
- data/test/optimist/parser_parse_test.rb +79 -0
- data/test/optimist/parser_test.rb +1220 -0
- data/test/optimist/version_needed_test.rb +19 -0
- data/test/optimist_test.rb +190 -0
- data/test/support/assert_helpers.rb +46 -0
- data/test/test_helper.rb +22 -0
- metadata +125 -0
@@ -0,0 +1,19 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
module Optimist
|
4
|
+
class VersionNeededTest < ::MiniTest::Test
|
5
|
+
def test_class
|
6
|
+
assert_kind_of Exception, vn("message")
|
7
|
+
end
|
8
|
+
|
9
|
+
def test_message
|
10
|
+
assert "message", vn("message").message
|
11
|
+
end
|
12
|
+
|
13
|
+
private
|
14
|
+
|
15
|
+
def vn(*args)
|
16
|
+
VersionNeeded.new(*args)
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,190 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
class OptimistTest < MiniTest::Test
|
4
|
+
def setup
|
5
|
+
Optimist.send(:instance_variable_set, "@last_parser", nil)
|
6
|
+
end
|
7
|
+
|
8
|
+
def parser(&block)
|
9
|
+
Optimist::Parser.new(&block)
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_options
|
13
|
+
opts = Optimist.options %w(-f) do
|
14
|
+
opt :f
|
15
|
+
end
|
16
|
+
|
17
|
+
assert_equal true, opts[:f]
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_options_die_default
|
21
|
+
assert_stderr(/Error: unknown argument.*Try --help/m) do
|
22
|
+
assert_system_exit(-1) do
|
23
|
+
Optimist.options %w(-f) do
|
24
|
+
opt :x
|
25
|
+
end
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_options_die_educate_on_error
|
31
|
+
assert_stderr(/Error: unknown argument.*Options/m) do
|
32
|
+
assert_system_exit(-1) do
|
33
|
+
Optimist.options %w(-f) do
|
34
|
+
opt :x
|
35
|
+
educate_on_error
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_die_without_options_ever_run
|
42
|
+
assert_raises(ArgumentError) { Optimist.die 'hello' }
|
43
|
+
end
|
44
|
+
|
45
|
+
def test_die
|
46
|
+
assert_stderr(/Error: issue with parsing/) do
|
47
|
+
assert_system_exit(-1) do
|
48
|
+
Optimist.options []
|
49
|
+
Optimist.die "issue with parsing"
|
50
|
+
end
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_die_custom_error_code
|
55
|
+
assert_stderr(/Error: issue with parsing/) do
|
56
|
+
assert_system_exit(5) do
|
57
|
+
Optimist.options []
|
58
|
+
Optimist.die "issue with parsing", nil, 5
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_die_custom_error_code_two_args
|
64
|
+
assert_stderr(/Error: issue with parsing/) do
|
65
|
+
assert_system_exit(5) do
|
66
|
+
Optimist.options []
|
67
|
+
Optimist.die "issue with parsing", 5
|
68
|
+
end
|
69
|
+
end
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_educate_without_options_ever_run
|
73
|
+
assert_raises(ArgumentError) { Optimist.educate }
|
74
|
+
end
|
75
|
+
|
76
|
+
def test_educate
|
77
|
+
assert_stdout(/Show this message/) do
|
78
|
+
assert_system_exit(0) do
|
79
|
+
Optimist.options []
|
80
|
+
Optimist.educate
|
81
|
+
end
|
82
|
+
end
|
83
|
+
end
|
84
|
+
|
85
|
+
def test_with_standard_exception_options
|
86
|
+
p = parser do
|
87
|
+
opt :f
|
88
|
+
end
|
89
|
+
|
90
|
+
opts = Optimist::with_standard_exception_handling p do
|
91
|
+
p.parse %w(-f)
|
92
|
+
end
|
93
|
+
|
94
|
+
assert_equal true, opts[:f]
|
95
|
+
end
|
96
|
+
|
97
|
+
def test_with_standard_exception_version_exception
|
98
|
+
p = parser do
|
99
|
+
version "5.5"
|
100
|
+
end
|
101
|
+
|
102
|
+
assert_stdout(/5\.5/) do
|
103
|
+
assert_system_exit(0) do
|
104
|
+
Optimist::with_standard_exception_handling p do
|
105
|
+
raise Optimist::VersionNeeded
|
106
|
+
end
|
107
|
+
end
|
108
|
+
end
|
109
|
+
end
|
110
|
+
|
111
|
+
def test_with_standard_exception_version_flag
|
112
|
+
p = parser do
|
113
|
+
version "5.5"
|
114
|
+
end
|
115
|
+
|
116
|
+
assert_stdout(/5\.5/) do
|
117
|
+
assert_system_exit(0) do
|
118
|
+
Optimist::with_standard_exception_handling p do
|
119
|
+
p.parse %w(-v)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
end
|
123
|
+
end
|
124
|
+
|
125
|
+
def test_with_standard_exception_die_exception
|
126
|
+
assert_stderr(/Error: cl error/) do
|
127
|
+
assert_system_exit(-1) do
|
128
|
+
p = parser
|
129
|
+
Optimist.with_standard_exception_handling(p) do
|
130
|
+
raise ::Optimist::CommandlineError.new('cl error')
|
131
|
+
end
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
def test_with_standard_exception_die_exception_custom_error
|
137
|
+
assert_stderr(/Error: cl error/) do
|
138
|
+
assert_system_exit(5) do
|
139
|
+
p = parser
|
140
|
+
Optimist.with_standard_exception_handling(p) do
|
141
|
+
raise ::Optimist::CommandlineError.new('cl error', 5)
|
142
|
+
end
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
def test_with_standard_exception_die
|
148
|
+
assert_stderr(/Error: cl error/) do
|
149
|
+
assert_system_exit(-1) do
|
150
|
+
p = parser
|
151
|
+
Optimist.with_standard_exception_handling(p) do
|
152
|
+
p.die 'cl error'
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
156
|
+
end
|
157
|
+
|
158
|
+
def test_with_standard_exception_die_custom_error
|
159
|
+
assert_stderr(/Error: cl error/) do
|
160
|
+
assert_system_exit(3) do
|
161
|
+
p = parser
|
162
|
+
Optimist.with_standard_exception_handling(p) do
|
163
|
+
p.die 'cl error', nil, 3
|
164
|
+
end
|
165
|
+
end
|
166
|
+
end
|
167
|
+
end
|
168
|
+
|
169
|
+
def test_with_standard_exception_help_needed
|
170
|
+
assert_stdout(/Options/) do
|
171
|
+
assert_system_exit(0) do
|
172
|
+
p = parser
|
173
|
+
Optimist.with_standard_exception_handling(p) do
|
174
|
+
raise Optimist::HelpNeeded
|
175
|
+
end
|
176
|
+
end
|
177
|
+
end
|
178
|
+
end
|
179
|
+
|
180
|
+
def test_with_standard_exception_help_needed_flag
|
181
|
+
assert_stdout(/Options/) do
|
182
|
+
assert_system_exit(0) do
|
183
|
+
p = parser
|
184
|
+
Optimist.with_standard_exception_handling(p) do
|
185
|
+
p.parse(%w(-h))
|
186
|
+
end
|
187
|
+
end
|
188
|
+
end
|
189
|
+
end
|
190
|
+
end
|
@@ -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
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
$LOAD_PATH.unshift File.expand_path('../../lib', __FILE__)
|
2
|
+
|
3
|
+
unless ENV['MUTANT']
|
4
|
+
begin
|
5
|
+
require "coveralls"
|
6
|
+
Coveralls.wear! do
|
7
|
+
add_filter '/test/'
|
8
|
+
end
|
9
|
+
rescue LoadError
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
begin
|
14
|
+
require "pry"
|
15
|
+
rescue LoadError
|
16
|
+
end
|
17
|
+
|
18
|
+
require 'minitest/autorun'
|
19
|
+
|
20
|
+
Dir[File.expand_path('../support/**/*.rb', __FILE__)].each { |f| require f }
|
21
|
+
|
22
|
+
require 'optimist'
|
metadata
ADDED
@@ -0,0 +1,125 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: optimist
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 3.0.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- William Morgan
|
8
|
+
- Keenan Brock
|
9
|
+
- Jason Frey
|
10
|
+
autorequire:
|
11
|
+
bindir: bin
|
12
|
+
cert_chain: []
|
13
|
+
date: 2018-08-24 00:00:00.000000000 Z
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: minitest
|
17
|
+
requirement: !ruby/object:Gem::Requirement
|
18
|
+
requirements:
|
19
|
+
- - "~>"
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: 5.4.3
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
requirements:
|
26
|
+
- - "~>"
|
27
|
+
- !ruby/object:Gem::Version
|
28
|
+
version: 5.4.3
|
29
|
+
- !ruby/object:Gem::Dependency
|
30
|
+
name: rake
|
31
|
+
requirement: !ruby/object:Gem::Requirement
|
32
|
+
requirements:
|
33
|
+
- - "~>"
|
34
|
+
- !ruby/object:Gem::Version
|
35
|
+
version: '10.0'
|
36
|
+
type: :development
|
37
|
+
prerelease: false
|
38
|
+
version_requirements: !ruby/object:Gem::Requirement
|
39
|
+
requirements:
|
40
|
+
- - "~>"
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: '10.0'
|
43
|
+
- !ruby/object:Gem::Dependency
|
44
|
+
name: chronic
|
45
|
+
requirement: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - ">="
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '0'
|
50
|
+
type: :development
|
51
|
+
prerelease: false
|
52
|
+
version_requirements: !ruby/object:Gem::Requirement
|
53
|
+
requirements:
|
54
|
+
- - ">="
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '0'
|
57
|
+
description: |-
|
58
|
+
Optimist is a commandline option parser for Ruby that just
|
59
|
+
gets out of your way. One line of code per option is all you need to write.
|
60
|
+
For that, you get a nice automatically-generated help page, robust option
|
61
|
+
parsing, command subcompletion, and sensible defaults for everything you don't
|
62
|
+
specify.
|
63
|
+
email: keenan@thebrocks.net
|
64
|
+
executables: []
|
65
|
+
extensions: []
|
66
|
+
extra_rdoc_files: []
|
67
|
+
files:
|
68
|
+
- ".gitignore"
|
69
|
+
- ".travis.yml"
|
70
|
+
- FAQ.txt
|
71
|
+
- Gemfile
|
72
|
+
- History.txt
|
73
|
+
- README.md
|
74
|
+
- Rakefile
|
75
|
+
- lib/optimist.rb
|
76
|
+
- optimist.gemspec
|
77
|
+
- test/optimist/command_line_error_test.rb
|
78
|
+
- test/optimist/help_needed_test.rb
|
79
|
+
- test/optimist/parser_educate_test.rb
|
80
|
+
- test/optimist/parser_opt_test.rb
|
81
|
+
- test/optimist/parser_parse_test.rb
|
82
|
+
- test/optimist/parser_test.rb
|
83
|
+
- test/optimist/version_needed_test.rb
|
84
|
+
- test/optimist_test.rb
|
85
|
+
- test/support/assert_helpers.rb
|
86
|
+
- test/test_helper.rb
|
87
|
+
homepage: http://manageiq.github.io/optimist/
|
88
|
+
licenses:
|
89
|
+
- MIT
|
90
|
+
metadata:
|
91
|
+
changelog_uri: https://github.com/ManageIQ/optimist/blob/master/History.txt
|
92
|
+
source_code_uri: https://github.com/ManageIQ/optimist/
|
93
|
+
bug_tracker_uri: https://github.com/ManageIQ/optimist/issues
|
94
|
+
post_install_message:
|
95
|
+
rdoc_options: []
|
96
|
+
require_paths:
|
97
|
+
- lib
|
98
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
99
|
+
requirements:
|
100
|
+
- - ">="
|
101
|
+
- !ruby/object:Gem::Version
|
102
|
+
version: '0'
|
103
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
104
|
+
requirements:
|
105
|
+
- - ">="
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
requirements: []
|
109
|
+
rubyforge_project:
|
110
|
+
rubygems_version: 2.6.13
|
111
|
+
signing_key:
|
112
|
+
specification_version: 4
|
113
|
+
summary: Optimist is a commandline option parser for Ruby that just gets out of your
|
114
|
+
way.
|
115
|
+
test_files:
|
116
|
+
- test/optimist/command_line_error_test.rb
|
117
|
+
- test/optimist/help_needed_test.rb
|
118
|
+
- test/optimist/parser_educate_test.rb
|
119
|
+
- test/optimist/parser_opt_test.rb
|
120
|
+
- test/optimist/parser_parse_test.rb
|
121
|
+
- test/optimist/parser_test.rb
|
122
|
+
- test/optimist/version_needed_test.rb
|
123
|
+
- test/optimist_test.rb
|
124
|
+
- test/support/assert_helpers.rb
|
125
|
+
- test/test_helper.rb
|