cmd-optparse.rb 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (106) hide show
  1. checksums.yaml +7 -0
  2. data/.bundle/config +2 -0
  3. data/.document +7 -0
  4. data/.editorconfig +13 -0
  5. data/.github/dependabot.yml +6 -0
  6. data/.github/workflows/test.yml +26 -0
  7. data/.gitignore +12 -0
  8. data/.rdoc_options +4 -0
  9. data/COPYING +56 -0
  10. data/Gemfile +5 -0
  11. data/README.md +59 -0
  12. data/Rakefile +14 -0
  13. data/cmd-optparse.rb.gemspec +30 -0
  14. data/doc/optparse/.document +1 -0
  15. data/doc/optparse/argument_converters.rdoc +380 -0
  16. data/doc/optparse/creates_option.rdoc +7 -0
  17. data/doc/optparse/option_params.rdoc +509 -0
  18. data/doc/optparse/ruby/argument_abbreviation.rb +9 -0
  19. data/doc/optparse/ruby/argument_keywords.rb +6 -0
  20. data/doc/optparse/ruby/argument_strings.rb +6 -0
  21. data/doc/optparse/ruby/argv.rb +2 -0
  22. data/doc/optparse/ruby/array.rb +6 -0
  23. data/doc/optparse/ruby/basic.rb +17 -0
  24. data/doc/optparse/ruby/block.rb +9 -0
  25. data/doc/optparse/ruby/collected_options.rb +8 -0
  26. data/doc/optparse/ruby/custom_converter.rb +9 -0
  27. data/doc/optparse/ruby/date.rb +6 -0
  28. data/doc/optparse/ruby/datetime.rb +6 -0
  29. data/doc/optparse/ruby/decimal_integer.rb +7 -0
  30. data/doc/optparse/ruby/decimal_numeric.rb +7 -0
  31. data/doc/optparse/ruby/default_values.rb +8 -0
  32. data/doc/optparse/ruby/descriptions.rb +15 -0
  33. data/doc/optparse/ruby/explicit_array_values.rb +9 -0
  34. data/doc/optparse/ruby/explicit_hash_values.rb +9 -0
  35. data/doc/optparse/ruby/false_class.rb +6 -0
  36. data/doc/optparse/ruby/float.rb +6 -0
  37. data/doc/optparse/ruby/help.rb +18 -0
  38. data/doc/optparse/ruby/help_banner.rb +7 -0
  39. data/doc/optparse/ruby/help_format.rb +25 -0
  40. data/doc/optparse/ruby/help_program_name.rb +7 -0
  41. data/doc/optparse/ruby/integer.rb +6 -0
  42. data/doc/optparse/ruby/long_names.rb +9 -0
  43. data/doc/optparse/ruby/long_optional.rb +6 -0
  44. data/doc/optparse/ruby/long_required.rb +6 -0
  45. data/doc/optparse/ruby/long_simple.rb +9 -0
  46. data/doc/optparse/ruby/long_with_negation.rb +6 -0
  47. data/doc/optparse/ruby/match_converter.rb +9 -0
  48. data/doc/optparse/ruby/matched_values.rb +6 -0
  49. data/doc/optparse/ruby/method.rb +11 -0
  50. data/doc/optparse/ruby/missing_options.rb +12 -0
  51. data/doc/optparse/ruby/mixed_names.rb +12 -0
  52. data/doc/optparse/ruby/name_abbrev.rb +9 -0
  53. data/doc/optparse/ruby/no_abbreviation.rb +10 -0
  54. data/doc/optparse/ruby/numeric.rb +6 -0
  55. data/doc/optparse/ruby/object.rb +6 -0
  56. data/doc/optparse/ruby/octal_integer.rb +7 -0
  57. data/doc/optparse/ruby/optional_argument.rb +9 -0
  58. data/doc/optparse/ruby/parse.rb +13 -0
  59. data/doc/optparse/ruby/parse_bang.rb +13 -0
  60. data/doc/optparse/ruby/proc.rb +13 -0
  61. data/doc/optparse/ruby/regexp.rb +6 -0
  62. data/doc/optparse/ruby/required_argument.rb +9 -0
  63. data/doc/optparse/ruby/shellwords.rb +6 -0
  64. data/doc/optparse/ruby/short_names.rb +9 -0
  65. data/doc/optparse/ruby/short_optional.rb +6 -0
  66. data/doc/optparse/ruby/short_range.rb +6 -0
  67. data/doc/optparse/ruby/short_required.rb +6 -0
  68. data/doc/optparse/ruby/short_simple.rb +9 -0
  69. data/doc/optparse/ruby/string.rb +6 -0
  70. data/doc/optparse/ruby/terminator.rb +6 -0
  71. data/doc/optparse/ruby/time.rb +6 -0
  72. data/doc/optparse/ruby/true_class.rb +6 -0
  73. data/doc/optparse/ruby/uri.rb +6 -0
  74. data/doc/optparse/tutorial.rdoc +858 -0
  75. data/lib/optionparser.rb +2 -0
  76. data/lib/optparse/ac.rb +54 -0
  77. data/lib/optparse/date.rb +18 -0
  78. data/lib/optparse/kwargs.rb +22 -0
  79. data/lib/optparse/shellwords.rb +7 -0
  80. data/lib/optparse/time.rb +11 -0
  81. data/lib/optparse/uri.rb +7 -0
  82. data/lib/optparse/version.rb +71 -0
  83. data/lib/optparse.rb +2346 -0
  84. data/misc/rb_optparse.bash +21 -0
  85. data/misc/rb_optparse.zsh +39 -0
  86. data/rakelib/.document +0 -0
  87. data/rakelib/changelogs.rake +34 -0
  88. data/rakelib/epoch.rake +5 -0
  89. data/rakelib/version.rake +51 -0
  90. data/test/lib/helper.rb +4 -0
  91. data/test/optparse/test_acceptable.rb +198 -0
  92. data/test/optparse/test_autoconf.rb +69 -0
  93. data/test/optparse/test_bash_completion.rb +46 -0
  94. data/test/optparse/test_cclass.rb +18 -0
  95. data/test/optparse/test_did_you_mean.rb +48 -0
  96. data/test/optparse/test_getopts.rb +49 -0
  97. data/test/optparse/test_kwargs.rb +38 -0
  98. data/test/optparse/test_load.rb +141 -0
  99. data/test/optparse/test_noarg.rb +79 -0
  100. data/test/optparse/test_optarg.rb +60 -0
  101. data/test/optparse/test_optparse.rb +127 -0
  102. data/test/optparse/test_placearg.rb +76 -0
  103. data/test/optparse/test_reqarg.rb +95 -0
  104. data/test/optparse/test_summary.rb +81 -0
  105. data/test/optparse/test_zsh_completion.rb +21 -0
  106. metadata +154 -0
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 35e41eb882f470b2af1fefe465395c4c453ed0fabcddc10c2beedc506895a353
4
+ data.tar.gz: 241e687823cf2fea4f9f2def31f979925707aafdf35e5a97b8b8a31f7d36ebf1
5
+ SHA512:
6
+ metadata.gz: acc0ce9ad24e69143ce0541fbc15a529c61282e1609cf5d250f899646b4bd2fb4bab700bf97c310f48a4ad9842a011ba4c14c311e5cd37f9d7ff84fc55847f18
7
+ data.tar.gz: 7a3775b8b352e66a6b93d5e53364e721fcf713fa59a156be2131af767d926208e4a639a328f34e115ce59d8710584a7a6476f8acadd07ac0869310d1c240df6c
data/.bundle/config ADDED
@@ -0,0 +1,2 @@
1
+ ---
2
+ BUNDLE_PATH: ".gems"
data/.document ADDED
@@ -0,0 +1,7 @@
1
+ COPYING
2
+ ChangeLog
3
+ README.md
4
+ doc/
5
+ lib/
6
+ logs/
7
+ misc/
data/.editorconfig ADDED
@@ -0,0 +1,13 @@
1
+ root = true
2
+
3
+ [*]
4
+ indent_style = space
5
+ indent_size = 2
6
+ end_of_line = lf
7
+ charset = utf-8
8
+ trim_trailing_whitespace = true
9
+ insert_final_newline = true
10
+ max_line_length = 80
11
+
12
+ [*.zsh]
13
+ indent_size = 4
@@ -0,0 +1,6 @@
1
+ version: 2
2
+ updates:
3
+ - package-ecosystem: 'github-actions'
4
+ directory: '/'
5
+ schedule:
6
+ interval: 'weekly'
@@ -0,0 +1,26 @@
1
+ name: cmd-optparse.rb
2
+
3
+ on:
4
+ push:
5
+ branches: [ main ]
6
+ pull_request:
7
+ branches: [ main ]
8
+
9
+ jobs:
10
+ tests:
11
+ strategy:
12
+ fail-fast: false
13
+ matrix:
14
+ os: [ubuntu-latest, macos-latest]
15
+ ruby: [3.1, 3.2]
16
+ runs-on: ${{ matrix.os }}
17
+ steps:
18
+ - uses: actions/checkout@v4
19
+ - name: Set up Ruby
20
+ uses: ruby/setup-ruby@v1
21
+ with:
22
+ ruby-version: ${{ matrix.ruby }}
23
+ - name: Install dependencies
24
+ run: bundle install
25
+ - name: Run test
26
+ run: bundle exec rake test
data/.gitignore ADDED
@@ -0,0 +1,12 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /rdoc/
6
+ /logs/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ /Gemfile.lock
11
+ /ChangeLog
12
+ /.gems
data/.rdoc_options ADDED
@@ -0,0 +1,4 @@
1
+ ---
2
+ page_dir: doc
3
+ main_page: README.md
4
+ title: Documentation for OptionParser
data/COPYING ADDED
@@ -0,0 +1,56 @@
1
+ Ruby is copyrighted free software by Yukihiro Matsumoto <matz@netlab.jp>.
2
+ You can redistribute it and/or modify it under either the terms of the
3
+ 2-clause BSDL (see the file BSDL), or the conditions below:
4
+
5
+ 1. You may make and give away verbatim copies of the source form of the
6
+ software without restriction, provided that you duplicate all of the
7
+ original copyright notices and associated disclaimers.
8
+
9
+ 2. You may modify your copy of the software in any way, provided that
10
+ you do at least ONE of the following:
11
+
12
+ a. place your modifications in the Public Domain or otherwise
13
+ make them Freely Available, such as by posting said
14
+ modifications to Usenet or an equivalent medium, or by allowing
15
+ the author to include your modifications in the software.
16
+
17
+ b. use the modified software only within your corporation or
18
+ organization.
19
+
20
+ c. give non-standard binaries non-standard names, with
21
+ instructions on where to get the original software distribution.
22
+
23
+ d. make other distribution arrangements with the author.
24
+
25
+ 3. You may distribute the software in object code or binary form,
26
+ provided that you do at least ONE of the following:
27
+
28
+ a. distribute the binaries and library files of the software,
29
+ together with instructions (in the manual page or equivalent)
30
+ on where to get the original distribution.
31
+
32
+ b. accompany the distribution with the machine-readable source of
33
+ the software.
34
+
35
+ c. give non-standard binaries non-standard names, with
36
+ instructions on where to get the original software distribution.
37
+
38
+ d. make other distribution arrangements with the author.
39
+
40
+ 4. You may modify and include the part of the software into any other
41
+ software (possibly commercial). But some files in the distribution
42
+ are not written by the author, so that they are not under these terms.
43
+
44
+ For the list of those files and their copying conditions, see the
45
+ file LEGAL.
46
+
47
+ 5. The scripts and library files supplied as input to or produced as
48
+ output from the software do not automatically fall under the
49
+ copyright of the software, but belong to whomever generated them,
50
+ and may be sold commercially, and may be aggregated with this
51
+ software.
52
+
53
+ 6. THIS SOFTWARE IS PROVIDED "AS IS" AND WITHOUT ANY EXPRESS OR
54
+ IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
55
+ WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
56
+ PURPOSE.
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source "https://rubygems.org"
2
+
3
+ gem "rake"
4
+ gem "test-unit"
5
+ gem "test-unit-ruby-core"
data/README.md ADDED
@@ -0,0 +1,59 @@
1
+ # OptionParser
2
+
3
+ OptionParser is a class for command-line option analysis. It is much more
4
+ advanced, yet also easier to use, than GetoptLong, and is a more Ruby-oriented
5
+ solution.
6
+
7
+ ## Features
8
+
9
+ 1. The argument specification and the code to handle it are written in the
10
+ same place.
11
+ 2. It can output an option summary; you don't need to maintain this string
12
+ separately.
13
+ 3. Optional and mandatory arguments are specified very gracefully.
14
+ 4. Arguments can be automatically converted to a specified class.
15
+ 5. Arguments can be restricted to a certain set.
16
+
17
+ ## Installation
18
+
19
+ Add this line to your application's Gemfile:
20
+
21
+ ```ruby
22
+ gem 'optparse'
23
+ ```
24
+
25
+ And then execute:
26
+
27
+ $ bundle install
28
+
29
+ Or install it yourself as:
30
+
31
+ $ gem install optparse
32
+
33
+ ## Usage
34
+
35
+ ```ruby
36
+ require 'optparse'
37
+
38
+ options = {}
39
+ OptionParser.new do |opts|
40
+ opts.banner = "Usage: example.rb [options]"
41
+
42
+ opts.on("-v", "--[no-]verbose", "Run verbosely") do |v|
43
+ options[:verbose] = v
44
+ end
45
+ end.parse!
46
+
47
+ p options
48
+ p ARGV
49
+ ```
50
+
51
+ ## Development
52
+
53
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
54
+
55
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
56
+
57
+ ## Contributing
58
+
59
+ Bug reports and pull requests are welcome on GitHub at https://github.com/ruby/optparse.
data/Rakefile ADDED
@@ -0,0 +1,14 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test/lib"
6
+ t.ruby_opts << "-rhelper"
7
+ t.test_files = FileList["test/**/test_*.rb"]
8
+ end
9
+
10
+ task :default => :test
11
+
12
+ task :rdoc do
13
+ sh("rdoc", *Bundler::GemHelper.instance.gemspec.rdoc_options, ".")
14
+ end
@@ -0,0 +1,30 @@
1
+ # frozen_string_literal: true
2
+
3
+ name = File.basename(__FILE__, ".gemspec")
4
+ version = ["lib", Array.new(name.count("-")+1, "..").join("/")].find do |dir|
5
+ break File.foreach(File.join(__dir__, dir, "#{name.tr('-', '/')}.rb")) do |line|
6
+ /^\s*OptionParser::Version\s*=\s*"(.*)"/ =~ line and break $1
7
+ end rescue nil
8
+ end
9
+
10
+ Gem::Specification.new do |spec|
11
+ spec.name = name
12
+ spec.version = "0.1.0"
13
+ spec.authors = ["0x1eef", "Nobu Nakada"]
14
+ spec.email = ["0x1eef@protonmail.com"]
15
+
16
+ spec.summary = %q{OptionParser is a class for command-line option analysis.}
17
+ spec.description = %q{OptionParser is a class for command-line option analysis.}
18
+ spec.homepage = "https://github.com/0x1eef/cmd-optparse.rb#readme"
19
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.5.0")
20
+ spec.licenses = ["Ruby", "BSD-2-Clause"]
21
+
22
+ spec.metadata["homepage_uri"] = spec.homepage
23
+ spec.metadata["source_code_uri"] = spec.homepage
24
+
25
+ spec.files = `git ls-files`.each_line.map { _1.chomp($/) }
26
+ spec.rdoc_options = ["--main=README.md", "--op=rdoc", "--page-dir=doc"]
27
+ spec.bindir = "exe"
28
+ spec.executables = []
29
+ spec.require_paths = ["lib"]
30
+ end
@@ -0,0 +1 @@
1
+ *.rdoc
@@ -0,0 +1,380 @@
1
+ == Argument Converters
2
+
3
+ An option can specify that its argument is to be converted
4
+ from the default +String+ to an instance of another class.
5
+
6
+ === Contents
7
+
8
+ - {Built-In Argument Converters}[#label-Built-In+Argument+Converters]
9
+ - {Date}[#label-Date]
10
+ - {DateTime}[#label-DateTime]
11
+ - {Time}[#label-Time]
12
+ - {URI}[#label-URI]
13
+ - {Shellwords}[#label-Shellwords]
14
+ - {Integer}[#label-Integer]
15
+ - {Float}[#label-Float]
16
+ - {Numeric}[#label-Numeric]
17
+ - {DecimalInteger}[#label-DecimalInteger]
18
+ - {OctalInteger}[#label-OctalInteger]
19
+ - {DecimalNumeric}[#label-DecimalNumeric]
20
+ - {TrueClass}[#label-TrueClass]
21
+ - {FalseClass}[#label-FalseClass]
22
+ - {Object}[#label-Object]
23
+ - {String}[#label-String]
24
+ - {Array}[#label-Array]
25
+ - {Regexp}[#label-Regexp]
26
+ - {Custom Argument Converters}[#label-Custom+Argument+Converters]
27
+
28
+ === Built-In Argument Converters
29
+
30
+ +OptionParser+ has a number of built-in argument converters,
31
+ which are demonstrated below.
32
+
33
+ ==== +Date+
34
+
35
+ File +date.rb+
36
+ defines an option whose argument is to be converted to a +Date+ object.
37
+ The argument is converted by method Date#parse.
38
+
39
+ :include: ruby/date.rb
40
+
41
+ Executions:
42
+
43
+ $ ruby date.rb --date 2001-02-03
44
+ [#<Date: 2001-02-03 ((2451944j,0s,0n),+0s,2299161j)>, Date]
45
+ $ ruby date.rb --date 20010203
46
+ [#<Date: 2001-02-03 ((2451944j,0s,0n),+0s,2299161j)>, Date]
47
+ $ ruby date.rb --date "3rd Feb 2001"
48
+ [#<Date: 2001-02-03 ((2451944j,0s,0n),+0s,2299161j)>, Date]
49
+
50
+ ==== +DateTime+
51
+
52
+ File +datetime.rb+
53
+ defines an option whose argument is to be converted to a +DateTime+ object.
54
+ The argument is converted by method DateTime#parse.
55
+
56
+ :include: ruby/datetime.rb
57
+
58
+ Executions:
59
+
60
+ $ ruby datetime.rb --datetime 2001-02-03T04:05:06+07:00
61
+ [#<DateTime: 2001-02-03T04:05:06+07:00 ((2451943j,75906s,0n),+25200s,2299161j)>, DateTime]
62
+ $ ruby datetime.rb --datetime 20010203T040506+0700
63
+ [#<DateTime: 2001-02-03T04:05:06+07:00 ((2451943j,75906s,0n),+25200s,2299161j)>, DateTime]
64
+ $ ruby datetime.rb --datetime "3rd Feb 2001 04:05:06 PM"
65
+ [#<DateTime: 2001-02-03T16:05:06+00:00 ((2451944j,57906s,0n),+0s,2299161j)>, DateTime]
66
+
67
+ ==== +Time+
68
+
69
+ File +time.rb+
70
+ defines an option whose argument is to be converted to a +Time+ object.
71
+ The argument is converted by method Time#httpdate or Time#parse.
72
+
73
+ :include: ruby/time.rb
74
+
75
+ Executions:
76
+
77
+ $ ruby time.rb --time "Thu, 06 Oct 2011 02:26:12 GMT"
78
+ [2011-10-06 02:26:12 UTC, Time]
79
+ $ ruby time.rb --time 2010-10-31
80
+ [2010-10-31 00:00:00 -0500, Time]
81
+
82
+ ==== +URI+
83
+
84
+ File +uri.rb+
85
+ defines an option whose argument is to be converted to a +URI+ object.
86
+ The argument is converted by method URI#parse.
87
+
88
+ :include: ruby/uri.rb
89
+
90
+ Executions:
91
+
92
+ $ ruby uri.rb --uri https://github.com
93
+ [#<URI::HTTPS https://github.com>, URI::HTTPS]
94
+ $ ruby uri.rb --uri http://github.com
95
+ [#<URI::HTTP http://github.com>, URI::HTTP]
96
+ $ ruby uri.rb --uri file://~/var
97
+ [#<URI::File file://~/var>, URI::File]
98
+
99
+ ==== +Shellwords+
100
+
101
+ File +shellwords.rb+
102
+ defines an option whose argument is to be converted to an +Array+ object by method
103
+ Shellwords#shellwords.
104
+
105
+ :include: ruby/shellwords.rb
106
+
107
+ Executions:
108
+
109
+ $ ruby shellwords.rb --shellwords "ruby my_prog.rb | less"
110
+ [["ruby", "my_prog.rb", "|", "less"], Array]
111
+ $ ruby shellwords.rb --shellwords "here are 'two words'"
112
+ [["here", "are", "two words"], Array]
113
+
114
+ ==== +Integer+
115
+
116
+ File +integer.rb+
117
+ defines an option whose argument is to be converted to an +Integer+ object.
118
+ The argument is converted by method Kernel#Integer.
119
+
120
+ :include: ruby/integer.rb
121
+
122
+ Executions:
123
+
124
+ $ ruby integer.rb --integer 100
125
+ [100, Integer]
126
+ $ ruby integer.rb --integer -100
127
+ [-100, Integer]
128
+ $ ruby integer.rb --integer 0100
129
+ [64, Integer]
130
+ $ ruby integer.rb --integer 0x100
131
+ [256, Integer]
132
+ $ ruby integer.rb --integer 0b100
133
+ [4, Integer]
134
+
135
+ ==== +Float+
136
+
137
+ File +float.rb+
138
+ defines an option whose argument is to be converted to a +Float+ object.
139
+ The argument is converted by method Kernel#Float.
140
+
141
+ :include: ruby/float.rb
142
+
143
+ Executions:
144
+
145
+ $ ruby float.rb --float 1
146
+ [1.0, Float]
147
+ $ ruby float.rb --float 3.14159
148
+ [3.14159, Float]
149
+ $ ruby float.rb --float 1.234E2
150
+ [123.4, Float]
151
+ $ ruby float.rb --float 1.234E-2
152
+ [0.01234, Float]
153
+
154
+ ==== +Numeric+
155
+
156
+ File +numeric.rb+
157
+ defines an option whose argument is to be converted to an instance
158
+ of +Rational+, +Float+, or +Integer+.
159
+ The argument is converted by method Kernel#Rational,
160
+ Kernel#Float, or Kernel#Integer.
161
+
162
+ :include: ruby/numeric.rb
163
+
164
+ Executions:
165
+
166
+ $ ruby numeric.rb --numeric 1/3
167
+ [(1/3), Rational]
168
+ $ ruby numeric.rb --numeric 3.333E-1
169
+ [0.3333, Float]
170
+ $ ruby numeric.rb --numeric 3
171
+ [3, Integer]
172
+
173
+ ==== +DecimalInteger+
174
+
175
+ File +decimal_integer.rb+
176
+ defines an option whose argument is to be converted to an +Integer+ object.
177
+ The argument is converted by method Kernel#Integer.
178
+
179
+ :include: ruby/decimal_integer.rb
180
+
181
+ The argument may not be in a binary or hexadecimal format;
182
+ a leading zero is ignored (not parsed as octal).
183
+
184
+ Executions:
185
+
186
+ $ ruby decimal_integer.rb --decimal_integer 100
187
+ [100, Integer]
188
+ $ ruby decimal_integer.rb --decimal_integer -100
189
+ [-100, Integer]
190
+ $ ruby decimal_integer.rb --decimal_integer 0100
191
+ [100, Integer]
192
+ $ ruby decimal_integer.rb --decimal_integer -0100
193
+ [-100, Integer]
194
+
195
+ ==== +OctalInteger+
196
+
197
+ File +octal_integer.rb+
198
+ defines an option whose argument is to be converted to an +Integer+ object.
199
+ The argument is converted by method Kernel#Integer.
200
+
201
+ :include: ruby/octal_integer.rb
202
+
203
+ The argument may not be in a binary or hexadecimal format;
204
+ it is parsed as octal, regardless of whether it has a leading zero.
205
+
206
+ Executions:
207
+
208
+ $ ruby octal_integer.rb --octal_integer 100
209
+ [64, Integer]
210
+ $ ruby octal_integer.rb --octal_integer -100
211
+ [-64, Integer]
212
+ $ ruby octal_integer.rb --octal_integer 0100
213
+ [64, Integer]
214
+
215
+ ==== +DecimalNumeric+
216
+
217
+ File +decimal_numeric.rb+
218
+ defines an option whose argument is to be converted to an +Integer+ object.
219
+ The argument is converted by method Kernel#Integer
220
+
221
+ :include: ruby/decimal_numeric.rb
222
+
223
+ The argument may not be in a binary or hexadecimal format;
224
+ a leading zero causes the argument to be parsed as octal.
225
+
226
+ Executions:
227
+
228
+ $ ruby decimal_numeric.rb --decimal_numeric 100
229
+ [100, Integer]
230
+ $ ruby decimal_numeric.rb --decimal_numeric -100
231
+ [-100, Integer]
232
+ $ ruby decimal_numeric.rb --decimal_numeric 0100
233
+ [64, Integer]
234
+
235
+ ==== +TrueClass+
236
+
237
+ File +true_class.rb+
238
+ defines an option whose argument is to be converted to +true+ or +false+.
239
+ The argument is evaluated by method Object#nil?.
240
+
241
+ :include: ruby/true_class.rb
242
+
243
+ The argument may be any of those shown in the examples below.
244
+
245
+ Executions:
246
+
247
+ $ ruby true_class.rb --true_class true
248
+ [true, TrueClass]
249
+ $ ruby true_class.rb --true_class yes
250
+ [true, TrueClass]
251
+ $ ruby true_class.rb --true_class +
252
+ [true, TrueClass]
253
+ $ ruby true_class.rb --true_class false
254
+ [false, FalseClass]
255
+ $ ruby true_class.rb --true_class no
256
+ [false, FalseClass]
257
+ $ ruby true_class.rb --true_class -
258
+ [false, FalseClass]
259
+ $ ruby true_class.rb --true_class nil
260
+ [false, FalseClass]
261
+
262
+ ==== +FalseClass+
263
+
264
+ File +false_class.rb+
265
+ defines an option whose argument is to be converted to +true+ or +false+.
266
+ The argument is evaluated by method Object#nil?.
267
+
268
+ :include: ruby/false_class.rb
269
+
270
+ The argument may be any of those shown in the examples below.
271
+
272
+ Executions:
273
+
274
+ $ ruby false_class.rb --false_class false
275
+ [false, FalseClass]
276
+ $ ruby false_class.rb --false_class no
277
+ [false, FalseClass]
278
+ $ ruby false_class.rb --false_class -
279
+ [false, FalseClass]
280
+ $ ruby false_class.rb --false_class nil
281
+ [false, FalseClass]
282
+ $ ruby false_class.rb --false_class true
283
+ [true, TrueClass]
284
+ $ ruby false_class.rb --false_class yes
285
+ [true, TrueClass]
286
+ $ ruby false_class.rb --false_class +
287
+ [true, TrueClass]
288
+
289
+ ==== +Object+
290
+
291
+ File +object.rb+
292
+ defines an option whose argument is not to be converted from +String+.
293
+
294
+ :include: ruby/object.rb
295
+
296
+ Executions:
297
+
298
+ $ ruby object.rb --object foo
299
+ ["foo", String]
300
+ $ ruby object.rb --object nil
301
+ ["nil", String]
302
+
303
+ ==== +String+
304
+
305
+ File +string.rb+
306
+ defines an option whose argument is not to be converted from +String+.
307
+
308
+ :include: ruby/string.rb
309
+
310
+ Executions:
311
+
312
+ $ ruby string.rb --string foo
313
+ ["foo", String]
314
+ $ ruby string.rb --string nil
315
+ ["nil", String]
316
+
317
+ ==== +Array+
318
+
319
+ File +array.rb+
320
+ defines an option whose argument is to be converted from +String+
321
+ to an array of strings, based on comma-separated substrings.
322
+
323
+ :include: ruby/array.rb
324
+
325
+ Executions:
326
+
327
+ $ ruby array.rb --array ""
328
+ [[], Array]
329
+ $ ruby array.rb --array foo,bar,baz
330
+ [["foo", "bar", "baz"], Array]
331
+ $ ruby array.rb --array "foo, bar, baz"
332
+ [["foo", " bar", " baz"], Array]
333
+
334
+ ==== +Regexp+
335
+
336
+ File +regexp.rb+
337
+ defines an option whose argument is to be converted to a +Regexp+ object.
338
+
339
+ :include: ruby/regexp.rb
340
+
341
+ Executions:
342
+
343
+ $ ruby regexp.rb --regexp foo
344
+
345
+ === Custom Argument Converters
346
+
347
+ You can create custom argument converters.
348
+ To create a custom converter, call OptionParser#accept with:
349
+
350
+ - An identifier, which may be any object.
351
+ - An optional match pattern, which defaults to <tt>/.*/m</tt>.
352
+ - A block that accepts the argument and returns the converted value.
353
+
354
+ This custom converter accepts any argument and converts it,
355
+ if possible, to a +Complex+ object.
356
+
357
+ :include: ruby/custom_converter.rb
358
+
359
+ Executions:
360
+
361
+ $ ruby custom_converter.rb --complex 0
362
+ [(0+0i), Complex]
363
+ $ ruby custom_converter.rb --complex 1
364
+ [(1+0i), Complex]
365
+ $ ruby custom_converter.rb --complex 1+2i
366
+ [(1+2i), Complex]
367
+ $ ruby custom_converter.rb --complex 0.3-0.5i
368
+ [(0.3-0.5i), Complex]
369
+
370
+ This custom converter accepts any 1-word argument
371
+ and capitalizes it, if possible.
372
+
373
+ :include: ruby/match_converter.rb
374
+
375
+ Executions:
376
+
377
+ $ ruby match_converter.rb --capitalize foo
378
+ ["Foo", String]
379
+ $ ruby match_converter.rb --capitalize "foo bar"
380
+ match_converter.rb:9:in `<main>': invalid argument: --capitalize foo bar (OptionParser::InvalidArgument)
@@ -0,0 +1,7 @@
1
+ Creates an option from the given parameters +params+.
2
+ See {Parameters for New Options}[optparse/option_params.rdoc].
3
+
4
+ The block, if given, is the handler for the created option.
5
+ When the option is encountered during command-line parsing,
6
+ the block is called with the argument given for the option, if any.
7
+ See {Option Handlers}[optparse/option_params.rdoc#label-Option+Handlers].