Module: Recheck::Optimist
- Defined in:
- lib/recheck/vendor/optimist.rb
Overview
change 1, see top of file
Defined Under Namespace
Classes: BooleanOption, CommandlineError, ConflictConstraint, Constraint, DateArrayOption, DateOption, DependConstraint, EitherConstraint, FloatArrayOption, FloatOption, HelpNeeded, IOArrayOption, IOOption, IntegerArrayOption, IntegerOption, LongNames, Option, Parser, ShortNames, StringArrayOption, StringOption, VersionNeeded
Constant Summary collapse
- VERSION =
"3.2.0"- FLOAT_RE =
Regex for floating point numbers
/^-?((\d+(\.\d+)?)|(\.\d+))([eE][-+]?[\d]+)?$/- PARAM_RE =
Regex for parameters
/^-(-|\.$|[^\d\.])/
Class Method Summary collapse
-
.die(arg, msg = nil, error_code = nil) ⇒ Object
Informs the user that their usage of ‘arg’ was wrong, as detailed by ‘msg’, and dies.
-
.educate ⇒ Object
Displays the help message and dies.
-
.options(args = ARGV, *a, &b) ⇒ Object
The easy, syntactic-sugary entry method into Optimist.
-
.with_standard_exception_handling(parser) ⇒ Object
If Optimist::options doesn’t do quite what you want, you can create a Parser object and call Parser#parse on it.
Class Method Details
.die(arg, msg = nil, error_code = nil) ⇒ Object
Informs the user that their usage of ‘arg’ was wrong, as detailed by ‘msg’, and dies. Example:
do
opt :volume, :default => 0.0
end
die :volume, "too loud" if opts[:volume] > 10.0
die :volume, "too soft" if opts[:volume] < 0.1
In the one-argument case, simply print that message, a notice about -h, and die. Example:
do
opt :whatever # ...
end
Optimist::die "need at least one filename" if ARGV.empty?
An exit code can be provide if needed
Optimist::die "need at least one filename", -2 if ARGV.empty?
1330 1331 1332 1333 1334 1335 1336 |
# File 'lib/recheck/vendor/optimist.rb', line 1330 def die(arg, msg = nil, error_code = nil) if @last_parser @last_parser.die arg, msg, error_code else raise ArgumentError, "Optimist::die can only be called after Optimist::options" end end |
.educate ⇒ Object
Displays the help message and dies. Example:
do
opt :volume, :default => 0.0
"Usage:\n \#$0 [options] <name>\nwhere [options] are:\n"
end
Optimist::educate if ARGV.empty?
1350 1351 1352 1353 1354 1355 1356 1357 |
# File 'lib/recheck/vendor/optimist.rb', line 1350 def educate if @last_parser @last_parser.educate exit else raise ArgumentError, "Optimist::educate can only be called after Optimist::options" end end |
.options(args = ARGV, *a, &b) ⇒ Object
The easy, syntactic-sugary entry method into Optimist. Creates a Parser, passes the block to it, then parses args with it, handling any errors or requests for help or version information appropriately (and then exiting). Modifies args in place. Returns a hash of option values.
The block passed in should contain zero or more calls to opt (Parser#opt), zero or more calls to text (Parser#text), and probably a call to version (Parser#version).
The returned block contains a value for every option specified with opt. The value will be the value given on the commandline, or the default value if the option was not specified on the commandline. For every option specified on the commandline, a key “<option name>_given” will also be set in the hash.
Example:
require 'optimist'
opts = Optimist:: do
opt :monkey, "Use monkey mode" # a flag --monkey, defaulting to false
opt :name, "Monkey name", :type => :string # a string --name <s>, defaulting to nil
opt :num_limbs, "Number of limbs", :default => 4 # an integer --num-limbs <i>, defaulting to 4
end
## if called with no arguments
p opts # => {:monkey=>false, :name=>nil, :num_limbs=>4, :help=>false}
## if called with --monkey
p opts # => {:monkey=>true, :name=>nil, :num_limbs=>4, :help=>false, :monkey_given=>true}
Settings:
Optimist:: and Optimist::Parser.new accept +settings+ to control how
are interpreted. These settings are given as hash arguments, e.g:
opts = Optimist::(ARGV, exact_match: false) do
opt :foobar, 'messed up'
opt :forget, 'forget it'
end
+settings+ include:
* :exact_match : (default=true) Allow minimum unambigous number of characters to match a long option
* :suggestions : (default=true) Enables suggestions when unknown arguments are given and DidYouMean is installed. DidYouMean comes standard with Ruby 2.3+
* :implicit_short_opts : (default=true) Short will only be created where explicitly defined. If you do not like short-, this will prevent having to define :short=> :none for all of your .
Because Optimist:: uses a default argument for +args+, you must pass that argument when using the settings feature.
See more examples at www.manageiq.org/optimist
1266 1267 1268 1269 |
# File 'lib/recheck/vendor/optimist.rb', line 1266 def (args = ARGV, *a, &b) @last_parser = Parser.new(*a, &b) with_standard_exception_handling(@last_parser) { @last_parser.parse args } end |
.with_standard_exception_handling(parser) ⇒ Object
If Optimist::options doesn’t do quite what you want, you can create a Parser object and call Parser#parse on it. That method will throw CommandlineError, HelpNeeded and VersionNeeded exceptions when necessary; if you want to have these handled for you in the standard manner (e.g. show the help and then exit upon an HelpNeeded exception), call your code from within a block passed to this method.
Note that this method will call System#exit after handling an exception!
Usage example:
require 'optimist'
p = Optimist::Parser.new do
opt :monkey, "Use monkey mode" # a flag --monkey, defaulting to false
opt :goat, "Use goat mode", :default => true # a flag --goat, defaulting to true
end
opts = Optimist::with_standard_exception_handling p do
o = p.parse ARGV
raise Optimist::HelpNeeded if ARGV.empty? # show help screen
o
end
Requires passing in the parser object.
1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 |
# File 'lib/recheck/vendor/optimist.rb', line 1296 def with_standard_exception_handling(parser) yield rescue CommandlineError => e parser.die(e., nil, e.error_code) rescue HelpNeeded parser.educate exit rescue VersionNeeded puts parser.version exit end |