optimist 3.1.0 → 3.2.1

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 87e142709336212fa484755022a85b8d833de889579b69a1b2fc35933bac88fd
4
- data.tar.gz: 4ac2b33acae583f3ce444c105112c144a32affc52a8ab52980ab9772a107f8c3
3
+ metadata.gz: 3d29d64e512655a994c2f2665c6b0be52194f64624a442509eaf49373efe0d23
4
+ data.tar.gz: c48da179e2d0dfa451e5eb2efd4ae62efafb6148dfdc18342c8c78f1faceb5b9
5
5
  SHA512:
6
- metadata.gz: 7ccadd2b388b3877e3a075e224b7b9f1ed3b48a4dd68a72a6ca7f8ca9120c999a366d0619bf1cfcec0018fd80baeab83fd8e686e35f91109668c4d0210e08ddd
7
- data.tar.gz: '0312801235dc1952812a79d01d68b71cff4db8187040cbc74f299bb519d926dc01fed15f0c656ca7d09e18dcd18289b1d951917cc31c8a8e1bfaa1377cbcde99'
6
+ metadata.gz: be8c02eb87bc55221dfb42dfeb347026651a945617fbfda12892e4960cba9762a02a224927f05c54a7a186778f646c5435508ea9e9d7a7c19573dcaa2b3869e1
7
+ data.tar.gz: fcb56bc996d0016e9c521f2d696cdc90338579060e745656c4e5cd0493390d94f57ecfe2d94864fb7b67b0c9f652dd2a29502d23af8ab182acd4a26486fc39a9
data/.codeclimate.yml CHANGED
@@ -12,5 +12,5 @@ plugins:
12
12
  rubocop:
13
13
  enabled: true
14
14
  config: ".rubocop_cc.yml"
15
- channel: rubocop-0-82
15
+ channel: rubocop-1-56-3
16
16
  version: '2'
@@ -13,18 +13,17 @@ jobs:
13
13
  fail-fast: false
14
14
  matrix:
15
15
  ruby-version:
16
- - '2.4'
17
- - '2.5'
18
- - '2.6'
19
16
  - '2.7'
20
17
  - '3.0'
21
18
  - '3.1'
22
19
  - '3.2'
23
- - jruby-9.3
20
+ - '3.3'
21
+ - '3.4'
22
+ - jruby-9.4
24
23
  - ruby-head
25
24
  - jruby-head
26
25
  steps:
27
- - uses: actions/checkout@v2
26
+ - uses: actions/checkout@v4
28
27
  - name: Set up Ruby
29
28
  uses: ruby/setup-ruby@v1
30
29
  with:
data/CHANGELOG.md CHANGED
@@ -4,9 +4,242 @@ This project adheres to [Semantic Versioning](http://semver.org/).
4
4
 
5
5
  ## [Unreleased]
6
6
 
7
+ ## [3.2.1] - 2025-03-19
8
+ ### Added
9
+ - Added testing of jruby-9.4 ([#172](https://github.com/ManageIQ/optimist/pull/172) - thanks @Fryguy)
10
+ - Added testing of Ruby 3.4 ([#173](https://github.com/ManageIQ/optimist/pull/173) - thanks @Fryguy)
11
+
12
+ ### Removed
13
+ - Drop testing of Ruby <2.7, jruby-9.3 ([#172](https://github.com/ManageIQ/optimist/pull/172) - thanks @Fryguy)
14
+
15
+ ### Fixed
16
+ - Fix issue where negative boolean flags were output incorrectly ([#179](https://github.com/ManageIQ/optimist/pull/179) - thanks @Fryguy)
17
+ - Fix issues with frozen strings which fail jruby-head (JRuby 10) ([#180](https://github.com/ManageIQ/optimist/pull/180) - thanks @Fryguy)
18
+
19
+ ## [3.2.0] - 2024-11-11
20
+ ### Added
21
+ - Align the short and long forms into their own columns in the help output ([#145](https://github.com/ManageIQ/optimist/pull/145) - thanks @akhoury6)
22
+ - Add support for DidYouMean when long options are spelled incorrectly ([#150](https://github.com/ManageIQ/optimist/pull/150) - thanks @nanobowers)
23
+ - Using `permitted:` restricts the allowed values that a end-user inputs to a pre-defined list ([#147](https://github.com/ManageIQ/optimist/pull/147) - thanks @akhoury6)
24
+ - Add exact_match to settings, defaulting to inexact matching ([#154](https://github.com/ManageIQ/optimist/pull/154) - thanks @nanobowers)
25
+ - Add setting to disable implicit short options ([#155](https://github.com/ManageIQ/optimist/pull/155) - thanks @nanobowers)
26
+ - Add alt longname and multiple char support ([#151](https://github.com/ManageIQ/optimist/pull/151) - thanks @nanobowers)
27
+ - Permitted regexp/range support ([#158](https://github.com/ManageIQ/optimist/pull/158), [#159](https://github.com/ManageIQ/optimist/pull/159) - thanks @nanobowers)
28
+ - Add some examples ([#161](https://github.com/ManageIQ/optimist/pull/161) - thanks @nanobowers)
29
+
30
+ ### Changed
31
+ - Enable frozen_string_literal for future-ruby support ([#149](https://github.com/ManageIQ/optimist/pull/149), [#153](https://github.com/ManageIQ/optimist/pull/153) - thanks @nanobowers)
32
+ - Refactor constraints ([#156](https://github.com/ManageIQ/optimist/pull/156) - thanks @nanobowers)
33
+ - Fix assert_raises to assert_raises_errmatch ([#160](https://github.com/ManageIQ/optimist/pull/160) - thanks @nanobowers)
34
+
7
35
  ## [3.1.0] - 2023-07-24
8
36
  ### Added
9
- - Implement `either` command ([#130](https://github.com/ManageIQ/optimist/pull/130))
37
+ - Implement `either` command ([#130](https://github.com/ManageIQ/optimist/pull/130) - thanks @alezummo)
38
+
39
+ ## [3.0.1] - 2020-04-20
40
+
41
+ - Add a LICENSE.txt file to the released package
42
+ - Test fixes (thanks @aried3r, @neontapir, @npras)
43
+
44
+ ## [3.0.0] - 2018-08-24
45
+
46
+ - The gem has been officially renamed to optimist
47
+
48
+ ## [2.1.3] - 2018-07-05
49
+
50
+ - Refactor each option type into subclasses of Option. Define a registry for the registration of each option. This makes the code more modular and facilitates extension by allowing additional Option subclasses. (thanks @clxy)
51
+ - Fixed implementation of ignore_invalid_options. (thanks @metcalf)
52
+ - Various warning cleanup for ruby 2.1, 2.3, etc. (thanks @nanobowers)
53
+ - Optimist.die can now accept an error code.
54
+ - fixed default (thanks @nanobowers)
55
+ - Change from ruby license to MIT license in the code.
56
+
57
+ ## [2.1.2] - 2015-03-10
58
+
59
+ - loosen mime-types requirements (for better ruby 1.8.7 support)
60
+ - use io/console gem instead of curses (for better jruby support)
61
+ - fix parsing bug when chronic gem is not available
62
+ - allow default array to be empty if a type is specified
63
+ - better specified license and better spec coverage
64
+
65
+ ## [2.1.1] - 2015-01-03
66
+
67
+ - Remove curses as a hard dependency. It is optional. This can leverage the gem if it is present.
68
+ - Fix ruby -w warnings
69
+
70
+ ## 2.1.0 - 2015-01-02
71
+
72
+ - Integer parser now supports underscore separator.
73
+ - Add Parser#usage and Parser#synopsis commands for creating a standard banner
74
+ message. Using Parser#banner directly will override both of those.
75
+ - Add Parser#ignore_invalid_options to prevent erroring on unknown options.
76
+ - Allow flags to act as switches if they have defaults set and no value is
77
+ passed on the commandline
78
+ - Parser#opt learned to accept a block or a :callback option which it will call
79
+ after parsing the option.
80
+ - Add Optimist::educate which displays the help message and dies.
81
+ - Reformat help message to be more GNUish.
82
+ - Fix command name in help message when script has no extension.
83
+ - Fix handling of newlines inside descriptions
84
+ - Documentation and other fixes.
85
+
86
+ ## 2.0 - 2012-08-11
87
+
88
+ - Change flag logic: --no-X will always be false, and --X will always be true,
89
+ regardless of default.
90
+ - For flags that default to true, display --no-X instead of --X in the help
91
+ menu. Accept both versions on the commandline.
92
+ - Fix a spurious warning
93
+ - Update Rakefile to 1.9
94
+ - Minor documentation fixes
95
+
96
+ ## 1.16.2 - 2010-04-06
97
+
98
+ - Bugfix in Optimist::options. Thanks to Brian C. Thomas for pointing it out.
99
+
100
+ ## 1.16.1 - 2010-04-05
101
+
102
+ - Bugfix in Optimist::die method introduced in last release.
103
+
104
+ ## 1.16 - 2010-04-01
105
+
106
+ - Add Optimist::with_standard_exception_handling method for easing the use of Parser directly.
107
+ - Handle scientific notation in float arguments, thanks to Will Fitzgerald.
108
+ - Drop hoe dependency.
109
+
110
+ ## 1.15 - 2009-09-30
111
+
112
+ - Don't raise an exception when out of short arguments (thanks to Rafael
113
+ Sevilla for pointing out how dumb this behavior was).
114
+
115
+ ## 1.14 - 2009-06-19
116
+ - Make :multi arguments default to [], not nil, when not set on the commandline.
117
+ - Minor commenting and error message improvements
118
+
119
+ ## 1.13 - 2009-03-16
120
+
121
+ - Fix parsing of "--longarg=<value with spaces>".
122
+
123
+ ## 1.12 - 2009-01-30
124
+
125
+ - Fix some unit test failures in the last release. Should be more careful.
126
+ - Make default short options only be assigned *after- all user-specified
127
+ short options. Now there's a little less juggling to do when you just
128
+ want to specify a few short options.
129
+
130
+ ## 1.11 - 2009-01-29
131
+
132
+ - Set <opt>_given keys in the results hash for options that were specified
133
+ on the commandline.
134
+
135
+ ## 1.10.2 - 2008-10-23
136
+
137
+ - No longer try `stty size` for screen size detection. Just use curses, and
138
+ screen users will have to deal with the screen clearing.
139
+
140
+ ## 1.10.1 - 2008-10-22
141
+
142
+ - Options hash now responds to method calls as well as standard hash lookup.
143
+ - Default values for multi-occurrence parameters now autoboxed.
144
+ - The relationship between multi-value, multi-occurrence, and default values
145
+ improved and explained.
146
+ - Documentation improvements.
147
+
148
+ ## 1.10 - 2008-10-21
149
+
150
+ - Added :io type for parameters that point to IO streams (filenames, URIs, etc).
151
+ - For screen size detection, first try `stty size` before loading Curses.
152
+ - Improved documentation.
153
+
154
+ ## 1.9 - 2008-08-20
155
+
156
+ - Added 'stop_on_unknown' command to stop parsing on any unknown argument.
157
+ This is useful for handling sub-commands when you don't know the entire
158
+ set of commands up front. (E.g. if the initial arguments can change it.)
159
+ - Added a :multi option for parameters, signifying that they can be specified
160
+ multiple times.
161
+ - Added :ints, :strings, :doubles, and :floats option types, which can take
162
+ multiple arguments.
163
+
164
+ ## 1.8.2 - 2008-06-25
165
+
166
+ - Bugfix for #conflicts and #depends error messages
167
+
168
+ ## 1.8.1 - 2008-06-24
169
+
170
+ - Bugfix for short option autocreation
171
+ - More aggressive documentation
172
+
173
+ ## 1.8 - 2008-06-16
174
+
175
+ - Sub-command support via Parser#stop_on
176
+
177
+ ## 1.7.2 - 2008-01-16
178
+
179
+ - Ruby 1.9-ify. Apparently this means replacing :'s with ;'s.
180
+
181
+ ## 1.7.1 - 2008-01-07
182
+
183
+ - Documentation improvements
184
+
185
+ ## 1.7 - 2007-06-17
186
+
187
+ - Fix incorrect error message for multiple missing required arguments
188
+ (thanks to Neill Zero)
189
+
190
+ ## 1.6 - 2007-04-01
191
+
192
+ - Don't attempt curses screen-width magic unless running on a terminal.
193
+
194
+ ## 1.5 - 2007-03-31
195
+
196
+ - --help and --version do the right thing even if the rest of the
197
+ command line is incorrect.
198
+ - Added #conflicts and #depends to model dependencies and exclusivity
199
+ between arguments.
200
+ - Minor bugfixes.
201
+
202
+ ## 1.4 - 2007-03-26
203
+
204
+ - Disable short options with :short => :none.
205
+ - Minor bugfixes and error message improvements.
206
+
207
+ ## 1.3 - 2007-01-31
208
+
209
+ - Wrap at (screen width - 1) instead of screen width.
210
+ - User can override --help and --version.
211
+ - Bugfix in handling of -v and -h.
212
+ - More tests to confirm the above.
213
+
214
+ ## 1.2 - 2007-01-31
215
+
216
+ - Minor documentation tweaks.
217
+ - Removed hoe dependency.
218
+
219
+ ## 1.1 - 2007-01-30
220
+
221
+ - Optimist::options now passes any arguments as block arguments. Since
222
+ instance variables are not properly captured by the block, this
223
+ makes it slightly less noisy to pass them in as local variables.
224
+ (A real-life use for _why's cloaker!)
225
+ - Help display now preserves original argument order.
226
+ - Optimist::die now also has a single string form in case death is not
227
+ due to a single argument.
228
+ - Parser#text now an alias for Parser#banner, and can be called
229
+ multiple times, with the output being placed in the right position
230
+ in the help text.
231
+ - Slightly more indicative formatting for parameterized arguments.
232
+
233
+ ## 1.0 - 2007-01-29
234
+
235
+ - Initial release.
10
236
 
11
- [Unreleased]: https://github.com/ManageIQ/optimist/compare/v3.1.0...HEAD
237
+ [Unreleased]: https://github.com/ManageIQ/optimist/compare/v3.2.1...HEAD
238
+ [3.2.1]: https://github.com/ManageIQ/optimist/compare/v3.2.0...v3.2.1
239
+ [3.2.0]: https://github.com/ManageIQ/optimist/compare/v3.1.0...v3.2.0
12
240
  [3.1.0]: https://github.com/ManageIQ/optimist/compare/v3.0.1...v3.1.0
241
+ [3.0.1]: https://github.com/ManageIQ/optimist/compare/v3.0.0...v3.0.1
242
+ [3.0.0]: https://github.com/ManageIQ/optimist/compare/v2.1.3...v3.0.0
243
+ [2.1.3]: https://github.com/ManageIQ/optimist/compare/v2.1.2...v2.1.3
244
+ [2.1.2]: https://github.com/ManageIQ/optimist/compare/v2.1.1...v2.1.2
245
+ [2.1.1]: https://github.com/ManageIQ/optimist/compare/v2.1.0...v2.1.1
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../lib/optimist'
3
+
4
+ opts = Optimist::options do
5
+ opt :monkey, "Use monkey mode" # flag --monkey, default false
6
+ opt :name, "Monkey name", :type => :string # string --name <s>, default nil
7
+ opt :num_limbs, "Number of limbs", :default => 4 # integer --num-limbs <i>, default to 4
8
+ end
9
+ p opts
@@ -0,0 +1,20 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../lib/optimist'
3
+
4
+ opts = Optimist::options do
5
+ # 'short:' can now take more than one short-option character
6
+ # you can specify 'short:' as a string/symbol or an array of strings/symbols
7
+ # 'alt:' adds additional long-opt choices (over the original name or the long: name)
8
+ # you can specify 'alt:' as a string/symbol or an array of strings/symbols.
9
+ #
10
+ opt :concat, 'concatenate flag', short: ['-C', 'A'], alt: ['cat', '--append']
11
+ opt :array_len, 'set Array length', long: 'size', alt: 'length', type: Integer
12
+ end
13
+
14
+ p opts
15
+
16
+ # $ ./alt_names.rb -h
17
+ # Options:
18
+ # -C, -A, --concat, --cat, --append concatenate flag
19
+ # -s, --size, --length=<i> set Array length
20
+ # -h, --help Show this message
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../lib/optimist'
3
+
4
+ Optimist::options do
5
+ synopsis "Overall synopsis of this program"
6
+ version "cool-script v0.3 (code-name: apple-cake)"
7
+ opt :juice, "use juice"
8
+ opt :milk, "use milk"
9
+ opt :litres, "quantity of liquid", :default => 2.0
10
+ opt :brand, "brand name of the liquid", :type => :string
11
+ end
@@ -0,0 +1,12 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../lib/optimist'
3
+
4
+ Optimist::options do
5
+ synopsis "Overall synopsis of this program"
6
+ version "cool-script v0.3.1 (code-name: apple-cake)"
7
+ banner "My Banner"
8
+ opt :juice, "use juice"
9
+ opt :milk, "use milk"
10
+ opt :litres, "quantity of liquid", :default => 2.0
11
+ opt :brand, "brand name of the liquid", :type => :string
12
+ end
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../lib/optimist'
3
+
4
+ Optimist::options do
5
+ version "cool-script v0.3.2 (code-name: apple-cake)"
6
+ banner self.version ## print out the version in the banner
7
+ banner "drinks"
8
+ opt :juice, "use juice"
9
+ opt :milk, "use milk"
10
+ banner "drink control" ## can be used for categories
11
+ opt :litres, "quantity of liquid", :default => 2.0
12
+ opt :brand, "brand name of the liquid", :type => :string
13
+ banner "other controls"
14
+ end
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../lib/optimist'
3
+
4
+ opts = Optimist::options do
5
+ opt :default_false, "Boolean flag with false default", :default => false, :short => "f"
6
+ opt :default_true, "Boolean flag with true default", :default => true, :short => "t"
7
+ end
8
+
9
+ puts opts
@@ -0,0 +1,28 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../lib/optimist'
3
+
4
+ opts = Optimist::options do
5
+ opt :dog, "user is dog"
6
+ opt :cat, "user is cat"
7
+ opt :rat, "user is rat"
8
+ conflicts :dog, :cat, :rat
9
+
10
+ opt :wash, "pet wash"
11
+ opt :dry, "pet dry"
12
+ depends :wash, :dry
13
+
14
+ opt :credit, "pay creditcard"
15
+ opt :cash, "pay cash"
16
+ opt :cheque, "pay cheque"
17
+ either :credit, :cash, :cheque
18
+ end
19
+ p opts
20
+
21
+ # $ ./constraints.rb --dog --cat
22
+ # Error: only one of --dog, --cat, --rat can be given.
23
+
24
+ # $ ./constraints.rb --dog --wash
25
+ # Error: --wash, --dry have a dependency and must be given together.
26
+
27
+ # $ ./constraints.rb --cash --cheque --rat --wash --dry
28
+ # Error: one and only one of --credit, --cash, --cheque is required.
@@ -0,0 +1,26 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../lib/optimist'
3
+
4
+ opts = Optimist::options do
5
+ opt :cone, "Ice cream cone"
6
+ opt :zippy, "It zips"
7
+ opt :zapzy, "It zapz"
8
+ opt :big_bug, "Madagascar cockroach"
9
+ end
10
+ p opts
11
+
12
+ # $ ./didyoumean.rb --one
13
+ # Error: unknown argument '--one'. Did you mean: [--cone] ?.
14
+ # Try --help for help.
15
+
16
+ # $ ./didyoumean.rb --zappy
17
+ # Error: unknown argument '--zappy'. Did you mean: [--zapzy, --zippy] ?.
18
+ # Try --help for help.
19
+
20
+ # $ ./didyoumean.rb --big_bug
21
+ # Error: unknown argument '--big_bug'. Did you mean: [--big-bug] ?.
22
+ # Try --help for help.
23
+
24
+ # $ ./didyoumean.rb --bigbug
25
+ # Error: unknown argument '--bigbug'. Did you mean: [--big-bug] ?.
26
+ # Try --help for help.
@@ -0,0 +1,15 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../lib/optimist'
3
+
4
+ opts = Optimist::options do
5
+ version "cool-script v0.1 (code-name: bananas foster)"
6
+ banner "This script is pretty cool."
7
+ opt :juice, "use juice"
8
+ opt :milk, "use milk"
9
+ opt :litres, "quantity of liquid", :default => 2.0
10
+ opt :brand, "brand name of the liquid", :type => :string
11
+ opt :config, "config file path", :type => String, :required => true
12
+ opt :drinkers, "number of people drinking the liquid", :default => 6
13
+ end
14
+ Optimist::die :drinkers, "must be value a greater than zero" if opts[:drinkers] < 1
15
+ Optimist::die :config, "must point to an existing file" unless File.exist?(opts[:config]) if opts[:config]
@@ -0,0 +1,18 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../lib/optimist'
3
+
4
+ opts = Optimist::options(ARGV, exact_match: false) do
5
+ opt :apple, "An apple"
6
+ opt :apple_sauce, "Cooked apple puree"
7
+ opt :atom, "Smallest unit of ordinary matter"
8
+ opt :anvil, "Heavy metal"
9
+ opt :anteater, "Eats ants"
10
+ end
11
+ p opts
12
+
13
+ # $ ./partialmatch.rb --anv 1
14
+ # {:apple=>false, :apple_sauce=>false, :atom=>false, :anvil=>true, :anteater=>false, :help=>false, :anvil_given=>true}
15
+ #
16
+ # $ ./partialmatch.rb --an 1
17
+ # Error: ambiguous option '--an' matched keys (anvil,anteater).
18
+ # Try --help for help.
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../lib/optimist'
3
+
4
+ opts = Optimist::options do
5
+ opt :french, "starts with french", type: String,
6
+ permitted: %w(fries toast),
7
+ permitted_response: "option %{arg} must be something that starts " +
8
+ "with french, e.g. %{permitted} but you gave '%{given}'"
9
+ opt :dog, "starts with dog", permitted: %r/(house|bone|tail)/, type: String
10
+ opt :zipcode, "zipcode", permitted: %r/^[0-9]{5}$/, default: '39759',
11
+ permitted_response: "option %{arg} must be a zipcode, a five-digit number from 00000..99999"
12
+ opt :adult, "adult age", permitted: (18...99), type: Integer
13
+ opt :minor, "minor age", permitted: (0..18), type: Integer
14
+ opt :letter, "a letter", permitted: ('a'...'z'), type: String
15
+ end
16
+
17
+ p opts
18
+
19
+ # $ ./permitted.rb -z 384949
20
+ # Error: option -z must be a zipcode, a five-digit number from 00000..99999.
21
+ # Try --help for help.
22
+ #
23
+ # $ ./permitted.rb --minor 19
24
+ # Error: option '--minor' only accepts value in range of: 0..18.
25
+ # Try --help for help.
26
+ #
27
+ # $ ./permitted.rb -f frog
28
+ # Error: option -f must be something that starts with french, e.g. ["fries", "toast"] but you gave 'frog'.
29
+ # Try --help for help.
@@ -0,0 +1,43 @@
1
+ #!/usr/bin/env ruby
2
+ require_relative '../lib/optimist'
3
+
4
+ class ZipCode
5
+ REGEXP = %r/^(?<zip>[0-9]{5})(\-(?<plusfour>[0-9]{4}))?$/
6
+ def initialize(zipstring)
7
+ matcher = REGEXP.match(zipstring)
8
+ raise "Invalid zip-code" unless matcher
9
+ @zip = matcher[:zip]
10
+ @plusfour = matcher[:plusfour]
11
+ end
12
+ end
13
+
14
+ #module Optimist
15
+ class ZipCodeOption < Optimist::Option
16
+ # register_alias registers with the global parser.
17
+ register_alias :zipcode
18
+ def type_format ; "=<zip>" ; end # optional for use with help-message
19
+ def parse(paramlist, _neg_given)
20
+ paramlist.map do |plist|
21
+ plist.map do |param_string|
22
+ raise Optimist::CommandlineError, "option '#{self.name}' should be formatted as a zipcode" unless param_string=~ ZipCode::REGEXP
23
+ ZipCode.new(param_string)
24
+ end
25
+ end
26
+ end
27
+ end
28
+
29
+ opts = Optimist::options do
30
+ opt :zipcode, "United states postal code", :type => :zipcode
31
+ end
32
+
33
+ p opts[:zipcode]
34
+
35
+ # $ ./types_custom.rb --zipcode 39759
36
+ # <ZipCode:0x0000000000a34968 @zip="39759", @plusfour=nil>
37
+ #
38
+ # $ ./types_custom.rb --zipcode 39759-0001
39
+ # <ZipCode:0x000000000117a100 @zip="39759", @plusfour="0001">
40
+ #
41
+ # $ ./types_custom.rb --zipcode 384134
42
+ # Error: option 'zipcode' should be formatted as a zipcode.
43
+ # Try --help for help.