cli-option_parser.rb 0.5.2
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 +7 -0
- data/.bundle/config +2 -0
- data/.document +7 -0
- data/.editorconfig +13 -0
- data/.github/dependabot.yml +6 -0
- data/.github/workflows/test.yml +26 -0
- data/.gitignore +13 -0
- data/.rdoc_options +4 -0
- data/COPYING +56 -0
- data/Gemfile +5 -0
- data/README.md +59 -0
- data/Rakefile +14 -0
- data/cli-option_parser.rb.gemspec +23 -0
- data/doc/optparse/.document +1 -0
- data/doc/optparse/argument_converters.rdoc +380 -0
- data/doc/optparse/creates_option.rdoc +7 -0
- data/doc/optparse/option_params.rdoc +509 -0
- data/doc/optparse/ruby/argument_abbreviation.rb +9 -0
- data/doc/optparse/ruby/argument_keywords.rb +6 -0
- data/doc/optparse/ruby/argument_strings.rb +6 -0
- data/doc/optparse/ruby/argv.rb +2 -0
- data/doc/optparse/ruby/array.rb +6 -0
- data/doc/optparse/ruby/basic.rb +17 -0
- data/doc/optparse/ruby/block.rb +9 -0
- data/doc/optparse/ruby/collected_options.rb +8 -0
- data/doc/optparse/ruby/custom_converter.rb +9 -0
- data/doc/optparse/ruby/date.rb +6 -0
- data/doc/optparse/ruby/datetime.rb +6 -0
- data/doc/optparse/ruby/decimal_integer.rb +7 -0
- data/doc/optparse/ruby/decimal_numeric.rb +7 -0
- data/doc/optparse/ruby/default_values.rb +8 -0
- data/doc/optparse/ruby/descriptions.rb +15 -0
- data/doc/optparse/ruby/explicit_array_values.rb +9 -0
- data/doc/optparse/ruby/explicit_hash_values.rb +9 -0
- data/doc/optparse/ruby/false_class.rb +6 -0
- data/doc/optparse/ruby/float.rb +6 -0
- data/doc/optparse/ruby/help.rb +18 -0
- data/doc/optparse/ruby/help_banner.rb +7 -0
- data/doc/optparse/ruby/help_format.rb +25 -0
- data/doc/optparse/ruby/help_program_name.rb +7 -0
- data/doc/optparse/ruby/integer.rb +6 -0
- data/doc/optparse/ruby/long_names.rb +9 -0
- data/doc/optparse/ruby/long_optional.rb +6 -0
- data/doc/optparse/ruby/long_required.rb +6 -0
- data/doc/optparse/ruby/long_simple.rb +9 -0
- data/doc/optparse/ruby/long_with_negation.rb +6 -0
- data/doc/optparse/ruby/match_converter.rb +9 -0
- data/doc/optparse/ruby/matched_values.rb +6 -0
- data/doc/optparse/ruby/method.rb +11 -0
- data/doc/optparse/ruby/missing_options.rb +12 -0
- data/doc/optparse/ruby/mixed_names.rb +12 -0
- data/doc/optparse/ruby/name_abbrev.rb +9 -0
- data/doc/optparse/ruby/no_abbreviation.rb +10 -0
- data/doc/optparse/ruby/numeric.rb +6 -0
- data/doc/optparse/ruby/object.rb +6 -0
- data/doc/optparse/ruby/octal_integer.rb +7 -0
- data/doc/optparse/ruby/optional_argument.rb +9 -0
- data/doc/optparse/ruby/parse.rb +13 -0
- data/doc/optparse/ruby/parse_bang.rb +13 -0
- data/doc/optparse/ruby/proc.rb +13 -0
- data/doc/optparse/ruby/regexp.rb +6 -0
- data/doc/optparse/ruby/required_argument.rb +9 -0
- data/doc/optparse/ruby/shellwords.rb +6 -0
- data/doc/optparse/ruby/short_names.rb +9 -0
- data/doc/optparse/ruby/short_optional.rb +6 -0
- data/doc/optparse/ruby/short_range.rb +6 -0
- data/doc/optparse/ruby/short_required.rb +6 -0
- data/doc/optparse/ruby/short_simple.rb +9 -0
- data/doc/optparse/ruby/string.rb +6 -0
- data/doc/optparse/ruby/terminator.rb +6 -0
- data/doc/optparse/ruby/time.rb +6 -0
- data/doc/optparse/ruby/true_class.rb +6 -0
- data/doc/optparse/ruby/uri.rb +6 -0
- data/doc/optparse/tutorial.rdoc +858 -0
- data/lib/cli/option_parser/ac.rb +52 -0
- data/lib/cli/option_parser/date.rb +16 -0
- data/lib/cli/option_parser/kwargs.rb +21 -0
- data/lib/cli/option_parser/shellwords.rb +4 -0
- data/lib/cli/option_parser/time.rb +9 -0
- data/lib/cli/option_parser/uri.rb +4 -0
- data/lib/cli/option_parser/version.rb +71 -0
- data/lib/cli/option_parser.rb +2353 -0
- data/lib/cli-option_parser.rb +1 -0
- data/misc/rb_optparse.bash +21 -0
- data/misc/rb_optparse.zsh +39 -0
- data/rakelib/.document +0 -0
- data/rakelib/changelogs.rake +34 -0
- data/rakelib/epoch.rake +5 -0
- data/rakelib/version.rake +51 -0
- data/test/lib/helper.rb +4 -0
- data/test/optparse/test_acceptable.rb +198 -0
- data/test/optparse/test_autoconf.rb +69 -0
- data/test/optparse/test_bash_completion.rb +46 -0
- data/test/optparse/test_cclass.rb +18 -0
- data/test/optparse/test_did_you_mean.rb +48 -0
- data/test/optparse/test_getopts.rb +49 -0
- data/test/optparse/test_kwargs.rb +38 -0
- data/test/optparse/test_load.rb +141 -0
- data/test/optparse/test_noarg.rb +79 -0
- data/test/optparse/test_optarg.rb +60 -0
- data/test/optparse/test_optparse.rb +127 -0
- data/test/optparse/test_placearg.rb +76 -0
- data/test/optparse/test_reqarg.rb +95 -0
- data/test/optparse/test_summary.rb +81 -0
- data/test/optparse/test_zsh_completion.rb +21 -0
- metadata +154 -0
@@ -0,0 +1,141 @@
|
|
1
|
+
# frozen_string_literal: false
|
2
|
+
require 'test/unit'
|
3
|
+
require 'optparse'
|
4
|
+
require 'tmpdir'
|
5
|
+
|
6
|
+
class ::OptionParserLoad < Test::Unit::TestCase
|
7
|
+
def setup
|
8
|
+
@tmpdir = Dir.mktmpdir("optparse_test-")
|
9
|
+
@basename = File.basename($0, '.*')
|
10
|
+
@envs = %w[HOME XDG_CONFIG_HOME XDG_CONFIG_DIRS].each_with_object({}) do |v, h|
|
11
|
+
h[v] = ENV.delete(v)
|
12
|
+
end
|
13
|
+
end
|
14
|
+
|
15
|
+
def teardown
|
16
|
+
ENV.update(@envs)
|
17
|
+
FileUtils.rm_rf(@tmpdir)
|
18
|
+
end
|
19
|
+
|
20
|
+
def new_parser
|
21
|
+
@result = nil
|
22
|
+
CLI::OptionParser.new do |opt|
|
23
|
+
opt.on("--test=arg") {|v| @result = v}
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def assert_load(result)
|
28
|
+
assert new_parser.load
|
29
|
+
assert_equal(result, @result)
|
30
|
+
assert new_parser.load(into: into = {})
|
31
|
+
assert_equal({test: result}, into)
|
32
|
+
end
|
33
|
+
|
34
|
+
def setup_options(env, dir, suffix = nil)
|
35
|
+
optdir = File.join(@tmpdir, dir)
|
36
|
+
FileUtils.mkdir_p(optdir)
|
37
|
+
file = File.join(optdir, [@basename, suffix].join(""))
|
38
|
+
File.write(file, "--test=#{dir}")
|
39
|
+
ENV.update(env)
|
40
|
+
if block_given?
|
41
|
+
begin
|
42
|
+
yield dir, optdir
|
43
|
+
ensure
|
44
|
+
File.unlink(file)
|
45
|
+
Dir.rmdir(optdir) rescue nil
|
46
|
+
end
|
47
|
+
else
|
48
|
+
return dir, optdir
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def setup_options_home(&block)
|
53
|
+
setup_options({'HOME'=>@tmpdir}, ".options", &block)
|
54
|
+
end
|
55
|
+
|
56
|
+
def setup_options_xdg_config_home(&block)
|
57
|
+
setup_options({'XDG_CONFIG_HOME'=>@tmpdir+"/xdg"}, "xdg", ".options", &block)
|
58
|
+
end
|
59
|
+
|
60
|
+
def setup_options_home_config(&block)
|
61
|
+
setup_options({'HOME'=>@tmpdir}, ".config", ".options", &block)
|
62
|
+
end
|
63
|
+
|
64
|
+
def setup_options_xdg_config_dirs(&block)
|
65
|
+
setup_options({'XDG_CONFIG_DIRS'=>@tmpdir+"/xdgconf"}, "xdgconf", ".options", &block)
|
66
|
+
end
|
67
|
+
|
68
|
+
def setup_options_home_config_settings(&block)
|
69
|
+
setup_options({'HOME'=>@tmpdir}, "config/settings", ".options", &block)
|
70
|
+
end
|
71
|
+
|
72
|
+
def test_load_home_options
|
73
|
+
result, = setup_options_home
|
74
|
+
assert_load(result)
|
75
|
+
|
76
|
+
setup_options_xdg_config_home do
|
77
|
+
assert_load(result)
|
78
|
+
end
|
79
|
+
|
80
|
+
setup_options_home_config do
|
81
|
+
assert_load(result)
|
82
|
+
end
|
83
|
+
|
84
|
+
setup_options_xdg_config_dirs do
|
85
|
+
assert_load(result)
|
86
|
+
end
|
87
|
+
|
88
|
+
setup_options_home_config_settings do
|
89
|
+
assert_load(result)
|
90
|
+
end
|
91
|
+
end
|
92
|
+
|
93
|
+
def test_load_xdg_config_home
|
94
|
+
result, = setup_options_xdg_config_home
|
95
|
+
assert_load(result)
|
96
|
+
|
97
|
+
setup_options_home_config do
|
98
|
+
assert_load(result)
|
99
|
+
end
|
100
|
+
|
101
|
+
setup_options_xdg_config_dirs do
|
102
|
+
assert_load(result)
|
103
|
+
end
|
104
|
+
|
105
|
+
setup_options_home_config_settings do
|
106
|
+
assert_load(result)
|
107
|
+
end
|
108
|
+
end
|
109
|
+
|
110
|
+
def test_load_home_config
|
111
|
+
result, = setup_options_home_config
|
112
|
+
assert_load(result)
|
113
|
+
|
114
|
+
setup_options_xdg_config_dirs do
|
115
|
+
assert_load(result)
|
116
|
+
end
|
117
|
+
|
118
|
+
setup_options_home_config_settings do
|
119
|
+
assert_load(result)
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
123
|
+
def test_load_xdg_config_dirs
|
124
|
+
result, = setup_options_xdg_config_dirs
|
125
|
+
assert_load(result)
|
126
|
+
|
127
|
+
setup_options_home_config_settings do
|
128
|
+
assert_load(result)
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
def test_load_home_config_settings
|
133
|
+
result, = setup_options_home_config_settings
|
134
|
+
assert_load(result)
|
135
|
+
end
|
136
|
+
|
137
|
+
def test_load_nothing
|
138
|
+
assert !new_parser.load
|
139
|
+
assert_nil @result
|
140
|
+
end
|
141
|
+
end
|
@@ -0,0 +1,79 @@
|
|
1
|
+
# frozen_string_literal: false
|
2
|
+
require_relative 'test_optparse'
|
3
|
+
|
4
|
+
module ::OptionParserNoArg
|
5
|
+
def setup
|
6
|
+
super
|
7
|
+
@opt.def_option "--with_underscore" do |x| @flag = x end
|
8
|
+
@opt.def_option "--with-hyphen" do |x| @flag = x end
|
9
|
+
end
|
10
|
+
|
11
|
+
class Def1 < ::OptionParser
|
12
|
+
include ::OptionParserNoArg
|
13
|
+
def setup
|
14
|
+
super
|
15
|
+
@opt.def_option("-x") {|x| @flag = x}
|
16
|
+
@opt.def_option("--option") {|x| @flag = x}
|
17
|
+
end
|
18
|
+
end
|
19
|
+
class Def2 < ::OptionParser
|
20
|
+
include ::OptionParserNoArg
|
21
|
+
def setup
|
22
|
+
super
|
23
|
+
@opt.def_option("-x", "--option") {|x| @flag = x}
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_short
|
28
|
+
assert_raise(CLI::OptionParser::InvalidOption) {@opt.parse!(%w"-xq")}
|
29
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"-x")})
|
30
|
+
assert_equal(true, @flag)
|
31
|
+
@flag = nil
|
32
|
+
assert_equal(%w"foo", no_error {@opt.parse!(%w"-x foo")})
|
33
|
+
assert_equal(true, @flag)
|
34
|
+
end
|
35
|
+
|
36
|
+
def test_abbrev
|
37
|
+
assert_raise(CLI::OptionParser::InvalidOption) {@opt.parse!(%w"-oq")}
|
38
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"-o")})
|
39
|
+
assert_equal(true, @flag)
|
40
|
+
@flag = nil
|
41
|
+
assert_raise(CLI::OptionParser::InvalidOption) {@opt.parse!(%w"-O")}
|
42
|
+
assert_nil(@flag)
|
43
|
+
@flag = nil
|
44
|
+
assert_equal(%w"foo", no_error {@opt.parse!(%w"-o foo")})
|
45
|
+
assert_equal(true, @flag)
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_long
|
49
|
+
assert_raise(CLI::OptionParser::NeedlessArgument) {@opt.parse!(%w"--option=x")}
|
50
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--opt")})
|
51
|
+
assert_equal(true, @flag)
|
52
|
+
@flag = nil
|
53
|
+
assert_equal(%w"foo", no_error {@opt.parse!(%w"--opt foo")})
|
54
|
+
assert_equal(true, @flag)
|
55
|
+
end
|
56
|
+
|
57
|
+
def test_ambiguous
|
58
|
+
@opt.def_option("--open") {|x|}
|
59
|
+
assert_raise(CLI::OptionParser::AmbiguousOption) {@opt.parse!(%w"--op")}
|
60
|
+
assert_raise(CLI::OptionParser::AmbiguousOption) {@opt.parse!(%w"-o")}
|
61
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--opt")})
|
62
|
+
assert_equal(true, @flag)
|
63
|
+
end
|
64
|
+
|
65
|
+
def test_hyphenize
|
66
|
+
@flag = nil
|
67
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--with_underscore")})
|
68
|
+
assert_equal(true, @flag)
|
69
|
+
@flag = nil
|
70
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--with-underscore")})
|
71
|
+
assert_equal(true, @flag)
|
72
|
+
@flag = nil
|
73
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--with-hyphen")})
|
74
|
+
assert_equal(true, @flag)
|
75
|
+
@flag = nil
|
76
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--with_hyphen")})
|
77
|
+
assert_equal(true, @flag)
|
78
|
+
end
|
79
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
# frozen_string_literal: false
|
2
|
+
require_relative 'test_optparse'
|
3
|
+
|
4
|
+
class ::OptionParserOptArg < ::OptionParser
|
5
|
+
def setup
|
6
|
+
super
|
7
|
+
@opt.def_option("-x[VAL]") {|x| @flag = x}
|
8
|
+
@opt.def_option("--option[=VAL]") {|x| @flag = x}
|
9
|
+
@opt.def_option("--regexp[=REGEXP]", Regexp) {|x| @reopt = x}
|
10
|
+
@opt.def_option "--with_underscore[=VAL]" do |x| @flag = x end
|
11
|
+
@opt.def_option "--with-hyphen[=VAL]" do |x| @flag = x end
|
12
|
+
@reopt = nil
|
13
|
+
end
|
14
|
+
|
15
|
+
def test_short
|
16
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"-x")})
|
17
|
+
assert_equal(nil, @flag)
|
18
|
+
@flag = false
|
19
|
+
assert_equal(%w"foo", no_error {@opt.parse!(%w"-x foo")})
|
20
|
+
assert_equal(nil, @flag)
|
21
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"-xfoo")})
|
22
|
+
assert_equal("foo", @flag)
|
23
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"-x=")})
|
24
|
+
assert_equal("=", @flag)
|
25
|
+
end
|
26
|
+
|
27
|
+
def test_abbrev
|
28
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"-o")})
|
29
|
+
assert_equal(nil, @flag)
|
30
|
+
@flag = false
|
31
|
+
assert_equal(%w"foo", no_error {@opt.parse!(%w"-o foo")})
|
32
|
+
assert_equal(nil, @flag)
|
33
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"-ofoo")})
|
34
|
+
assert_equal("foo", @flag)
|
35
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"-o=")})
|
36
|
+
assert_equal("=", @flag)
|
37
|
+
end
|
38
|
+
|
39
|
+
def test_long
|
40
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--opt")})
|
41
|
+
assert_equal(nil, @flag)
|
42
|
+
assert_equal(%w"foo", no_error {@opt.parse!(%w"--opt= foo")})
|
43
|
+
assert_equal("", @flag)
|
44
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--opt=foo")})
|
45
|
+
assert_equal("foo", @flag)
|
46
|
+
assert_equal(%w"foo", no_error {@opt.parse!(%w"--opt foo")})
|
47
|
+
assert_equal(nil, @flag)
|
48
|
+
end
|
49
|
+
|
50
|
+
def test_hyphenize
|
51
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--with_underscore=foo1")})
|
52
|
+
assert_equal("foo1", @flag)
|
53
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--with-underscore=foo2")})
|
54
|
+
assert_equal("foo2", @flag)
|
55
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--with-hyphen=foo3")})
|
56
|
+
assert_equal("foo3", @flag)
|
57
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--with_hyphen=foo4")})
|
58
|
+
assert_equal("foo4", @flag)
|
59
|
+
end
|
60
|
+
end
|
@@ -0,0 +1,127 @@
|
|
1
|
+
# frozen_string_literal: false
|
2
|
+
require 'test/unit'
|
3
|
+
require 'cli-option_parser'
|
4
|
+
|
5
|
+
class ::OptionParserTest < Test::Unit::TestCase
|
6
|
+
def setup
|
7
|
+
@opt = CLI::OptionParser.new
|
8
|
+
@flag = self.class # cannot set by option
|
9
|
+
end
|
10
|
+
|
11
|
+
class DummyOutput < String
|
12
|
+
alias write concat
|
13
|
+
end
|
14
|
+
def assert_no_error(*args)
|
15
|
+
$stderr, stderr = DummyOutput.new, $stderr
|
16
|
+
assert_nothing_raised(*args) {return yield}
|
17
|
+
ensure
|
18
|
+
stderr, $stderr = $stderr, stderr
|
19
|
+
$!.backtrace.delete_if {|e| /\A#{Regexp.quote(__FILE__)}:#{__LINE__-2}/o =~ e} if $!
|
20
|
+
assert_empty(stderr)
|
21
|
+
end
|
22
|
+
alias no_error assert_no_error
|
23
|
+
|
24
|
+
def test_permute
|
25
|
+
assert_equal(%w"", no_error {@opt.permute!(%w"")})
|
26
|
+
assert_equal(self.class, @flag)
|
27
|
+
assert_equal(%w"foo bar", no_error {@opt.permute!(%w"foo bar")})
|
28
|
+
assert_equal(self.class, @flag)
|
29
|
+
assert_equal(%w"- foo bar", no_error {@opt.permute!(%w"- foo bar")})
|
30
|
+
assert_equal(self.class, @flag)
|
31
|
+
assert_equal(%w"foo bar", no_error {@opt.permute!(%w"-- foo bar")})
|
32
|
+
assert_equal(self.class, @flag)
|
33
|
+
assert_equal(%w"foo - bar", no_error {@opt.permute!(%w"foo - bar")})
|
34
|
+
assert_equal(self.class, @flag)
|
35
|
+
assert_equal(%w"foo bar", no_error {@opt.permute!(%w"foo -- bar")})
|
36
|
+
assert_equal(self.class, @flag)
|
37
|
+
assert_equal(%w"foo --help bar", no_error {@opt.permute!(%w"foo -- --help bar")})
|
38
|
+
assert_equal(self.class, @flag)
|
39
|
+
end
|
40
|
+
|
41
|
+
def test_order
|
42
|
+
assert_equal(%w"", no_error {@opt.order!(%w"")})
|
43
|
+
assert_equal(self.class, @flag)
|
44
|
+
assert_equal(%w"foo bar", no_error {@opt.order!(%w"foo bar")})
|
45
|
+
assert_equal(self.class, @flag)
|
46
|
+
assert_equal(%w"- foo bar", no_error {@opt.order!(%w"- foo bar")})
|
47
|
+
assert_equal(self.class, @flag)
|
48
|
+
assert_equal(%w"foo bar", no_error {@opt.permute!(%w"-- foo bar")})
|
49
|
+
assert_equal(self.class, @flag)
|
50
|
+
assert_equal(%w"foo - bar", no_error {@opt.order!(%w"foo - bar")})
|
51
|
+
assert_equal(self.class, @flag)
|
52
|
+
assert_equal(%w"foo -- bar", no_error {@opt.order!(%w"foo -- bar")})
|
53
|
+
assert_equal(self.class, @flag)
|
54
|
+
assert_equal(%w"foo -- --help bar", no_error {@opt.order!(%w"foo -- --help bar")})
|
55
|
+
assert_equal(self.class, @flag)
|
56
|
+
end
|
57
|
+
|
58
|
+
def test_regexp
|
59
|
+
return unless defined?(@reopt)
|
60
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--regexp=/foo/")})
|
61
|
+
assert_equal(/foo/, @reopt)
|
62
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--regexp=/foo/i")})
|
63
|
+
assert_equal(/foo/i, @reopt)
|
64
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--regexp=/foo/n")})
|
65
|
+
assert_equal(/foo/n, @reopt)
|
66
|
+
assert_equal(%w"", no_error {@opt.parse!(%W"--regexp=/\u{3042}/s")})
|
67
|
+
assert_equal(Encoding::Windows_31J, @reopt.encoding)
|
68
|
+
assert_equal("\x82\xa0".force_encoding(Encoding::Windows_31J), @reopt.source)
|
69
|
+
end
|
70
|
+
|
71
|
+
def test_into
|
72
|
+
@opt.def_option "-h", "--host=HOST", "hostname"
|
73
|
+
@opt.def_option "-p", "--port=PORT", "port", Integer
|
74
|
+
@opt.def_option "-v", "--verbose" do @verbose = true end
|
75
|
+
@opt.def_option "-q", "--quiet" do @quiet = true end
|
76
|
+
result = {}
|
77
|
+
@opt.parse %w(--host localhost --port 8000 -v), into: result
|
78
|
+
assert_equal({
|
79
|
+
h: "localhost", host: "localhost",
|
80
|
+
p: 8000, port: 8000,
|
81
|
+
v: true, verbose: true
|
82
|
+
}, result)
|
83
|
+
assert_equal(true, @verbose)
|
84
|
+
end
|
85
|
+
|
86
|
+
def test_require_exact
|
87
|
+
@opt.def_option('-F', '--zrs=IRS', 'zrs')
|
88
|
+
%w(--zrs --zr --z -zfoo -z -F -Ffoo).each do |arg|
|
89
|
+
result = {}
|
90
|
+
@opt.parse([arg, 'foo'], into: result)
|
91
|
+
assert_equal({F: 'foo', zrs: 'foo'}, result)
|
92
|
+
end
|
93
|
+
|
94
|
+
@opt.require_exact = true
|
95
|
+
%w(--zrs -F -Ffoo).each do |arg|
|
96
|
+
result = {}
|
97
|
+
@opt.parse([arg, 'foo'], into: result)
|
98
|
+
assert_equal({F: 'foo', zrs: 'foo'}, result)
|
99
|
+
end
|
100
|
+
|
101
|
+
assert_raise(CLI::OptionParser::InvalidOption) {@opt.parse(%w(--zr foo))}
|
102
|
+
assert_raise(CLI::OptionParser::InvalidOption) {@opt.parse(%w(--z foo))}
|
103
|
+
assert_raise(CLI::OptionParser::InvalidOption) {@opt.parse(%w(-zrs foo))}
|
104
|
+
assert_raise(CLI::OptionParser::InvalidOption) {@opt.parse(%w(-zr foo))}
|
105
|
+
assert_raise(CLI::OptionParser::InvalidOption) {@opt.parse(%w(-z foo))}
|
106
|
+
end
|
107
|
+
|
108
|
+
def test_raise_unknown
|
109
|
+
@opt.def_option('--my-foo [ARG]') {|arg| @foo = arg}
|
110
|
+
assert @opt.raise_unknown
|
111
|
+
|
112
|
+
@opt.raise_unknown = false
|
113
|
+
assert_equal(%w[--my-bar], @opt.parse(%w[--my-foo --my-bar]))
|
114
|
+
assert_nil(@foo)
|
115
|
+
|
116
|
+
assert_equal(%w[--my-bar], @opt.parse(%w[--my-foo x --my-bar]))
|
117
|
+
assert_equal("x", @foo)
|
118
|
+
end
|
119
|
+
|
120
|
+
def test_nonopt_pattern
|
121
|
+
@opt.def_option(/^[^-]/) do |arg|
|
122
|
+
assert(false, "Never gets called")
|
123
|
+
end
|
124
|
+
e = assert_raise(CLI::OptionParser::InvalidOption) {@opt.parse(%w(-t))}
|
125
|
+
assert_equal(["-t"], e.args)
|
126
|
+
end
|
127
|
+
end
|
@@ -0,0 +1,76 @@
|
|
1
|
+
# frozen_string_literal: false
|
2
|
+
require_relative 'test_optparse'
|
3
|
+
|
4
|
+
class ::OptionParserPlaceArg < ::OptionParser
|
5
|
+
def setup
|
6
|
+
super
|
7
|
+
@opt.def_option("-x [VAL]") {|x| @flag = x}
|
8
|
+
@opt.def_option("--option [VAL]") {|x| @flag = x}
|
9
|
+
@opt.def_option("-T [level]", /^[0-4]$/, Integer) {|x| @topt = x}
|
10
|
+
@topt = nil
|
11
|
+
@opt.def_option("-n") {}
|
12
|
+
@opt.def_option("--regexp [REGEXP]", Regexp) {|x| @reopt = x}
|
13
|
+
@reopt = nil
|
14
|
+
@opt.def_option "--with_underscore=VAL" do |x| @flag = x end
|
15
|
+
@opt.def_option "--with-hyphen=VAL" do |x| @flag = x end
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_short
|
19
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"-x -n")})
|
20
|
+
assert_equal(nil, @flag)
|
21
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"-x -")})
|
22
|
+
assert_equal("-", @flag)
|
23
|
+
@flag = false
|
24
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"-x foo")})
|
25
|
+
assert_equal("foo", @flag)
|
26
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"-xbar")})
|
27
|
+
assert_equal("bar", @flag)
|
28
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"-x=")})
|
29
|
+
assert_equal("=", @flag)
|
30
|
+
end
|
31
|
+
|
32
|
+
def test_abbrev
|
33
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"-o -n")})
|
34
|
+
assert_equal(nil, @flag)
|
35
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"-o -")})
|
36
|
+
assert_equal("-", @flag)
|
37
|
+
@flag = false
|
38
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"-o foo")})
|
39
|
+
assert_equal("foo", @flag)
|
40
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"-obar")})
|
41
|
+
assert_equal("bar", @flag)
|
42
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"-o=")})
|
43
|
+
assert_equal("=", @flag)
|
44
|
+
end
|
45
|
+
|
46
|
+
def test_long
|
47
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--opt -n")})
|
48
|
+
assert_equal(nil, @flag)
|
49
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--opt -")})
|
50
|
+
assert_equal("-", @flag)
|
51
|
+
assert_equal(%w"foo", no_error {@opt.parse!(%w"--opt= foo")})
|
52
|
+
assert_equal("", @flag)
|
53
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--opt=foo")})
|
54
|
+
assert_equal("foo", @flag)
|
55
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--opt bar")})
|
56
|
+
assert_equal("bar", @flag)
|
57
|
+
end
|
58
|
+
|
59
|
+
def test_hyphenize
|
60
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--with_underscore=foo1")})
|
61
|
+
assert_equal("foo1", @flag)
|
62
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--with-underscore=foo2")})
|
63
|
+
assert_equal("foo2", @flag)
|
64
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--with-hyphen=foo3")})
|
65
|
+
assert_equal("foo3", @flag)
|
66
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--with_hyphen=foo4")})
|
67
|
+
assert_equal("foo4", @flag)
|
68
|
+
end
|
69
|
+
|
70
|
+
def test_conv
|
71
|
+
assert_equal(%w"te.rb", no_error('[ruby-dev:38333]') {@opt.parse!(%w"-T te.rb")})
|
72
|
+
assert_nil(@topt)
|
73
|
+
assert_equal(%w"te.rb", no_error('[ruby-dev:38333]') {@opt.parse!(%w"-T1 te.rb")})
|
74
|
+
assert_equal(1, @topt)
|
75
|
+
end
|
76
|
+
end
|
@@ -0,0 +1,95 @@
|
|
1
|
+
# frozen_string_literal: false
|
2
|
+
require_relative 'test_optparse'
|
3
|
+
|
4
|
+
module ::OptionParserReqArg
|
5
|
+
def setup
|
6
|
+
super
|
7
|
+
@opt.def_option "--with_underscore=VAL" do |x| @flag = x end
|
8
|
+
@opt.def_option "--with-hyphen=VAL" do |x| @flag = x end
|
9
|
+
end
|
10
|
+
|
11
|
+
class Def1 < ::OptionParser
|
12
|
+
include ::OptionParserReqArg
|
13
|
+
def setup
|
14
|
+
super
|
15
|
+
@opt.def_option("-xVAL") {|x| @flag = x}
|
16
|
+
@opt.def_option("--option=VAL") {|x| @flag = x}
|
17
|
+
@opt.def_option("--regexp=REGEXP", Regexp) {|x| @reopt = x}
|
18
|
+
@reopt = nil
|
19
|
+
end
|
20
|
+
end
|
21
|
+
class Def2 < ::OptionParser
|
22
|
+
include ::OptionParserReqArg
|
23
|
+
def setup
|
24
|
+
super
|
25
|
+
@opt.def_option("-x", "--option=VAL") {|x| @flag = x}
|
26
|
+
end
|
27
|
+
end
|
28
|
+
class Def3 < ::OptionParser
|
29
|
+
include ::OptionParserReqArg
|
30
|
+
def setup
|
31
|
+
super
|
32
|
+
@opt.def_option("--option=VAL", "-x") {|x| @flag = x}
|
33
|
+
end
|
34
|
+
end
|
35
|
+
class Def4 < ::OptionParser
|
36
|
+
include ::OptionParserReqArg
|
37
|
+
def setup
|
38
|
+
super
|
39
|
+
@opt.def_option("-xVAL", "--option=VAL") {|x| @flag = x}
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_short
|
44
|
+
assert_raise(CLI::OptionParser::MissingArgument) {@opt.parse!(%w"-x")}
|
45
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"-x foo")})
|
46
|
+
assert_equal("foo", @flag)
|
47
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"-xbar")})
|
48
|
+
assert_equal("bar", @flag)
|
49
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"-x=")})
|
50
|
+
assert_equal("=", @flag)
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_abbrev
|
54
|
+
assert_raise(CLI::OptionParser::MissingArgument) {@opt.parse!(%w"-o")}
|
55
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"-o foo")})
|
56
|
+
assert_equal("foo", @flag)
|
57
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"-obar")})
|
58
|
+
assert_equal("bar", @flag)
|
59
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"-o=")})
|
60
|
+
assert_equal("=", @flag)
|
61
|
+
end
|
62
|
+
|
63
|
+
def test_long
|
64
|
+
assert_raise(CLI::OptionParser::MissingArgument) {@opt.parse!(%w"--opt")}
|
65
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--opt foo")})
|
66
|
+
assert_equal("foo", @flag)
|
67
|
+
assert_equal(%w"foo", no_error {@opt.parse!(%w"--opt= foo")})
|
68
|
+
assert_equal("", @flag)
|
69
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--opt=foo")})
|
70
|
+
assert_equal("foo", @flag)
|
71
|
+
end
|
72
|
+
|
73
|
+
def test_hyphenize
|
74
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--with_underscore foo1")})
|
75
|
+
assert_equal("foo1", @flag)
|
76
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--with-underscore foo2")})
|
77
|
+
assert_equal("foo2", @flag)
|
78
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--with-hyphen foo3")})
|
79
|
+
assert_equal("foo3", @flag)
|
80
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--with_hyphen foo4")})
|
81
|
+
assert_equal("foo4", @flag)
|
82
|
+
end
|
83
|
+
|
84
|
+
class ::OptionParser::WithPattern < ::OptionParser
|
85
|
+
def test_pattern
|
86
|
+
pat = num = nil
|
87
|
+
@opt.def_option("--pattern=VAL", /(\w+)(?:\s*:\s*(\w+))?/) {|x, y, z| pat = [x, y, z]}
|
88
|
+
@opt.def_option("-T NUM", /\A[1-4]\z/) {|n| num = n}
|
89
|
+
no_error {@opt.parse!(%w"--pattern=key:val")}
|
90
|
+
assert_equal(%w"key:val key val", pat, '[ruby-list:45645]')
|
91
|
+
no_error {@opt.parse!(%w"-T 4")}
|
92
|
+
assert_equal("4", num, '[ruby-dev:37514]')
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
@@ -0,0 +1,81 @@
|
|
1
|
+
# frozen_string_literal: false
|
2
|
+
require_relative 'test_optparse'
|
3
|
+
|
4
|
+
class ::OptionParserSummaryTest < ::OptionParser
|
5
|
+
def test_short_clash
|
6
|
+
r = nil
|
7
|
+
o = CLI::OptionParser.new do |opts|
|
8
|
+
opts.on("-f", "--first-option", "description 1", "description 2"){r = "first-option"}
|
9
|
+
opts.on("-t", "--test-option"){r = "test-option"}
|
10
|
+
opts.on("-t", "--another-test-option"){r = "another-test-option"}
|
11
|
+
opts.separator "this is\nseparator"
|
12
|
+
opts.on("-l", "--last-option"){r = "last-option"}
|
13
|
+
end
|
14
|
+
s = o.summarize
|
15
|
+
o.parse("-t")
|
16
|
+
assert_match(/--#{r}/, s.grep(/^\s*-t,/)[0])
|
17
|
+
assert_match(/first-option/, s[0])
|
18
|
+
assert_match(/description 1/, s[0])
|
19
|
+
assert_match(/description 2/, s[1])
|
20
|
+
assert_match(/last-option/, s[-1])
|
21
|
+
end
|
22
|
+
|
23
|
+
def test_banner
|
24
|
+
o = CLI::OptionParser.new("foo bar")
|
25
|
+
assert_equal("foo bar", o.banner)
|
26
|
+
end
|
27
|
+
|
28
|
+
def test_banner_from_progname
|
29
|
+
o = CLI::OptionParser.new
|
30
|
+
o.program_name = "foobar"
|
31
|
+
assert_equal("Usage: foobar [options]\n", o.help)
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_summary
|
35
|
+
o = CLI::OptionParser.new("foo\nbar")
|
36
|
+
assert_equal("foo\nbar\n", o.to_s)
|
37
|
+
assert_equal(["foo\n", "bar"], o.to_a)
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_summary_containing_space
|
41
|
+
# test for r35467. CLI::OptionParser#to_a shouldn't split str by spaces.
|
42
|
+
bug6348 = '[ruby-dev:45568]'
|
43
|
+
o = CLI::OptionParser.new("foo bar")
|
44
|
+
assert_equal("foo bar\n", o.to_s, bug6348)
|
45
|
+
assert_equal(["foo bar"], o.to_a, bug6348)
|
46
|
+
end
|
47
|
+
|
48
|
+
def test_ver
|
49
|
+
o = CLI::OptionParser.new("foo bar")
|
50
|
+
o.program_name = "foo"
|
51
|
+
assert_warning('') {assert_nil(o.version)}
|
52
|
+
assert_warning('') {assert_nil(o.release)}
|
53
|
+
o.version = [0, 1]
|
54
|
+
assert_equal "foo 0.1", o.ver
|
55
|
+
o.release = "rel"
|
56
|
+
assert_equal "foo 0.1 (rel)", o.ver
|
57
|
+
end
|
58
|
+
|
59
|
+
# https://github.com/ruby/optparse/issues/37
|
60
|
+
def test_very_long_without_short
|
61
|
+
o = CLI::OptionParser.new do |opts|
|
62
|
+
# This causes TypeError
|
63
|
+
opts.on('', '--long-long-option-param-without-short', "Error desc") { options[:long_long_option_param_without_short] = true }
|
64
|
+
opts.on('', '--long-option-param', "Long desc") { options[:long_option_param_without_short] = true }
|
65
|
+
opts.on('-a', '--long-long-option-param-with-short', "Normal description") { options[:long_long_option_param_with_short] = true }
|
66
|
+
|
67
|
+
opts.on('', '--long-long-option-param-without-short-but-with-desc', 'Description of the long long param') { options[:long_long_option_param_without_short_but_with_desc] = true }
|
68
|
+
end
|
69
|
+
|
70
|
+
s = o.summarize
|
71
|
+
|
72
|
+
assert_match(/^\s*--long-long-option-param-without-short$/, s[0])
|
73
|
+
assert_match(/^\s*Error desc$/, s[1])
|
74
|
+
assert_match(/^\s*--long-option-param\s+Long desc$/, s[2])
|
75
|
+
assert_match(/^\s*-a\s+Normal description$/, s[3])
|
76
|
+
assert_match(/^\s*--long-long-option-param-with-short$/, s[4])
|
77
|
+
|
78
|
+
assert_match(/^\s*--long-long-option-param-without-short-but-with-desc$/, s[5])
|
79
|
+
assert_match(/^\s*Description of the long long param$/, s[6])
|
80
|
+
end
|
81
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: false
|
2
|
+
require 'test/unit'
|
3
|
+
require 'optparse'
|
4
|
+
|
5
|
+
class ::OptionParserZshCompletion < Test::Unit::TestCase
|
6
|
+
def setup
|
7
|
+
@opt = CLI::OptionParser.new
|
8
|
+
@opt.define("-z", "zzz") {}
|
9
|
+
@opt.define("--foo") {}
|
10
|
+
@opt.define("--bar=BAR") {}
|
11
|
+
@opt.define("--for=TYPE", [:hello, :help, :zot]) {}
|
12
|
+
end
|
13
|
+
|
14
|
+
def test_compsys
|
15
|
+
compsys = @opt.compsys("", "zshcompsys")
|
16
|
+
assert_match(/\"-z\[zzz\]\"/, compsys)
|
17
|
+
assert_match(/\"--foo\[\]\"/, compsys)
|
18
|
+
assert_match(/\"--bar\[\]\"/, compsys)
|
19
|
+
assert_match(/\"--for\[\]\"/, compsys)
|
20
|
+
end
|
21
|
+
end
|