optimist 3.1.0 → 3.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.codeclimate.yml +1 -1
- data/.github/workflows/ci.yaml +3 -1
- data/CHANGELOG.md +222 -2
- data/examples/a_basic_example.rb +10 -0
- data/examples/alt_names.rb +20 -0
- data/examples/banners1.rb +11 -0
- data/examples/banners2.rb +12 -0
- data/examples/banners3.rb +14 -0
- data/examples/constraints.rb +28 -0
- data/examples/didyoumean.rb +26 -0
- data/examples/medium_example.rb +15 -0
- data/examples/partialmatch.rb +18 -0
- data/examples/permitted.rb +29 -0
- data/examples/types_custom.rb +43 -0
- data/lib/optimist.rb +347 -93
- data/optimist.gemspec +1 -1
- data/renovate.json +6 -0
- data/test/optimist/alt_names_test.rb +168 -0
- data/test/optimist/command_line_error_test.rb +1 -1
- data/test/optimist/help_needed_test.rb +1 -1
- data/test/optimist/parser_constraint_test.rb +141 -0
- data/test/optimist/parser_educate_test.rb +22 -1
- data/test/optimist/parser_opt_test.rb +1 -1
- data/test/optimist/parser_parse_test.rb +3 -3
- data/test/optimist/parser_permitted_test.rb +121 -0
- data/test/optimist/parser_test.rb +277 -176
- data/test/optimist/version_needed_test.rb +1 -1
- data/test/optimist_test.rb +5 -3
- data/test/support/assert_helpers.rb +6 -0
- metadata +22 -5
- data/History.txt +0 -175
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 416ba2a0bcc410fa9e8a746b73335cff35b550634d4f26a7c1c17547db2ba61b
|
4
|
+
data.tar.gz: 1a2b555e3a7ec6eef5af0976d660454151888052e9492695ac5d932090e858ee
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 0ce87d7db57a7840f84cb9be3c0aad95e9dd227becbf873309ea7256175be969b937742ca781b0f5345e71ca73a6cc508e5d1e0953d03391fb44e5efa43e681c
|
7
|
+
data.tar.gz: a760065b4b8e4477e9fd90644b92e7b802eff249cc6f742d83cb8f891e1d35b9cbfbc9380a2fb6fc6f75f69cd2a1741336ee900a61b6e4abae23d82005b75b06
|
data/.codeclimate.yml
CHANGED
data/.github/workflows/ci.yaml
CHANGED
@@ -20,11 +20,12 @@ jobs:
|
|
20
20
|
- '3.0'
|
21
21
|
- '3.1'
|
22
22
|
- '3.2'
|
23
|
+
- '3.3'
|
23
24
|
- jruby-9.3
|
24
25
|
- ruby-head
|
25
26
|
- jruby-head
|
26
27
|
steps:
|
27
|
-
- uses: actions/checkout@
|
28
|
+
- uses: actions/checkout@v4
|
28
29
|
- name: Set up Ruby
|
29
30
|
uses: ruby/setup-ruby@v1
|
30
31
|
with:
|
@@ -32,3 +33,4 @@ jobs:
|
|
32
33
|
bundler-cache: true
|
33
34
|
- name: Run tests
|
34
35
|
run: bundle exec rake
|
36
|
+
continue-on-error: ${{ matrix.ruby-version == 'ruby-head' }}
|
data/CHANGELOG.md
CHANGED
@@ -4,9 +4,229 @@ This project adheres to [Semantic Versioning](http://semver.org/).
|
|
4
4
|
|
5
5
|
## [Unreleased]
|
6
6
|
|
7
|
+
## [3.2.0] - 2024-11-11
|
8
|
+
### Added
|
9
|
+
- Align the short and long forms into their own columns in the help output ([#145](https://github.com/ManageIQ/optimist/pull/145) - thanks @akhoury6)
|
10
|
+
- Add support for DidYouMean when long options are spelled incorrectly ([#150](https://github.com/ManageIQ/optimist/pull/150) - thanks @nanobowers)
|
11
|
+
- 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)
|
12
|
+
- Add exact_match to settings, defaulting to inexact matching ([#154](https://github.com/ManageIQ/optimist/pull/154) - thanks @nanobowers)
|
13
|
+
- Add setting to disable implicit short options ([#155](https://github.com/ManageIQ/optimist/pull/155) - thanks @nanobowers)
|
14
|
+
- Add alt longname and multiple char support ([#151](https://github.com/ManageIQ/optimist/pull/151) - thanks @nanobowers)
|
15
|
+
- Permitted regexp/range support ([#158](https://github.com/ManageIQ/optimist/pull/158), [#159](https://github.com/ManageIQ/optimist/pull/159) - thanks @nanobowers)
|
16
|
+
- Add some examples ([#161](https://github.com/ManageIQ/optimist/pull/161) - thanks @nanobowers)
|
17
|
+
|
18
|
+
### Changed
|
19
|
+
- 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)
|
20
|
+
- Refactor constraints ([#156](https://github.com/ManageIQ/optimist/pull/156) - thanks @nanobowers)
|
21
|
+
- Fix assert_raises to assert_raises_errmatch ([#160](https://github.com/ManageIQ/optimist/pull/160) - thanks @nanobowers)
|
22
|
+
|
7
23
|
## [3.1.0] - 2023-07-24
|
8
24
|
### Added
|
9
|
-
- Implement `either` command ([#130](https://github.com/ManageIQ/optimist/pull/130))
|
25
|
+
- Implement `either` command ([#130](https://github.com/ManageIQ/optimist/pull/130) - thanks @alezummo)
|
26
|
+
|
27
|
+
## [3.0.1] - 2020-04-20
|
28
|
+
|
29
|
+
- Add a LICENSE.txt file to the released package
|
30
|
+
- Test fixes (thanks @aried3r, @neontapir, @npras)
|
31
|
+
|
32
|
+
## [3.0.0] - 2018-08-24
|
33
|
+
|
34
|
+
- The gem has been officially renamed to optimist
|
35
|
+
|
36
|
+
## [2.1.3] - 2018-07-05
|
37
|
+
|
38
|
+
- 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)
|
39
|
+
- Fixed implementation of ignore_invalid_options. (thanks @metcalf)
|
40
|
+
- Various warning cleanup for ruby 2.1, 2.3, etc. (thanks @nanobowers)
|
41
|
+
- Optimist.die can now accept an error code.
|
42
|
+
- fixed default (thanks @nanobowers)
|
43
|
+
- Change from ruby license to MIT license in the code.
|
44
|
+
|
45
|
+
## [2.1.2] - 2015-03-10
|
46
|
+
|
47
|
+
- loosen mime-types requirements (for better ruby 1.8.7 support)
|
48
|
+
- use io/console gem instead of curses (for better jruby support)
|
49
|
+
- fix parsing bug when chronic gem is not available
|
50
|
+
- allow default array to be empty if a type is specified
|
51
|
+
- better specified license and better spec coverage
|
52
|
+
|
53
|
+
## [2.1.1] - 2015-01-03
|
54
|
+
|
55
|
+
- Remove curses as a hard dependency. It is optional. This can leverage the gem if it is present.
|
56
|
+
- Fix ruby -w warnings
|
57
|
+
|
58
|
+
## 2.1.0 - 2015-01-02
|
59
|
+
|
60
|
+
- Integer parser now supports underscore separator.
|
61
|
+
- Add Parser#usage and Parser#synopsis commands for creating a standard banner
|
62
|
+
message. Using Parser#banner directly will override both of those.
|
63
|
+
- Add Parser#ignore_invalid_options to prevent erroring on unknown options.
|
64
|
+
- Allow flags to act as switches if they have defaults set and no value is
|
65
|
+
passed on the commandline
|
66
|
+
- Parser#opt learned to accept a block or a :callback option which it will call
|
67
|
+
after parsing the option.
|
68
|
+
- Add Optimist::educate which displays the help message and dies.
|
69
|
+
- Reformat help message to be more GNUish.
|
70
|
+
- Fix command name in help message when script has no extension.
|
71
|
+
- Fix handling of newlines inside descriptions
|
72
|
+
- Documentation and other fixes.
|
73
|
+
|
74
|
+
## 2.0 - 2012-08-11
|
75
|
+
|
76
|
+
- Change flag logic: --no-X will always be false, and --X will always be true,
|
77
|
+
regardless of default.
|
78
|
+
- For flags that default to true, display --no-X instead of --X in the help
|
79
|
+
menu. Accept both versions on the commandline.
|
80
|
+
- Fix a spurious warning
|
81
|
+
- Update Rakefile to 1.9
|
82
|
+
- Minor documentation fixes
|
83
|
+
|
84
|
+
## 1.16.2 - 2010-04-06
|
85
|
+
|
86
|
+
- Bugfix in Optimist::options. Thanks to Brian C. Thomas for pointing it out.
|
87
|
+
|
88
|
+
## 1.16.1 - 2010-04-05
|
89
|
+
|
90
|
+
- Bugfix in Optimist::die method introduced in last release.
|
91
|
+
|
92
|
+
## 1.16 - 2010-04-01
|
93
|
+
|
94
|
+
- Add Optimist::with_standard_exception_handling method for easing the use of Parser directly.
|
95
|
+
- Handle scientific notation in float arguments, thanks to Will Fitzgerald.
|
96
|
+
- Drop hoe dependency.
|
97
|
+
|
98
|
+
## 1.15 - 2009-09-30
|
99
|
+
|
100
|
+
- Don't raise an exception when out of short arguments (thanks to Rafael
|
101
|
+
Sevilla for pointing out how dumb this behavior was).
|
102
|
+
|
103
|
+
## 1.14 - 2009-06-19
|
104
|
+
- Make :multi arguments default to [], not nil, when not set on the commandline.
|
105
|
+
- Minor commenting and error message improvements
|
106
|
+
|
107
|
+
## 1.13 - 2009-03-16
|
108
|
+
|
109
|
+
- Fix parsing of "--longarg=<value with spaces>".
|
110
|
+
|
111
|
+
## 1.12 - 2009-01-30
|
112
|
+
|
113
|
+
- Fix some unit test failures in the last release. Should be more careful.
|
114
|
+
- Make default short options only be assigned *after- all user-specified
|
115
|
+
short options. Now there's a little less juggling to do when you just
|
116
|
+
want to specify a few short options.
|
117
|
+
|
118
|
+
## 1.11 - 2009-01-29
|
119
|
+
|
120
|
+
- Set <opt>_given keys in the results hash for options that were specified
|
121
|
+
on the commandline.
|
122
|
+
|
123
|
+
## 1.10.2 - 2008-10-23
|
124
|
+
|
125
|
+
- No longer try `stty size` for screen size detection. Just use curses, and
|
126
|
+
screen users will have to deal with the screen clearing.
|
127
|
+
|
128
|
+
## 1.10.1 - 2008-10-22
|
129
|
+
|
130
|
+
- Options hash now responds to method calls as well as standard hash lookup.
|
131
|
+
- Default values for multi-occurrence parameters now autoboxed.
|
132
|
+
- The relationship between multi-value, multi-occurrence, and default values
|
133
|
+
improved and explained.
|
134
|
+
- Documentation improvements.
|
135
|
+
|
136
|
+
## 1.10 - 2008-10-21
|
137
|
+
|
138
|
+
- Added :io type for parameters that point to IO streams (filenames, URIs, etc).
|
139
|
+
- For screen size detection, first try `stty size` before loading Curses.
|
140
|
+
- Improved documentation.
|
141
|
+
|
142
|
+
## 1.9 - 2008-08-20
|
143
|
+
|
144
|
+
- Added 'stop_on_unknown' command to stop parsing on any unknown argument.
|
145
|
+
This is useful for handling sub-commands when you don't know the entire
|
146
|
+
set of commands up front. (E.g. if the initial arguments can change it.)
|
147
|
+
- Added a :multi option for parameters, signifying that they can be specified
|
148
|
+
multiple times.
|
149
|
+
- Added :ints, :strings, :doubles, and :floats option types, which can take
|
150
|
+
multiple arguments.
|
151
|
+
|
152
|
+
## 1.8.2 - 2008-06-25
|
153
|
+
|
154
|
+
- Bugfix for #conflicts and #depends error messages
|
155
|
+
|
156
|
+
## 1.8.1 - 2008-06-24
|
157
|
+
|
158
|
+
- Bugfix for short option autocreation
|
159
|
+
- More aggressive documentation
|
160
|
+
|
161
|
+
## 1.8 - 2008-06-16
|
162
|
+
|
163
|
+
- Sub-command support via Parser#stop_on
|
164
|
+
|
165
|
+
## 1.7.2 - 2008-01-16
|
166
|
+
|
167
|
+
- Ruby 1.9-ify. Apparently this means replacing :'s with ;'s.
|
168
|
+
|
169
|
+
## 1.7.1 - 2008-01-07
|
170
|
+
|
171
|
+
- Documentation improvements
|
172
|
+
|
173
|
+
## 1.7 - 2007-06-17
|
174
|
+
|
175
|
+
- Fix incorrect error message for multiple missing required arguments
|
176
|
+
(thanks to Neill Zero)
|
177
|
+
|
178
|
+
## 1.6 - 2007-04-01
|
179
|
+
|
180
|
+
- Don't attempt curses screen-width magic unless running on a terminal.
|
181
|
+
|
182
|
+
## 1.5 - 2007-03-31
|
183
|
+
|
184
|
+
- --help and --version do the right thing even if the rest of the
|
185
|
+
command line is incorrect.
|
186
|
+
- Added #conflicts and #depends to model dependencies and exclusivity
|
187
|
+
between arguments.
|
188
|
+
- Minor bugfixes.
|
189
|
+
|
190
|
+
## 1.4 - 2007-03-26
|
191
|
+
|
192
|
+
- Disable short options with :short => :none.
|
193
|
+
- Minor bugfixes and error message improvements.
|
194
|
+
|
195
|
+
## 1.3 - 2007-01-31
|
196
|
+
|
197
|
+
- Wrap at (screen width - 1) instead of screen width.
|
198
|
+
- User can override --help and --version.
|
199
|
+
- Bugfix in handling of -v and -h.
|
200
|
+
- More tests to confirm the above.
|
201
|
+
|
202
|
+
## 1.2 - 2007-01-31
|
203
|
+
|
204
|
+
- Minor documentation tweaks.
|
205
|
+
- Removed hoe dependency.
|
206
|
+
|
207
|
+
## 1.1 - 2007-01-30
|
208
|
+
|
209
|
+
- Optimist::options now passes any arguments as block arguments. Since
|
210
|
+
instance variables are not properly captured by the block, this
|
211
|
+
makes it slightly less noisy to pass them in as local variables.
|
212
|
+
(A real-life use for _why's cloaker!)
|
213
|
+
- Help display now preserves original argument order.
|
214
|
+
- Optimist::die now also has a single string form in case death is not
|
215
|
+
due to a single argument.
|
216
|
+
- Parser#text now an alias for Parser#banner, and can be called
|
217
|
+
multiple times, with the output being placed in the right position
|
218
|
+
in the help text.
|
219
|
+
- Slightly more indicative formatting for parameterized arguments.
|
220
|
+
|
221
|
+
## 1.0 - 2007-01-29
|
222
|
+
|
223
|
+
- Initial release.
|
10
224
|
|
11
|
-
[Unreleased]: https://github.com/ManageIQ/optimist/compare/v3.
|
225
|
+
[Unreleased]: https://github.com/ManageIQ/optimist/compare/v3.2.0...HEAD
|
226
|
+
[3.2.0]: https://github.com/ManageIQ/optimist/compare/v3.1.0...v3.2.0
|
12
227
|
[3.1.0]: https://github.com/ManageIQ/optimist/compare/v3.0.1...v3.1.0
|
228
|
+
[3.0.1]: https://github.com/ManageIQ/optimist/compare/v3.0.0...v3.0.1
|
229
|
+
[3.0.0]: https://github.com/ManageIQ/optimist/compare/v2.1.3...v3.0.0
|
230
|
+
[2.1.3]: https://github.com/ManageIQ/optimist/compare/v2.1.2...v2.1.3
|
231
|
+
[2.1.2]: https://github.com/ManageIQ/optimist/compare/v2.1.1...v2.1.2
|
232
|
+
[2.1.1]: https://github.com/ManageIQ/optimist/compare/v2.1.0...v2.1.1
|
@@ -0,0 +1,10 @@
|
|
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
|
10
|
+
|
@@ -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,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.
|