Class: Clio::Usage::Interface

Inherits:
Object
  • Object
show all
Defined in:
lib/clio/usage/interface.rb

Overview

Command Interface (toplevel signature)

The end result provide by Usage::Parser#parse. This class consists of an array of command signatures and parse errors.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(signatures = [], errors = []) ⇒ Interface

Returns a new instance of Interface.



19
20
21
22
# File 'lib/clio/usage/interface.rb', line 19

def initialize(signatures=[], errors=[])
  @signatures = signatures
  @errors     = errors
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(s, *a) ⇒ Object



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
# File 'lib/clio/usage/interface.rb', line 99

def method_missing(s, *a)
  s = s.to_s
  case s
  #when /[=]$/
  #  n = s.chomp('=')
  #  usage.option(n).type(*a)
  #  #parser.parse
  #  res = parser.options[n.to_sym]
  #when /[!]$/
  #  n = s.chomp('!')
  #  res = parser.parse
  when /[?]$/
    options[s.chomp('?').to_sym]
  else
    options[s.to_sym]
  end
end

Instance Attribute Details

#errorsObject (readonly) Also known as: parse_errors

Returns the value of attribute errors.



14
15
16
# File 'lib/clio/usage/interface.rb', line 14

def errors
  @errors
end

#signaturesObject (readonly)

Returns the value of attribute signatures.



13
14
15
# File 'lib/clio/usage/interface.rb', line 13

def signatures
  @signatures
end

Instance Method Details

#[](i) ⇒ Object

Index on each subcommand, with 0 being the toplevel command.



88
89
90
# File 'lib/clio/usage/interface.rb', line 88

def [](i)
  @signatures[i]
end

#argumentsObject



56
57
58
59
60
61
62
63
64
65
# File 'lib/clio/usage/interface.rb', line 56

def arguments
  #parse unless parsed?
  @arguments ||= (
    m = []
    @signatures.each do |s|
      m.concat(s.arguments)
    end
    m
  )
end

#commandObject

TODO: Join by what character?



25
26
27
28
# File 'lib/clio/usage/interface.rb', line 25

def command
  return nil if commands.empty?
  return commands.join(' ')
end

#commandsObject



31
32
33
34
35
36
37
38
39
40
# File 'lib/clio/usage/interface.rb', line 31

def commands
  #parse unless parsed?
  @commands ||= (
    a = []
    @signatures[1..-1].each do |s|
      a << s.command.to_s
    end
    a
  )
end

#optionsObject Also known as: switches



43
44
45
46
47
48
49
50
51
52
# File 'lib/clio/usage/interface.rb', line 43

def options
  #parse unless parsed?
  @options ||= (
    h = {}
    @signatures.each do |s|
      h.merge!(s.options)
    end
    h
  )
end

#parametersObject

Return parameters array of [*arguments, options]



68
69
70
# File 'lib/clio/usage/interface.rb', line 68

def parameters
  arguments + [options]
end

#to_aObject



93
94
95
96
# File 'lib/clio/usage/interface.rb', line 93

def to_a
  #parse unless parsed?
  @signatures.collect{ |s| s.to_a }
end

#valid?Boolean

Were the commandline arguments valid? This simply checks to see if there were any parse errors.

Returns:

  • (Boolean)


82
83
84
85
# File 'lib/clio/usage/interface.rb', line 82

def valid?
  #parse unless @parsed
  errors.empty?
end