Class: Ardb::CLIRB

Inherits:
Object
  • Object
show all
Defined in:
lib/ardb/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

Instance Method Summary collapse

Constructor Details

#initialize(&block) ⇒ CLIRB

Returns a new instance of CLIRB.



59
60
61
62
63
64
65
66
67
68
69
# File 'lib/ardb/cli.rb', line 59

def initialize(&block)
  @options = []; instance_eval(&block) if block
  require 'optparse'
  @data, @args, @opts = [], [], {}; @parser = OptionParser.new do |p|
    p.banner = ''; @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

#argsObject (readonly)

Returns the value of attribute args.



57
58
59
# File 'lib/ardb/cli.rb', line 57

def args
  @args
end

#argvObject (readonly)

Returns the value of attribute argv.



57
58
59
# File 'lib/ardb/cli.rb', line 57

def argv
  @argv
end

#dataObject (readonly)

Returns the value of attribute data.



57
58
59
# File 'lib/ardb/cli.rb', line 57

def data
  @data
end

#optsObject (readonly)

Returns the value of attribute opts.



57
58
59
# File 'lib/ardb/cli.rb', line 57

def opts
  @opts
end

Instance Method Details

#inspectObject



79
80
81
# File 'lib/ardb/cli.rb', line 79

def inspect
  "#<#{self.class}:#{'0x0%x' % (object_id << 1)} @data=#{@data.inspect}>"
end

#option(*args) ⇒ Object



71
# File 'lib/ardb/cli.rb', line 71

def option(*args); @options << Option.new(*args); end

#parse!(argv) ⇒ Object



72
73
74
75
76
77
# File 'lib/ardb/cli.rb', line 72

def parse!(argv)
  @args = (argv || []).dup.tap do |args_list|
    begin; @parser.parse!(args_list)
    rescue OptionParser::ParseError => err; raise Error, err.message; end
  end; @data = @args + [@opts]
end

#to_sObject



78
# File 'lib/ardb/cli.rb', line 78

def to_s; @parser.to_s; end