Class: ParticleCMD::Definition

Inherits:
Object
  • Object
show all
Defined in:
lib/particlecmd/definition.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) {|_self| ... } ⇒ Definition

Returns a new instance of Definition.

Yields:

  • (_self)

Yield Parameters:



4
5
6
7
8
9
10
11
# File 'lib/particlecmd/definition.rb', line 4

def initialize(name)
  @name        = name
  @positionals = []
  @flags       = []
  @options     = []
  @collecting  = false
  yield self
end

Instance Attribute Details

#collectingObject

Returns the value of attribute collecting.



2
3
4
# File 'lib/particlecmd/definition.rb', line 2

def collecting
  @collecting
end

#flagsObject

Returns the value of attribute flags.



2
3
4
# File 'lib/particlecmd/definition.rb', line 2

def flags
  @flags
end

#nameObject

Returns the value of attribute name.



2
3
4
# File 'lib/particlecmd/definition.rb', line 2

def name
  @name
end

#optionsObject

Returns the value of attribute options.



2
3
4
# File 'lib/particlecmd/definition.rb', line 2

def options
  @options
end

#positionalsObject

Returns the value of attribute positionals.



2
3
4
# File 'lib/particlecmd/definition.rb', line 2

def positionals
  @positionals
end

Class Method Details

.from_string(name, string) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/particlecmd/definition.rb', line 13

def self.from_string(name, string)
  d = new name do end
  string.split(' ').each do |word|
    if word == '...'
      d.collecting = true
    elsif word[0] == '-'
      i = word.match(/-+(.+?)(=(.+))?$/)
      if i[3]
        d.option i[1], argname: i[3]
      else
        d.flag i[1]
      end
    else
      d.positional word
    end
  end
  d
end

Instance Method Details

#collect_extraObject



45
46
47
# File 'lib/particlecmd/definition.rb', line 45

def collect_extra
  @collecting = true
end

#command_descriptionObject



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/particlecmd/definition.rb', line 91

def command_description
  s = ''

  nm = [
    (@positionals.map { |p| p[:name].length }.max or 0),
    (@flags.map { |f| f[:name].length + 2 }.max or 0),
    (@options.map { |o| o[:name].length + o[:argname].length + 3 }.max or 0)
  ].max

  @positionals.each do |p|
    s << (sprintf "    %*s  %s\n", nm, p[:name], p[:description])
  end
  s << "\n"
  @flags.each do |f|
    s << (sprintf "    %*s  %s\n", nm, "--#{f[:name]}", f[:description])
  end
  s << "\n"
  @options.each do |o|
    s << (sprintf "    %*s=%s  %s\n", nm-o[:argname].length-1, "--#{o[:name]}", o[:argname], o[:description])
  end
  s
end

#command_signature(include_name = true) ⇒ Object



75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/particlecmd/definition.rb', line 75

def command_signature(include_name = true)
  s = ''
  s << "#{@name} " if include_name
  @positionals.each do |p|
    s << "<#{p[:name]}> "
  end
  @flags.each do |f|
    s << "[--#{f[:name]}] "
  end
  @options.each do |o|
    s << "[--#{o[:name]}=#{o[:argname]}] "
  end
  s << "..." if @collecting
  s
end

#description(type, name, desc) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/particlecmd/definition.rb', line 32

def description(type, name, desc)
  case type
    when :positional
      @positionals
    when :flag
      @flags
    when :option
      @options
    else
      raise RuntimeError.new "Invalid argument type: #{type}"
  end.find { |i| i[:name] == name }[:description] = desc
end

#flag(name, **_opts) ⇒ Object



57
58
59
60
61
62
63
# File 'lib/particlecmd/definition.rb', line 57

def flag(name, **_opts)
  opts = {
    name: name,
    description: ''
  }.merge _opts
  @flags << opts
end

#help_messageObject



114
115
116
# File 'lib/particlecmd/definition.rb', line 114

def help_message
  'Usage: ' + command_signature + "\nArguments:\n" + command_description
end

#match(info) ⇒ Object



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'lib/particlecmd/definition.rb', line 118

def match(info)
  if @collecting && @positionals.empty? && @flags.empty? && @options.empty?
    res = ParticleCMD::Result.new
    res.extra = info.argv
    return res
  end

  if @collecting
    return nil if @positionals.length > info.positionals.length
  else
    return nil if @positionals.length != info.positionals.length
  end
  return nil if @flags.length < info.flags.length
  return nil if @options.length < info.options.length
  return nil unless (info.flags - @flags.map { |f| f[:name] }).empty?
  return nil unless (info.options.keys - @options.map { |o| o[:name] }).empty?
  
  res = ParticleCMD::Result.new

  for i in 0..(@positionals.length - 1) do
    res.positionals[@positionals[i][:name]] = info.positionals[i]
  end

  for i in 0..(@flags.length - 1) do
    n = @flags[i][:name]
    res.flags[n] = if info.flags.include? n
      true
    else
      false
    end
  end

  for i in 0..(@options.length - 1) do
    n = @options[i][:name]
    res.options[n] = if info.options.include? n
      info.options[n]
    else
      nil
    end
  end

  if @collecting && @positionals.length < info.positionals.length
    res.extra = info.positionals[@positionals.length, info.positionals.length]
  end

  res
end

#option(name, **_opts) ⇒ Object



65
66
67
68
69
70
71
72
73
# File 'lib/particlecmd/definition.rb', line 65

def option(name, **_opts)
  opts = {
    name: name,
    description: '',
    argname: 'VALUE',
    default: nil
  }.merge _opts
  @options << opts
end

#positional(name, **_opts) ⇒ Object



49
50
51
52
53
54
55
# File 'lib/particlecmd/definition.rb', line 49

def positional(name, **_opts)
  opts = {
    name: name,
    description: ''
  }.merge _opts
  @positionals << opts
end