cmd-optparse.rb 0.1.0
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 +12 -0
- data/.rdoc_options +4 -0
- data/COPYING +56 -0
- data/Gemfile +5 -0
- data/README.md +59 -0
- data/Rakefile +14 -0
- data/cmd-optparse.rb.gemspec +30 -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/optionparser.rb +2 -0
- data/lib/optparse/ac.rb +54 -0
- data/lib/optparse/date.rb +18 -0
- data/lib/optparse/kwargs.rb +22 -0
- data/lib/optparse/shellwords.rb +7 -0
- data/lib/optparse/time.rb +11 -0
- data/lib/optparse/uri.rb +7 -0
- data/lib/optparse/version.rb +71 -0
- data/lib/optparse.rb +2346 -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,21 @@
|
|
1
|
+
# -*- bash -*-
|
2
|
+
#
|
3
|
+
# Completion for bash:
|
4
|
+
#
|
5
|
+
# (1) install this file,
|
6
|
+
#
|
7
|
+
# (2) load the script, and
|
8
|
+
# . ~/.profile.d/rb_optparse.bash
|
9
|
+
#
|
10
|
+
# (3) define completions in your .bashrc,
|
11
|
+
# rb_optparse command_using_optparse_1
|
12
|
+
# rb_optparse command_using_optparse_2
|
13
|
+
|
14
|
+
_rb_optparse() {
|
15
|
+
COMPREPLY=($("${COMP_WORDS[0]}" "--*-completion-bash=${COMP_WORDS[COMP_CWORD]}"))
|
16
|
+
return 0
|
17
|
+
}
|
18
|
+
|
19
|
+
rb_optparse () {
|
20
|
+
[ $# = 0 ] || complete -o default -F _rb_optparse "$@"
|
21
|
+
}
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# -*- zsh -*-
|
2
|
+
#
|
3
|
+
# Completion for zsh:
|
4
|
+
# (based on <http://d.hatena.ne.jp/rubikitch/20071002/zshcomplete>)
|
5
|
+
#
|
6
|
+
# (1) install this file.
|
7
|
+
# mkdir -p ~/.zsh.d
|
8
|
+
# cp rb_optparse.zsh ~/.zsh.d/rb_optparse.zsh
|
9
|
+
#
|
10
|
+
# (2) load the script, and add a directory to fpath before compinit.
|
11
|
+
# echo '. ~/.zsh.d/rb_optparse.zsh' >> "${ZDOTDIR:-~}/.zshrc"
|
12
|
+
# echo 'fpath=(~/.zsh.d/Completion $fpath)' >> "${ZDOTDIR:-~}/.zshrc"
|
13
|
+
# echo 'autoload -U compinit; compinit' >> "${ZDOTDIR:-~}/.zshrc"
|
14
|
+
#
|
15
|
+
# (3) restart zsh.
|
16
|
+
#
|
17
|
+
# (4) generate completion files once.
|
18
|
+
# generate-complete-function/ruby/optparse COMMAND1
|
19
|
+
# generate-complete-function/ruby/optparse COMMAND2
|
20
|
+
#
|
21
|
+
|
22
|
+
generate-complete-function/ruby/optparse ()
|
23
|
+
{
|
24
|
+
local cmpl="_${1:t}"
|
25
|
+
mkdir -p "${ZSH_COMPLETION_DIR-$HOME/.zsh.d/Completion}"
|
26
|
+
$1 "--*-completion-zsh=${1:t}" >! "${ZSH_COMPLETION_DIR-$HOME/.zsh.d/Completion}/$cmpl"
|
27
|
+
if [[ $(type -w "$cmpl") == "${cmpl}: function" ]]; then
|
28
|
+
unfunction "$cmpl"
|
29
|
+
autoload -U "$cmpl"
|
30
|
+
else
|
31
|
+
compinit "$cmpl"
|
32
|
+
fi
|
33
|
+
}
|
34
|
+
|
35
|
+
compdef _command generate-complete-function/ruby/optparse
|
36
|
+
|
37
|
+
for cmd in "$@"; do
|
38
|
+
generate-complete-function/ruby/optparse "$cmd"
|
39
|
+
done
|
data/rakelib/.document
ADDED
File without changes
|
@@ -0,0 +1,34 @@
|
|
1
|
+
task "build" => "changelogs"
|
2
|
+
|
3
|
+
changelog = proc do |output, ver = nil, prev = nil|
|
4
|
+
ver &&= Gem::Version.new(ver)
|
5
|
+
range = [[prev], [ver, "HEAD"]].map {|ver, branch| ver ? "v#{ver.to_s}" : branch}.compact.join("..")
|
6
|
+
IO.popen(%W[git log --format=fuller --topo-order --no-merges #{range}]) do |log|
|
7
|
+
line = log.gets
|
8
|
+
FileUtils.mkpath(File.dirname(output))
|
9
|
+
File.open(output, "wb") do |f|
|
10
|
+
f.print "-*- coding: utf-8 -*-\n\n", line
|
11
|
+
log.each_line do |line|
|
12
|
+
line.sub!(/^(?!:)(?:Author|Commit)?(?:Date)?: /, ' \&')
|
13
|
+
line.sub!(/ +$/, '')
|
14
|
+
f.print(line)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
tags = IO.popen(%w[git tag -l v[0-9]*]).grep(/v(.*)/) {$1}
|
21
|
+
tags.sort_by! {|tag| tag.scan(/\d+/).map(&:to_i)}
|
22
|
+
tags.inject(nil) do |prev, tag|
|
23
|
+
task("logs/ChangeLog-#{tag}") {|t| changelog[t.name, tag, prev]}
|
24
|
+
tag
|
25
|
+
end
|
26
|
+
|
27
|
+
desc "Make ChangeLog"
|
28
|
+
task "ChangeLog", [:ver, :prev] do |t, ver: nil, prev: tags.last|
|
29
|
+
changelog[t.name, ver, prev]
|
30
|
+
end
|
31
|
+
|
32
|
+
changelogs = ["ChangeLog", *tags.map {|tag| "logs/ChangeLog-#{tag}"}]
|
33
|
+
task "changelogs" => changelogs
|
34
|
+
CLOBBER.concat(changelogs) << "logs"
|
data/rakelib/epoch.rake
ADDED
@@ -0,0 +1,51 @@
|
|
1
|
+
class << (helper = Bundler::GemHelper.instance)
|
2
|
+
def mainfile
|
3
|
+
"lib/#{File.basename(gemspec.loaded_from, ".gemspec")}.rb"
|
4
|
+
end
|
5
|
+
|
6
|
+
def update_version
|
7
|
+
File.open(mainfile, "r+b") do |f|
|
8
|
+
d = f.read
|
9
|
+
if d.sub!(/^(\s*OptionParser::Version\s*=\s*)".*"/) {$1 + gemspec.version.to_s.dump}
|
10
|
+
f.rewind
|
11
|
+
f.truncate(0)
|
12
|
+
f.print(d)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
def commit_bump
|
18
|
+
sh(%W[git -C #{File.dirname(gemspec.loaded_from)} commit -m bump\ up\ to\ #{gemspec.version}
|
19
|
+
#{mainfile}])
|
20
|
+
end
|
21
|
+
|
22
|
+
def version=(v)
|
23
|
+
gemspec.version = v
|
24
|
+
update_version
|
25
|
+
commit_bump
|
26
|
+
end
|
27
|
+
|
28
|
+
def bump(major, minor = 0, teeny = 0, pre: nil)
|
29
|
+
self.version = [major, minor, teeny, pre].compact.join(".")
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
major, minor, teeny = helper.gemspec.version.segments
|
34
|
+
|
35
|
+
task "bump:teeny", [:pre] do |t, pre: nil|
|
36
|
+
helper.bump(major, minor, teeny+1, pre: pre)
|
37
|
+
end
|
38
|
+
|
39
|
+
task "bump:minor", [:pre] do |t, pre: nil|
|
40
|
+
helper.bump(major, minor+1, pre: pre)
|
41
|
+
end
|
42
|
+
|
43
|
+
task "bump:major", [:pre] do |t, pre: nil|
|
44
|
+
helper.bump(major+1, pre: pre)
|
45
|
+
end
|
46
|
+
|
47
|
+
task "bump" => "bump:teeny"
|
48
|
+
|
49
|
+
task "tag" do
|
50
|
+
helper.__send__(:tag_version)
|
51
|
+
end
|
data/test/lib/helper.rb
ADDED
@@ -0,0 +1,198 @@
|
|
1
|
+
# frozen_string_literal: false
|
2
|
+
require_relative 'test_optparse'
|
3
|
+
|
4
|
+
class TestOptionParserAcceptable < TestOptionParser
|
5
|
+
|
6
|
+
def setup
|
7
|
+
super
|
8
|
+
@opt.def_option("--integer VAL", Integer) { |v| @integer = v }
|
9
|
+
@opt.def_option("--float VAL", Float) { |v| @float = v }
|
10
|
+
@opt.def_option("--numeric VAL", Numeric) { |v| @numeric = v }
|
11
|
+
|
12
|
+
@opt.def_option("--decimal-integer VAL",
|
13
|
+
OptionParser::DecimalInteger) { |i| @decimal_integer = i }
|
14
|
+
@opt.def_option("--octal-integer VAL",
|
15
|
+
OptionParser::OctalInteger) { |i| @octal_integer = i }
|
16
|
+
@opt.def_option("--decimal-numeric VAL",
|
17
|
+
OptionParser::DecimalNumeric) { |i| @decimal_numeric = i }
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_integer
|
21
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--integer 0")})
|
22
|
+
assert_equal(0, @integer)
|
23
|
+
|
24
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--integer 0b10")})
|
25
|
+
assert_equal(2, @integer)
|
26
|
+
|
27
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--integer 077")})
|
28
|
+
assert_equal(63, @integer)
|
29
|
+
|
30
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--integer 10")})
|
31
|
+
assert_equal(10, @integer)
|
32
|
+
|
33
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--integer 0x3")})
|
34
|
+
assert_equal(3, @integer)
|
35
|
+
|
36
|
+
assert_raise(OptionParser::InvalidArgument) do
|
37
|
+
@opt.parse!(%w"--integer 0b")
|
38
|
+
end
|
39
|
+
|
40
|
+
assert_raise(OptionParser::InvalidArgument) do
|
41
|
+
@opt.parse!(%w"--integer 09")
|
42
|
+
end
|
43
|
+
|
44
|
+
assert_raise(OptionParser::InvalidArgument) do
|
45
|
+
@opt.parse!(%w"--integer 0x")
|
46
|
+
end
|
47
|
+
|
48
|
+
assert_raise(OptionParser::InvalidArgument) do
|
49
|
+
@opt.parse!(%w"--integer 1234xyz")
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
def test_float
|
54
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--float 0")})
|
55
|
+
assert_in_epsilon(0.0, @float)
|
56
|
+
|
57
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--float 0.0")})
|
58
|
+
assert_in_epsilon(0.0, @float)
|
59
|
+
|
60
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--float 1.2")})
|
61
|
+
assert_in_epsilon(1.2, @float)
|
62
|
+
|
63
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--float 1E2")})
|
64
|
+
assert_in_epsilon(100, @float)
|
65
|
+
|
66
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--float 1E-2")})
|
67
|
+
assert_in_epsilon(0.01, @float)
|
68
|
+
|
69
|
+
assert_raise(OptionParser::InvalidArgument) do
|
70
|
+
@opt.parse!(%w"--float 0e")
|
71
|
+
end
|
72
|
+
|
73
|
+
assert_raise(OptionParser::InvalidArgument) do
|
74
|
+
@opt.parse!(%w"--float 1.234xyz")
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def test_numeric
|
79
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--numeric 0")})
|
80
|
+
assert_equal(0, @numeric)
|
81
|
+
|
82
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--numeric 0/1")})
|
83
|
+
assert_equal(0, @numeric)
|
84
|
+
|
85
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--numeric 1/2")})
|
86
|
+
assert_equal(Rational(1, 2), @numeric)
|
87
|
+
|
88
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--numeric 010")})
|
89
|
+
assert_equal(8, @numeric)
|
90
|
+
|
91
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--numeric 1.2/2.3")})
|
92
|
+
assert_equal(Rational(12, 23), @numeric)
|
93
|
+
|
94
|
+
assert_raise(OptionParser::InvalidArgument) do
|
95
|
+
@opt.parse!(%w"--numeric 1/")
|
96
|
+
end
|
97
|
+
|
98
|
+
assert_raise(OptionParser::InvalidArgument) do
|
99
|
+
@opt.parse!(%w"--numeric 12/34xyz")
|
100
|
+
end
|
101
|
+
|
102
|
+
assert_raise(OptionParser::InvalidArgument) do
|
103
|
+
@opt.parse!(%w"--numeric 12x/34yz")
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def test_decimal_integer
|
108
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--decimal-integer 0")})
|
109
|
+
assert_equal(0, @decimal_integer)
|
110
|
+
|
111
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--decimal-integer 10")})
|
112
|
+
assert_equal(10, @decimal_integer)
|
113
|
+
|
114
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--decimal-integer 010")})
|
115
|
+
assert_equal(10, @decimal_integer)
|
116
|
+
|
117
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--decimal-integer 09")})
|
118
|
+
assert_equal(9, @decimal_integer)
|
119
|
+
|
120
|
+
assert_raise(OptionParser::InvalidArgument) do
|
121
|
+
@opt.parse!(%w"--decimal-integer 0b1")
|
122
|
+
end
|
123
|
+
|
124
|
+
assert_raise(OptionParser::InvalidArgument) do
|
125
|
+
@opt.parse!(%w"--decimal-integer x")
|
126
|
+
end
|
127
|
+
|
128
|
+
assert_raise(OptionParser::InvalidArgument) do
|
129
|
+
@opt.parse!(%w"--decimal-integer 1234xyz")
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
133
|
+
def test_octal_integer
|
134
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--octal-integer 0")})
|
135
|
+
assert_equal(0, @octal_integer)
|
136
|
+
|
137
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--octal-integer 6")})
|
138
|
+
assert_equal(6, @octal_integer)
|
139
|
+
|
140
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--octal-integer 07")})
|
141
|
+
assert_equal(7, @octal_integer)
|
142
|
+
|
143
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--octal-integer 10")})
|
144
|
+
assert_equal(8, @octal_integer)
|
145
|
+
|
146
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--octal-integer 011")})
|
147
|
+
assert_equal(9, @octal_integer)
|
148
|
+
|
149
|
+
assert_raise(OptionParser::InvalidArgument) do
|
150
|
+
@opt.parse!(%w"--octal-integer 09")
|
151
|
+
end
|
152
|
+
|
153
|
+
assert_raise(OptionParser::InvalidArgument) do
|
154
|
+
@opt.parse!(%w"--octal-integer 0b1")
|
155
|
+
end
|
156
|
+
|
157
|
+
assert_raise(OptionParser::InvalidArgument) do
|
158
|
+
@opt.parse!(%w"--octal-integer x")
|
159
|
+
end
|
160
|
+
|
161
|
+
assert_raise(OptionParser::InvalidArgument) do
|
162
|
+
@opt.parse!(%w"--octal-integer 01234xyz")
|
163
|
+
end
|
164
|
+
end
|
165
|
+
|
166
|
+
def test_decimal_numeric
|
167
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--decimal-numeric 0")})
|
168
|
+
assert_equal(0, @decimal_numeric)
|
169
|
+
|
170
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--decimal-numeric 01")})
|
171
|
+
assert_equal(1, @decimal_numeric)
|
172
|
+
|
173
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--decimal-numeric 1.2")})
|
174
|
+
assert_in_delta(1.2, @decimal_numeric)
|
175
|
+
|
176
|
+
assert_equal(%w"", no_error {@opt.parse!(%w"--decimal-numeric 1E2")})
|
177
|
+
assert_in_delta(100.0, @decimal_numeric)
|
178
|
+
|
179
|
+
assert_raise(OptionParser::InvalidArgument) do
|
180
|
+
@opt.parse!(%w"--decimal-numeric 0b1")
|
181
|
+
end
|
182
|
+
|
183
|
+
e = assert_raise(OptionParser::InvalidArgument) do
|
184
|
+
@opt.parse!(%w"--decimal-numeric 09")
|
185
|
+
end
|
186
|
+
|
187
|
+
assert_equal("invalid argument: --decimal-numeric 09", e.message)
|
188
|
+
|
189
|
+
assert_raise(OptionParser::InvalidArgument) do
|
190
|
+
@opt.parse!(%w"--decimal-integer 1234xyz")
|
191
|
+
end
|
192
|
+
|
193
|
+
assert_raise(OptionParser::InvalidArgument) do
|
194
|
+
@opt.parse!(%w"--decimal-integer 12.34xyz")
|
195
|
+
end
|
196
|
+
end
|
197
|
+
|
198
|
+
end
|
@@ -0,0 +1,69 @@
|
|
1
|
+
# frozen_string_literal: false
|
2
|
+
require 'test/unit'
|
3
|
+
require 'optparse/ac'
|
4
|
+
|
5
|
+
class TestOptionParserAutoConf < Test::Unit::TestCase
|
6
|
+
def setup
|
7
|
+
@opt = OptionParser::AC.new
|
8
|
+
@foo = @bar = self.class
|
9
|
+
@opt.ac_arg_enable("foo", "foo option") {|x| @foo = x}
|
10
|
+
@opt.ac_arg_disable("bar", "bar option") {|x| @bar = x}
|
11
|
+
@opt.ac_arg_with("zot", "zot option") {|x| @zot = x}
|
12
|
+
end
|
13
|
+
|
14
|
+
class DummyOutput < String
|
15
|
+
alias write concat
|
16
|
+
end
|
17
|
+
def no_error(*args)
|
18
|
+
$stderr, stderr = DummyOutput.new, $stderr
|
19
|
+
assert_nothing_raised(*args) {return yield}
|
20
|
+
ensure
|
21
|
+
stderr, $stderr = $stderr, stderr
|
22
|
+
$!.backtrace.delete_if {|e| /\A#{Regexp.quote(__FILE__)}:#{__LINE__-2}/o =~ e} if $!
|
23
|
+
assert_empty(stderr)
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_enable
|
27
|
+
@opt.parse!(%w"--enable-foo")
|
28
|
+
assert_equal(true, @foo)
|
29
|
+
@opt.parse!(%w"--enable-bar")
|
30
|
+
assert_equal(true, @bar)
|
31
|
+
end
|
32
|
+
|
33
|
+
def test_enable_value
|
34
|
+
@opt.parse!(%w"--enable-foo=A")
|
35
|
+
assert_equal("A", @foo)
|
36
|
+
@opt.parse!(%w"--enable-bar=B")
|
37
|
+
assert_equal("B", @bar)
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_disable
|
41
|
+
@opt.parse!(%w"--disable-foo")
|
42
|
+
assert_equal(false, @foo)
|
43
|
+
@opt.parse!(%w"--disable-bar")
|
44
|
+
assert_equal(false, @bar)
|
45
|
+
end
|
46
|
+
|
47
|
+
def test_with
|
48
|
+
@opt.parse!(%w"--with-zot=foobar")
|
49
|
+
assert_equal("foobar", @zot)
|
50
|
+
@opt.parse!(%w"--without-zot")
|
51
|
+
assert_nil(@zot)
|
52
|
+
end
|
53
|
+
|
54
|
+
def test_without
|
55
|
+
@opt.parse!(%w"--without-zot")
|
56
|
+
assert_nil(@zot)
|
57
|
+
assert_raise(OptionParser::NeedlessArgument) {@opt.parse!(%w"--without-zot=foobar")}
|
58
|
+
end
|
59
|
+
|
60
|
+
def test_help
|
61
|
+
help = @opt.help
|
62
|
+
assert_match(/--enable-foo/, help)
|
63
|
+
assert_match(/--disable-bar/, help)
|
64
|
+
assert_match(/--with-zot/, help)
|
65
|
+
assert_not_match(/--disable-foo/, help)
|
66
|
+
assert_not_match(/--enable-bar/, help)
|
67
|
+
assert_not_match(/--without/, help)
|
68
|
+
end
|
69
|
+
end
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: false
|
2
|
+
require 'test/unit'
|
3
|
+
require 'optparse'
|
4
|
+
|
5
|
+
class TestOptionParserBashCompletion < Test::Unit::TestCase
|
6
|
+
def setup
|
7
|
+
@opt = 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_empty
|
15
|
+
assert_equal([], @opt.candidate(""))
|
16
|
+
end
|
17
|
+
|
18
|
+
def test_one_hyphen
|
19
|
+
assert_equal(%w[-z --foo --bar= --for=], @opt.candidate("-"))
|
20
|
+
end
|
21
|
+
|
22
|
+
def test_two_hyphen
|
23
|
+
assert_equal(%w[--foo --bar= --for=], @opt.candidate("--"))
|
24
|
+
end
|
25
|
+
|
26
|
+
def test_long_f
|
27
|
+
assert_equal(%w[--foo --for=], @opt.candidate("--f"))
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_long_for_option
|
31
|
+
assert_equal(%w[--for=], @opt.candidate("--for"))
|
32
|
+
end
|
33
|
+
|
34
|
+
def test_long_for_option_args
|
35
|
+
assert_equal(%w[hello help zot], @opt.candidate("--for="))
|
36
|
+
end
|
37
|
+
|
38
|
+
def test_long_for_option_complete
|
39
|
+
assert_equal(%w[hello help], @opt.candidate("--for=h"))
|
40
|
+
end
|
41
|
+
|
42
|
+
def test_case_sensitive
|
43
|
+
@opt.define("-Z") {}
|
44
|
+
assert_equal(%w[-z], @opt.candidate("-z"))
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,18 @@
|
|
1
|
+
# frozen_string_literal: false
|
2
|
+
require_relative 'test_optparse'
|
3
|
+
|
4
|
+
class TestOptionParserCClass < TestOptionParser
|
5
|
+
def test_no_argument
|
6
|
+
flags = []
|
7
|
+
@opt.def_option("-[a-z]") {|x| flags << x}
|
8
|
+
no_error {@opt.parse!(%w"-a")}
|
9
|
+
assert_equal(%w"a", flags)
|
10
|
+
end
|
11
|
+
|
12
|
+
def test_required_argument
|
13
|
+
flags = []
|
14
|
+
@opt.def_option("-[a-z]X") {|x| flags << x}
|
15
|
+
no_error {@opt.parse!(%w"-a")}
|
16
|
+
assert_equal(%w"a", flags)
|
17
|
+
end
|
18
|
+
end
|
@@ -0,0 +1,48 @@
|
|
1
|
+
# frozen_string_literal: false
|
2
|
+
require_relative 'test_optparse'
|
3
|
+
begin
|
4
|
+
require "did_you_mean"
|
5
|
+
rescue LoadError
|
6
|
+
return
|
7
|
+
end
|
8
|
+
|
9
|
+
class TestOptionParserDidYouMean < TestOptionParser
|
10
|
+
def setup
|
11
|
+
super
|
12
|
+
@opt.def_option("--foo", Integer) { |v| @foo = v }
|
13
|
+
@opt.def_option("--bar", Integer) { |v| @bar = v }
|
14
|
+
@opt.def_option("--baz", Integer) { |v| @baz = v }
|
15
|
+
@formatter = ::DidYouMean.formatter
|
16
|
+
if ::DidYouMean.const_defined?(:Formatter)
|
17
|
+
::DidYouMean.formatter = ::DidYouMean::Formatter
|
18
|
+
else
|
19
|
+
case @formatter
|
20
|
+
when ::DidYouMean::PlainFormatter
|
21
|
+
else
|
22
|
+
::DidYouMean.formatter = ::DidYouMean::PlainFormatter.new
|
23
|
+
end
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
def teardown
|
28
|
+
::DidYouMean.formatter = @formatter
|
29
|
+
end
|
30
|
+
|
31
|
+
def test_no_suggestion
|
32
|
+
assert_raise_with_message(OptionParser::InvalidOption, "invalid option: --cuz") do
|
33
|
+
@opt.permute!(%w"--cuz")
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
def test_plain
|
38
|
+
assert_raise_with_message(OptionParser::InvalidOption, /invalid option: --baa\nDid you mean\?\s+bar\s+baz\Z/) do
|
39
|
+
@opt.permute!(%w"--baa")
|
40
|
+
end
|
41
|
+
end
|
42
|
+
|
43
|
+
def test_ambiguous
|
44
|
+
assert_raise_with_message(OptionParser::AmbiguousOption, /ambiguous option: --ba\nDid you mean\?\s+bar\s+baz\Z/) do
|
45
|
+
@opt.permute!(%w"--ba")
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
@@ -0,0 +1,49 @@
|
|
1
|
+
# frozen_string_literal: false
|
2
|
+
require 'test/unit'
|
3
|
+
require 'optparse'
|
4
|
+
|
5
|
+
class TestOptionParserGetopts < Test::Unit::TestCase
|
6
|
+
def setup
|
7
|
+
@opt = OptionParser.new
|
8
|
+
end
|
9
|
+
|
10
|
+
def test_short_noarg
|
11
|
+
o = @opt.getopts(%w[-a], "ab")
|
12
|
+
assert_equal(true, o['a'])
|
13
|
+
assert_equal(false, o['b'])
|
14
|
+
|
15
|
+
o = @opt.getopts(%w[-a], "ab", symbolize_names: true)
|
16
|
+
assert_equal(true, o[:a])
|
17
|
+
assert_equal(false, o[:b])
|
18
|
+
end
|
19
|
+
|
20
|
+
def test_short_arg
|
21
|
+
o = @opt.getopts(%w[-a1], "a:b:")
|
22
|
+
assert_equal("1", o['a'])
|
23
|
+
assert_equal(nil, o['b'])
|
24
|
+
|
25
|
+
o = @opt.getopts(%w[-a1], "a:b:", symbolize_names: true)
|
26
|
+
assert_equal("1", o[:a])
|
27
|
+
assert_equal(nil, o[:b])
|
28
|
+
end
|
29
|
+
|
30
|
+
def test_long_noarg
|
31
|
+
o = @opt.getopts(%w[--foo], "", "foo", "bar")
|
32
|
+
assert_equal(true, o['foo'])
|
33
|
+
assert_equal(false, o['bar'])
|
34
|
+
|
35
|
+
o = @opt.getopts(%w[--foo], "", "foo", "bar", symbolize_names: true)
|
36
|
+
assert_equal(true, o[:foo])
|
37
|
+
assert_equal(false, o[:bar])
|
38
|
+
end
|
39
|
+
|
40
|
+
def test_long_arg
|
41
|
+
o = @opt.getopts(%w[--bar ZOT], "", "foo:FOO", "bar:BAR")
|
42
|
+
assert_equal("FOO", o['foo'])
|
43
|
+
assert_equal("ZOT", o['bar'])
|
44
|
+
|
45
|
+
o = @opt.getopts(%w[--bar ZOT], "", "foo:FOO", "bar:BAR", symbolize_names: true)
|
46
|
+
assert_equal("FOO", o[:foo])
|
47
|
+
assert_equal("ZOT", o[:bar])
|
48
|
+
end
|
49
|
+
end
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: false
|
2
|
+
require 'test/unit'
|
3
|
+
require 'optparse'
|
4
|
+
require 'optparse/kwargs'
|
5
|
+
|
6
|
+
class TestOptionParserKwArg < Test::Unit::TestCase
|
7
|
+
class K
|
8
|
+
def initialize(host:, port: 8080)
|
9
|
+
@host = host
|
10
|
+
@port = port
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
class DummyOutput < String
|
15
|
+
alias write concat
|
16
|
+
end
|
17
|
+
def assert_no_error(*args)
|
18
|
+
$stderr, stderr = DummyOutput.new, $stderr
|
19
|
+
assert_nothing_raised(*args) {return yield}
|
20
|
+
ensure
|
21
|
+
stderr, $stderr = $stderr, stderr
|
22
|
+
$!.backtrace.delete_if {|e| /\A#{Regexp.quote(__FILE__)}:#{__LINE__-2}/o =~ e} if $!
|
23
|
+
assert_empty(stderr)
|
24
|
+
end
|
25
|
+
alias no_error assert_no_error
|
26
|
+
|
27
|
+
def test_kwarg
|
28
|
+
opt = OptionParser.new
|
29
|
+
options = opt.define_by_keywords({}, K.instance_method(:initialize),
|
30
|
+
port: [Integer])
|
31
|
+
assert_raise(OptionParser::MissingArgument) {opt.parse!(%w"--host")}
|
32
|
+
assert_nothing_raised {opt.parse!(%w"--host=localhost")}
|
33
|
+
assert_equal("localhost", options[:host])
|
34
|
+
assert_nothing_raised {opt.parse!(%w"--port")}
|
35
|
+
assert_nothing_raised {opt.parse!(%w"--port=80")}
|
36
|
+
assert_equal(80, options[:port])
|
37
|
+
end
|
38
|
+
end
|