optimist_xl 3.1.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +19 -0
- data/.travis.yml +13 -0
- data/FAQ.txt +92 -0
- data/Gemfile +4 -0
- data/History.txt +177 -0
- data/README.md +81 -0
- data/Rakefile +15 -0
- data/examples/a_basic_example.rb +10 -0
- data/examples/banners1.rb +11 -0
- data/examples/banners2.rb +12 -0
- data/examples/banners3.rb +14 -0
- data/examples/medium_example.rb +15 -0
- data/examples/permitted.rb +15 -0
- data/examples/subcommands.rb +15 -0
- data/examples/types.rb +12 -0
- data/examples/types_custom.rb +34 -0
- data/lib/optimist_xl.rb +1297 -0
- data/lib/optimist_xl/chronic.rb +36 -0
- data/optimist_xl.gemspec +35 -0
- data/test/optimist_xl/command_line_error_test.rb +27 -0
- data/test/optimist_xl/help_needed_test.rb +19 -0
- data/test/optimist_xl/parser_educate_test.rb +177 -0
- data/test/optimist_xl/parser_opt_test.rb +14 -0
- data/test/optimist_xl/parser_parse_test.rb +79 -0
- data/test/optimist_xl/parser_test.rb +1404 -0
- data/test/optimist_xl/permitted_test.rb +92 -0
- data/test/optimist_xl/subcommands_test.rb +153 -0
- data/test/optimist_xl/version_needed_test.rb +19 -0
- data/test/optimist_xl_test.rb +190 -0
- data/test/support/assert_helpers.rb +52 -0
- data/test/test_helper.rb +22 -0
- metadata +140 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d15a0ddf0d2205266ff1df22f86c3e96c9bcccfe449394840194269ff35de564
|
4
|
+
data.tar.gz: b84c5a820184de6941b54054d7ab3786be5c4b75b5e934d99021e21838cb1355
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: fbe733a8b0f74918aef616ed3a2d1d5a6f9ff7cc414de871f8bdfb33aba1186899f4d25c8e66c08bfbfe3dc2dcc87514e7d2b2e3f1859d1b96a4bddc773c2c77
|
7
|
+
data.tar.gz: 8de3c39ad1356af6b5e599127b0afefd78e4ccec11f5ec06b801eac94ad631a5e6fa1c266f64e94c2a0b8c11a137a6978409767c2d657266eb0643a51b688541
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/FAQ.txt
ADDED
@@ -0,0 +1,92 @@
|
|
1
|
+
Optimist FAQ
|
2
|
+
-----------
|
3
|
+
|
4
|
+
Q: Why should I use Optimist?
|
5
|
+
A: Because it will take you fewer lines of code to parse commandline arguments
|
6
|
+
than anything else out there.
|
7
|
+
|
8
|
+
Like this:
|
9
|
+
opts = Optimist::options do
|
10
|
+
opt :monkey, "Use monkey mode"
|
11
|
+
opt :goat, "Use goat mode", :default => true
|
12
|
+
opt :num_limbs, "Set number of limbs", :default => 4
|
13
|
+
end
|
14
|
+
|
15
|
+
That's it. 'opts' will be a hash and you can do whatever you want with it.
|
16
|
+
You don't have to mix processing code with the declarations. You don't have
|
17
|
+
to make a class for every option (what is this, Java?). You don't have to
|
18
|
+
write more than 1 line of code per option.
|
19
|
+
|
20
|
+
Plus, you get a beautiful help screen that detects your terminal width and
|
21
|
+
wraps appropriately.
|
22
|
+
|
23
|
+
Q: What is the philosophy behind Optimist?
|
24
|
+
A: Optimist does the parsing and gives you a hash table of options. You then
|
25
|
+
write whatever fancy constraint logic you need as regular Ruby code operating
|
26
|
+
on that hash table.
|
27
|
+
|
28
|
+
(Optimist does support limited constraints (see #conflicts and #depends), but
|
29
|
+
any non-trivial program will probably need to get fancier.)
|
30
|
+
|
31
|
+
Then if you need to abort and tell the user to fix their command line at any
|
32
|
+
point, you can call #die and Optimist will do that for you in a pretty way.
|
33
|
+
|
34
|
+
Q: What happens to the other stuff on the commandline?
|
35
|
+
A: Anything Optimist doesn't recognize as an option or as an option parameter is
|
36
|
+
left in ARGV for you to process.
|
37
|
+
|
38
|
+
Q: Does Optimist support multiple-value arguments?
|
39
|
+
A: Yes. If you set the :type of an option to something plural, like ":ints",
|
40
|
+
":strings", ":doubles", ":floats", ":ios", it will accept multiple arguments
|
41
|
+
on the commandline, and the value will be an array of the parameters.
|
42
|
+
|
43
|
+
Q: Does Optimist support arguments that can be given multiple times?
|
44
|
+
A: Yes. If you set :multi to true, then the argument can appear multiple times
|
45
|
+
on the commandline, and the value will be an array of the parameters.
|
46
|
+
|
47
|
+
Q: Does Optimist support subcommands?
|
48
|
+
A: Yes: you can direct Optimist to stop processing when it encounters certain
|
49
|
+
tokens. Then you can re-call Optimist with the subcommand-specific
|
50
|
+
configuration to process the rest of the commandline.
|
51
|
+
|
52
|
+
See the third example on the webpage.
|
53
|
+
|
54
|
+
(And if you don't know the subcommands ahead of time, you can call
|
55
|
+
#stop_on_unknown, which will cause Optimist to stop when it encounters any
|
56
|
+
unknown token. This might be more trouble than its worth if you're also
|
57
|
+
passing filenames on the commandline.)
|
58
|
+
|
59
|
+
Q: Why does Optimist disallow numeric short argument names, like '-1' and '-9'?
|
60
|
+
A: Because it's ambiguous whether these are arguments or negative integer or
|
61
|
+
floating-point parameters to arguments. E.g., is "-f -3" a negative floating
|
62
|
+
point parameter to -f, or two separate arguments?
|
63
|
+
|
64
|
+
Q: What was the big change in version 2.0?
|
65
|
+
A: The big change was boolean parameter (aka flag) handling. In pre-2.0,
|
66
|
+
not specifying a flag on the commandline would result in the option being set
|
67
|
+
to its default value; specifying it on the commandline would result in the
|
68
|
+
option being set to the opposite of its default value. This was weird for
|
69
|
+
options with a default of true:
|
70
|
+
opt :magic, "Use magic", default: true
|
71
|
+
Using --magic with the above configuration would result in a :magic => false
|
72
|
+
value in the options hash.
|
73
|
+
|
74
|
+
In 2.0, we introduce the GNU-style notion of a --no-x parameter. Now,
|
75
|
+
specifying --x will always set the option :x to true, regardless of its
|
76
|
+
default value, and specifying --no-x will always set the option :x to false,
|
77
|
+
regardless of its default value. The default value only comes into play when
|
78
|
+
neither form is given on the commandline.
|
79
|
+
|
80
|
+
E.g.:
|
81
|
+
opt :magic, "Use magic", :default => true
|
82
|
+
|
83
|
+
Using --magic will result in :magic => true, and --no-magic will result in
|
84
|
+
:magic => false, and neither will result in :magic => true.
|
85
|
+
|
86
|
+
There is one exception: if the option itself starts with a "no_", then you'll
|
87
|
+
get the opposite behavior:
|
88
|
+
|
89
|
+
opt :no_magic, "Don't use magic", :default => true
|
90
|
+
|
91
|
+
Using --magic will result in :no_magic => false, and --no-magic will result in
|
92
|
+
:no_magic => true, and neither will result in :no_magic => true.
|
data/Gemfile
ADDED
data/History.txt
ADDED
@@ -0,0 +1,177 @@
|
|
1
|
+
== [3.1.1] / 2020-01-20
|
2
|
+
|
3
|
+
* The gem has been forked from optimist to optimist_xl
|
4
|
+
* Added "native" subcommands support
|
5
|
+
* defaulting to inexact-match of long options
|
6
|
+
* defaulting to enabling suggestions for mistyped options
|
7
|
+
* ability to globally prevent short-options by default
|
8
|
+
* permitted and permitted_response keywords now available
|
9
|
+
|
10
|
+
== [3.0.0] / 2018-08-24
|
11
|
+
|
12
|
+
* The gem has been officially renamed to optimist
|
13
|
+
|
14
|
+
== [2.1.3] / 2018-07-05
|
15
|
+
|
16
|
+
* 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)
|
17
|
+
* Fixed implementation of ignore_invalid_options. (thanks @metcalf)
|
18
|
+
* Various warning cleanup for ruby 2.1, 2.3, etc. (thanks @nanobowers)
|
19
|
+
* Optimist.die can now accept an error code.
|
20
|
+
* fixed default (thanks @nanobowers)
|
21
|
+
* Change from ruby license to MIT license in the code.
|
22
|
+
|
23
|
+
== [2.1.2] / 2015-03-10
|
24
|
+
* loosen mime-types requirements (for better ruby 1.8.7 support)
|
25
|
+
* use io/console gem instead of curses (for better jruby support)
|
26
|
+
* fix parsing bug when chronic gem is not available
|
27
|
+
* allow default array to be empty if a type is specified
|
28
|
+
* better specified license and better spec coverage
|
29
|
+
|
30
|
+
== [2.1.1] / 2015-01-03
|
31
|
+
* Remove curses as a hard dependency. It is optional. This can leverage the gem if it is present.
|
32
|
+
* Fix ruby -w warnings
|
33
|
+
|
34
|
+
== 2.1.0 / 2015-01-02
|
35
|
+
* Integer parser now supports underscore separator.
|
36
|
+
* Add Parser#usage and Parser#synopsis commands for creating a standard banner
|
37
|
+
message. Using Parser#banner directly will override both of those.
|
38
|
+
* Add Parser#ignore_invalid_options to prevent erroring on unknown options.
|
39
|
+
* Allow flags to act as switches if they have defaults set and no value is
|
40
|
+
passed on the commandline
|
41
|
+
* Parser#opt learned to accept a block or a :callback option which it will call
|
42
|
+
after parsing the option.
|
43
|
+
* Add Optimist::educate which displays the help message and dies.
|
44
|
+
* Reformat help message to be more GNUish.
|
45
|
+
* Fix command name in help message when script has no extension.
|
46
|
+
* Fix handling of newlines inside descriptions
|
47
|
+
* Documentation and other fixes.
|
48
|
+
|
49
|
+
== 2.0 / 2012-08-11
|
50
|
+
* Change flag logic: --no-X will always be false, and --X will always be true,
|
51
|
+
regardless of default.
|
52
|
+
* For flags that default to true, display --no-X instead of --X in the help
|
53
|
+
menu. Accept both versions on the commandline.
|
54
|
+
* Fix a spurious warning
|
55
|
+
* Update Rakefile to 1.9
|
56
|
+
* Minor documentation fixes
|
57
|
+
|
58
|
+
== 1.16.2 / 2010-04-06
|
59
|
+
* Bugfix in Optimist::options. Thanks to Brian C. Thomas for pointing it out.
|
60
|
+
|
61
|
+
== 1.16.1 / 2010-04-05
|
62
|
+
* Bugfix in Optimist::die method introduced in last release.
|
63
|
+
|
64
|
+
== 1.16 / 2010-04-01
|
65
|
+
* Add Optimist::with_standard_exception_handling method for easing the use of Parser directly.
|
66
|
+
* Handle scientific notation in float arguments, thanks to Will Fitzgerald.
|
67
|
+
* Drop hoe dependency.
|
68
|
+
|
69
|
+
== 1.15 / 2009-09-30
|
70
|
+
* Don't raise an exception when out of short arguments (thanks to Rafael
|
71
|
+
Sevilla for pointing out how dumb this behavior was).
|
72
|
+
|
73
|
+
== 1.14 / 2009-06-19
|
74
|
+
* Make :multi arguments default to [], not nil, when not set on the commandline.
|
75
|
+
* Minor commenting and error message improvements
|
76
|
+
|
77
|
+
== 1.13 / 2009-03-16
|
78
|
+
* Fix parsing of "--longarg=<value with spaces>".
|
79
|
+
|
80
|
+
== 1.12 / 2009-01-30
|
81
|
+
* Fix some unit test failures in the last release. Should be more careful.
|
82
|
+
* Make default short options only be assigned *after* all user-specified
|
83
|
+
short options. Now there's a little less juggling to do when you just
|
84
|
+
want to specify a few short options.
|
85
|
+
|
86
|
+
== 1.11 / 2009-01-29
|
87
|
+
* Set <opt>_given keys in the results hash for options that were specified
|
88
|
+
on the commandline.
|
89
|
+
|
90
|
+
== 1.10.2 / 2008-10-23
|
91
|
+
* No longer try `stty size` for screen size detection. Just use curses, and
|
92
|
+
screen users will have to deal with the screen clearing.
|
93
|
+
|
94
|
+
== 1.10.1 / 2008-10-22
|
95
|
+
* Options hash now responds to method calls as well as standard hash lookup.
|
96
|
+
* Default values for multi-occurrence parameters now autoboxed.
|
97
|
+
* The relationship between multi-value, multi-occurrence, and default values
|
98
|
+
improved and explained.
|
99
|
+
* Documentation improvements.
|
100
|
+
|
101
|
+
== 1.10 / 2008-10-21
|
102
|
+
* Added :io type for parameters that point to IO streams (filenames, URIs, etc).
|
103
|
+
* For screen size detection, first try `stty size` before loading Curses.
|
104
|
+
* Improved documentation.
|
105
|
+
|
106
|
+
== 1.9 / 2008-08-20
|
107
|
+
* Added 'stop_on_unknown' command to stop parsing on any unknown argument.
|
108
|
+
This is useful for handling sub-commands when you don't know the entire
|
109
|
+
set of commands up front. (E.g. if the initial arguments can change it.)
|
110
|
+
* Added a :multi option for parameters, signifying that they can be specified
|
111
|
+
multiple times.
|
112
|
+
* Added :ints, :strings, :doubles, and :floats option types, which can take
|
113
|
+
multiple arguments.
|
114
|
+
|
115
|
+
== 1.8.2 / 2008-06-25
|
116
|
+
* Bugfix for #conflicts and #depends error messages
|
117
|
+
|
118
|
+
== 1.8.1 / 2008-06-24
|
119
|
+
* Bugfix for short option autocreation
|
120
|
+
* More aggressive documentation
|
121
|
+
|
122
|
+
== 1.8 / 2008-06-16
|
123
|
+
* Sub-command support via Parser#stop_on
|
124
|
+
|
125
|
+
== 1.7.2 / 2008-01-16
|
126
|
+
* Ruby 1.9-ify. Apparently this means replacing :'s with ;'s.
|
127
|
+
|
128
|
+
== 1.7.1 / 2008-01-07
|
129
|
+
* Documentation improvements
|
130
|
+
|
131
|
+
== 1.7 / 2007-06-17
|
132
|
+
* Fix incorrect error message for multiple missing required arguments
|
133
|
+
(thanks to Neill Zero)
|
134
|
+
|
135
|
+
== 1.6 / 2007-04-01
|
136
|
+
* Don't attempt curses screen-width magic unless running on a terminal.
|
137
|
+
|
138
|
+
== 1.5 / 2007-03-31
|
139
|
+
* --help and --version do the right thing even if the rest of the
|
140
|
+
command line is incorrect.
|
141
|
+
* Added #conflicts and #depends to model dependencies and exclusivity
|
142
|
+
between arguments.
|
143
|
+
* Minor bugfixes.
|
144
|
+
|
145
|
+
== 1.4 / 2007-03-26
|
146
|
+
* Disable short options with :short => :none.
|
147
|
+
* Minor bugfixes and error message improvements.
|
148
|
+
|
149
|
+
== 1.3 / 2007-01-31
|
150
|
+
* Wrap at (screen width - 1) instead of screen width.
|
151
|
+
* User can override --help and --version.
|
152
|
+
* Bugfix in handling of -v and -h.
|
153
|
+
* More tests to confirm the above.
|
154
|
+
|
155
|
+
== 1.2 / 2007-01-31
|
156
|
+
* Minor documentation tweaks.
|
157
|
+
* Removed hoe dependency.
|
158
|
+
|
159
|
+
== 1.1 / 2007-01-30
|
160
|
+
* Optimist::options now passes any arguments as block arguments. Since
|
161
|
+
instance variables are not properly captured by the block, this
|
162
|
+
makes it slightly less noisy to pass them in as local variables.
|
163
|
+
(A real-life use for _why's cloaker!)
|
164
|
+
* Help display now preserves original argument order.
|
165
|
+
* Optimist::die now also has a single string form in case death is not
|
166
|
+
due to a single argument.
|
167
|
+
* Parser#text now an alias for Parser#banner, and can be called
|
168
|
+
multiple times, with the output being placed in the right position
|
169
|
+
in the help text.
|
170
|
+
* Slightly more indicative formatting for parameterized arguments.
|
171
|
+
|
172
|
+
== 1.0 / 2007-01-29
|
173
|
+
* Initial release.
|
174
|
+
|
175
|
+
[2.1.3]: https://github.com/ManageIQ/optimist/compare/v2.1.2...v2.1.3
|
176
|
+
[2.1.2]: https://github.com/ManageIQ/optimist/compare/v2.1.1...v2.1.2
|
177
|
+
[2.1.1]: https://github.com/ManageIQ/optimist/compare/v2.1.0...v2.1.1
|
data/README.md
ADDED
@@ -0,0 +1,81 @@
|
|
1
|
+
# OptimistXL
|
2
|
+
|
3
|
+
http://github.com/nanobowers/optimist_xl
|
4
|
+
|
5
|
+
[![Build Status](https://travis-ci.org/nanobowers/optimist_xl.svg)](https://travis-ci.org/nanobowers/optimist_xl)
|
6
|
+
|
7
|
+
## Documentation
|
8
|
+
|
9
|
+
- Wiki: http://github.com/nanobowers/optimist_xl/wiki
|
10
|
+
- Examples: http://github.com/nanobowers/optimist_xl/tree/master/examples
|
11
|
+
- Code quickstart: See `OptimistXL.options` and then `OptimistXL::Parser#opt`.
|
12
|
+
|
13
|
+
## Description
|
14
|
+
|
15
|
+
OptimistXL is a commandline option parser for Ruby that just gets out of your way.
|
16
|
+
One line of code per option is all you need to write. For that, you get a nice
|
17
|
+
automatically-generated help page, robust option parsing, and sensible defaults
|
18
|
+
for everything you don't specify.
|
19
|
+
|
20
|
+
## Features
|
21
|
+
|
22
|
+
- Dirt-simple usage.
|
23
|
+
- Sensible defaults. No tweaking necessary, much tweaking possible.
|
24
|
+
- Support for long options, short options, subcommands, and automatic type validation and
|
25
|
+
conversion.
|
26
|
+
- Automatic help message generation, wrapped to current screen width.
|
27
|
+
|
28
|
+
## Extended features
|
29
|
+
|
30
|
+
### Parser Settings
|
31
|
+
- Automatic suggestions whens incorrect options are given
|
32
|
+
- disable with `suggestions: false`
|
33
|
+
- Inexact matching of long arguments
|
34
|
+
- disable with `exact_match: true`
|
35
|
+
- Available prevention of short-arguments by default
|
36
|
+
- enable with `explicit_short_opts: true`
|
37
|
+
|
38
|
+
### Option Settings
|
39
|
+
|
40
|
+
Permitted options allow specifying valid choices for an option using lists, ranges or regexp's
|
41
|
+
- `permitted:` to specify a allow lists, ranges or regexp filtering of options.
|
42
|
+
- `permitted_response:` can be added to provide more explicit output when incorrect choices are given.
|
43
|
+
- see [example](examples/permitted.rb)
|
44
|
+
- concept and code via @akhoury6
|
45
|
+
|
46
|
+
### Subcommands
|
47
|
+
"Native" subcommand support
|
48
|
+
- see [example](examples/subcommands.rb)
|
49
|
+
- ideas borrowed from https://github.com/jwliechty/trollop-subcommands
|
50
|
+
|
51
|
+
## Requirements
|
52
|
+
|
53
|
+
* Ruby 2.2+
|
54
|
+
* A burning desire to write less code.
|
55
|
+
|
56
|
+
## Install
|
57
|
+
|
58
|
+
* `gem install optimist_xl`
|
59
|
+
|
60
|
+
## Synopsis
|
61
|
+
|
62
|
+
```ruby
|
63
|
+
require 'optimist_xl'
|
64
|
+
opts = OptimistXL::options do
|
65
|
+
opt :monkey, "Use monkey mode" # flag --monkey, default false
|
66
|
+
opt :name, "Monkey name", :type => :string # string --name <s>, default nil
|
67
|
+
opt :num_limbs, "Number of limbs", :default => 4 # integer --num-limbs <i>, default to 4
|
68
|
+
end
|
69
|
+
|
70
|
+
p opts # a hash: { :monkey=>false, :name=>nil, :num_limbs=>4, :help=>false }
|
71
|
+
```
|
72
|
+
|
73
|
+
## License
|
74
|
+
|
75
|
+
Copyright © 2008-2014 [William Morgan](http://masanjin.net/).
|
76
|
+
|
77
|
+
Copyright © 2014 Red Hat, Inc.
|
78
|
+
|
79
|
+
Copyright © 2019 Ben Bowers
|
80
|
+
|
81
|
+
OptimistXL is released under the [MIT License](http://www.opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require 'bundler/gem_tasks'
|
2
|
+
require 'rake/testtask'
|
3
|
+
|
4
|
+
task :default => :test
|
5
|
+
|
6
|
+
Rake::TestTask.new do |t|
|
7
|
+
t.libs << 'test'
|
8
|
+
t.pattern = "test/**/*_test.rb"
|
9
|
+
end
|
10
|
+
|
11
|
+
begin
|
12
|
+
require 'coveralls/rake/task'
|
13
|
+
Coveralls::RakeTask.new
|
14
|
+
rescue LoadError
|
15
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require_relative '../lib/optimist_xl'
|
3
|
+
|
4
|
+
opts = OptimistXL::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,11 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
require_relative '../lib/optimist_xl'
|
3
|
+
|
4
|
+
opts = OptimistXL::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_xl'
|
3
|
+
|
4
|
+
opts = OptimistXL::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
|