Module: ParseArgv
- Defined in:
- lib/parse-argv.rb,
lib/parse-argv/version.rb,
lib/parse-argv/conversion.rb
Overview
ParseArgv uses a help text of a command line interface (CLI) to find out how to parse a command line. It takes care that required command line arguments are given and optional arguments are consumed.
With a given help text and a command line it produces a Result which contains all values and meta data (ParseArgv.from). The Result::Values support type Conversion and contextual error handling (Result::Value#as).
For some debugging and test purpose it can also serialize a given help text to a informational format (ParseArgv.parse).
For details about the help text syntax see syntax.
Defined Under Namespace
Modules: Conversion Classes: Error, Result
Constant Summary collapse
- VERSION =
current version
'0.1.2'
Class Method Summary collapse
-
.from(help_text, argv = ARGV) ⇒ Result
Parses the given +help_text+ and command line +argv+ to create an Result.
-
.on_error(function = nil, &block) ⇒ ParseArgv
Defines custom error handler which will be called with the detected Error.
-
.parse(help_text) ⇒ Array<Hash>
Parses the given +help_text+ and returns descriptive information about the commands found.
Class Method Details
.from(help_text, argv = ARGV) ⇒ Result
Parses the given +help_text+ and command line +argv+ to create an Result.
53 54 55 56 57 58 |
# File 'lib/parse-argv.rb', line 53 def self.from(help_text, argv = ARGV) result = Result.new(*Assemble[help_text, argv]) block_given? ? yield(result) : result rescue Error => e @on_error&.call(e) or raise end |
.on_error(function) ⇒ ParseArgv .on_error(&block) ⇒ ParseArgv
Defines custom error handler which will be called with the detected Error.
By default the error handler writes the ParseArgv::Error#message prefixed with related ParseArgv::Error#command name to $std_err and terminates the application with the suggested ParseArgv::Error#code.
99 100 101 102 103 |
# File 'lib/parse-argv.rb', line 99 def self.on_error(function = nil, &block) function ||= block or return @on_error @on_error = function == :raise ? nil : function self end |
.parse(help_text) ⇒ Array<Hash>
Parses the given +help_text+ and returns descriptive information about the commands found.
This method can be used to test a +help_text+.
71 72 73 |
# File 'lib/parse-argv.rb', line 71 def self.parse(help_text) Assemble.commands(help_text).map!(&:to_h) end |