Class: Command

Inherits:
Gloo::Core::Obj
  • Object
show all
Defined in:
lib/command.rb

Overview

Author

Eric Crane (eric.crane@mac.com)

Copyright

Copyright © 2026 Eric Crane. All rights reserved.

A CLI command.

Constant Summary collapse

KEYWORD =
'command'.freeze
KEYWORD_SHORT =
'command'.freeze
DESCRIPTION =
'description'.freeze
ACTION =
'action'.freeze
DYNAMIC =
'dynamic'.freeze
NODES =
'nodes'.freeze
CONTEXT =
'context'.freeze
OPTIONS =
'options'.freeze
OPTIONS_KEY =
'options_key'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.messagesObject

Get a list of message names that this object receives.



152
153
154
# File 'lib/command.rb', line 152

def self.messages
  return super + [ 'register' ]
end

.short_typenameObject

The short name of the object type.



30
31
32
# File 'lib/command.rb', line 30

def self.short_typename
  return KEYWORD_SHORT
end

.typenameObject

The name of the object type.



23
24
25
# File 'lib/command.rb', line 23

def self.typename
  return KEYWORD
end

Instance Method Details

#add_children_on_create?Boolean

Does this object have children to add when an object is created in interactive mode? This does not apply during obj load, etc.

Returns:

  • (Boolean)


128
129
130
# File 'lib/command.rb', line 128

def add_children_on_create?
  return true
end

#add_default_childrenObject

Add children to this object. This is used by containers to add children needed for default configurations.



137
138
139
140
141
142
# File 'lib/command.rb', line 137

def add_default_children
  fac = @engine.factory
  fac.create_string NAME, '', self
  fac.create_string DESCRIPTION, '', self
  fac.create_script ACTION, '', self
end

#contextObject



63
64
65
66
# File 'lib/command.rb', line 63

def context
  o = find_child CONTEXT
  return o ? o : nil
end

#descriptionObject

Get the description of the command.



37
38
39
40
41
42
# File 'lib/command.rb', line 37

def description
  o = find_child DESCRIPTION
  return '' unless o

  return o.value
end

#dynamic?Boolean

Is this a dynamic command? It is dynamic if there is a dynamic child. If so, it has contextual data.

Returns:

  • (Boolean)


49
50
51
52
53
# File 'lib/command.rb', line 49

def dynamic?
  o = find_child DYNAMIC
  return true if o
  return false
end

#dynamic_keyObject

Get the dynamic key for this command.



58
59
60
61
# File 'lib/command.rb', line 58

def dynamic_key
  o = find_child DYNAMIC
  return o ? o.value : nil
end

#get_child_nodesObject

Get the child nodes for this command.



180
181
182
183
184
185
186
# File 'lib/command.rb', line 180

def get_child_nodes
  data = nodes.children.map do |child|
    child.get_command_data
  end

  return data
end

#get_command_dataObject

Get the command data for this command.



191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/command.rb', line 191

def get_command_data
  if dynamic?
    return {
      name: name,
      description: description,
      dynamic: true,
      obj: pn,
      method: "cmd_obj_action_with_context",
      source: dynamic_key
    }
  elsif nodes
    return {
      name: name,
      description: description,
      children: get_child_nodes
    }
  else
    return {
      name: name,
      description: description,
      method: "cmd_obj_action",
      obj: pn
    }
  end
end

#msg_registerObject

Register the command with the shell.



159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/command.rb', line 159

def msg_register
  if @params&.token_count&.positive?
    pn = Gloo::Core::Pn.new( @engine, @params.first )
    shell = pn.resolve
    shell.add_command( self, get_command_data )

    # Are there options to add?
    if options_key
      shell.set_context( options_key, options )
    end
  end
end

#nodesObject

Get the child nodes for this command.



93
94
95
96
# File 'lib/command.rb', line 93

def nodes
  o = find_child NODES
  return o ? o : nil
end

#optionsObject

Get the options for this command.



79
80
81
82
83
84
85
86
87
88
# File 'lib/command.rb', line 79

def options
  arr = []
  o = find_child OPTIONS

  if o
    arr = o.children.map { |child| child.value }
  end

  return arr
end

#options_keyObject

Get the options key for this command.



71
72
73
74
# File 'lib/command.rb', line 71

def options_key
  o = find_child OPTIONS_KEY
  return o ? o.value : nil
end

#run_actionObject

Run the action script.



101
102
103
104
105
106
# File 'lib/command.rb', line 101

def run_action
  o = find_child ACTION
  return unless o

  Gloo::Exec::Dispatch.message( @engine, 'run', o )
end

#run_action_with_context(context) ⇒ Object



108
109
110
111
112
113
114
115
116
# File 'lib/command.rb', line 108

def run_action_with_context( context )
  o = find_child ACTION
  return unless o

  ctx = find_child CONTEXT
  ctx.set_value( context ) if ctx
  
  Gloo::Exec::Dispatch.message( @engine, 'run', o )
end