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
NAME =
'name'.freeze
DESCRIPTION =
'description'.freeze
ACTION =
'action'.freeze
DYNAMIC =
'dynamic'.freeze
NODES =
'nodes'.freeze
CONTEXT =
'context'.freeze
OPTIONS =
'options'.freeze
OPTIONS_KEY =
'options_key'.freeze
ON_ERROR =
'on_error'.freeze

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.messagesObject

Get a list of message names that this object receives.



169
170
171
# File 'lib/command.rb', line 169

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

.short_typenameObject

The short name of the object type.



32
33
34
# File 'lib/command.rb', line 32

def self.short_typename
  return KEYWORD_SHORT
end

.typenameObject

The name of the object type.



25
26
27
# File 'lib/command.rb', line 25

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)


145
146
147
# File 'lib/command.rb', line 145

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.



154
155
156
157
158
159
# File 'lib/command.rb', line 154

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

#contextObject



65
66
67
68
# File 'lib/command.rb', line 65

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

#descriptionObject

Get the description of the command.



39
40
41
42
43
44
# File 'lib/command.rb', line 39

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)


51
52
53
54
55
# File 'lib/command.rb', line 51

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

#dynamic_keyObject

Get the dynamic key for this command.



60
61
62
63
# File 'lib/command.rb', line 60

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

#get_child_nodesObject

Get the child nodes for this command.



197
198
199
200
201
202
203
# File 'lib/command.rb', line 197

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.



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
# File 'lib/command.rb', line 208

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.



176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/command.rb', line 176

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.



95
96
97
98
# File 'lib/command.rb', line 95

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

#optionsObject

Get the options for this command.



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

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.



73
74
75
76
# File 'lib/command.rb', line 73

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

#run_actionObject

Run the action script.



103
104
105
106
107
108
# File 'lib/command.rb', line 103

def run_action
  o = find_child ACTION
  return unless o

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

#run_action_with_context(context) ⇒ Object

Run the action script with context.



113
114
115
116
117
118
119
120
121
# File 'lib/command.rb', line 113

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

#run_on_errorObject

Run the on_error script if one exists. Returns true if the script was found and run, false otherwise.



127
128
129
130
131
132
133
# File 'lib/command.rb', line 127

def run_on_error
  o = find_child ON_ERROR
  return false unless o

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