Class: Qs::CLIRB
- Inherits:
-
Object
- Object
- Qs::CLIRB
- Defined in:
- lib/qs/cli.rb
Overview
Version 1.0.0, github.com/redding/cli.rb
Defined Under Namespace
Classes: Option
Constant Summary collapse
- Error =
Class.new(RuntimeError)
- HelpExit =
Class.new(RuntimeError)
- VersionExit =
Class.new(RuntimeError)
Instance Attribute Summary collapse
-
#args ⇒ Object
readonly
Returns the value of attribute args.
-
#argv ⇒ Object
readonly
Returns the value of attribute argv.
-
#data ⇒ Object
readonly
Returns the value of attribute data.
-
#opts ⇒ Object
readonly
Returns the value of attribute opts.
Instance Method Summary collapse
-
#initialize(&block) ⇒ CLIRB
constructor
A new instance of CLIRB.
- #inspect ⇒ Object
- #option(*args) ⇒ Object
- #parse!(argv) ⇒ Object
- #to_s ⇒ Object
Constructor Details
#initialize(&block) ⇒ CLIRB
Returns a new instance of CLIRB.
74 75 76 77 78 79 80 81 82 83 84 |
# File 'lib/qs/cli.rb', line 74 def initialize(&block) @options = []; instance_eval(&block) if block require 'optparse' @data, @args, @opts = [], [], {}; @parser = OptionParser.new do |p| p. = ''; @options.each do |o| @opts[o.name] = o.value; p.on(*o.parser_args){ |v| @opts[o.name] = v } end p.on_tail('--version', ''){ |v| raise VersionExit, v.to_s } p.on_tail('--help', ''){ |v| raise HelpExit, v.to_s } end end |
Instance Attribute Details
#args ⇒ Object (readonly)
Returns the value of attribute args.
72 73 74 |
# File 'lib/qs/cli.rb', line 72 def args @args end |
#argv ⇒ Object (readonly)
Returns the value of attribute argv.
72 73 74 |
# File 'lib/qs/cli.rb', line 72 def argv @argv end |
#data ⇒ Object (readonly)
Returns the value of attribute data.
72 73 74 |
# File 'lib/qs/cli.rb', line 72 def data @data end |
#opts ⇒ Object (readonly)
Returns the value of attribute opts.
72 73 74 |
# File 'lib/qs/cli.rb', line 72 def opts @opts end |
Instance Method Details
#inspect ⇒ Object
94 95 96 |
# File 'lib/qs/cli.rb', line 94 def inspect "#<#{self.class}:#{'0x0%x' % (object_id << 1)} @data=#{@data.inspect}>" end |
#option(*args) ⇒ Object
86 |
# File 'lib/qs/cli.rb', line 86 def option(*args); @options << Option.new(*args); end |
#parse!(argv) ⇒ Object
87 88 89 90 91 92 |
# File 'lib/qs/cli.rb', line 87 def parse!(argv) @args = (argv || []).dup.tap do |args_list| begin; @parser.parse!(args_list) rescue OptionParser::ParseError => err; raise Error, err.; end end; @data = @args + [@opts] end |
#to_s ⇒ Object
93 |
# File 'lib/qs/cli.rb', line 93 def to_s; @parser.to_s; end |