Module: Legate::CLI::OutputHelper

Included in:
AgentCommands, ToolCommands
Defined in:
lib/legate/cli/output_helper.rb

Overview

Helper module for CLI output control. Provides methods to conditionally output status messages and format results based on –quiet and –json flags.

Instance Method Summary collapse

Instance Method Details

#output_error(error, metadata: {}) ⇒ Object

Output error (JSON format in –json mode, otherwise text to stderr)

Parameters:

  • error (Exception, String)

    The error to output

  • metadata (Hash) (defaults to: {})

    Additional metadata to include



38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/legate/cli/output_helper.rb', line 38

def output_error(error, metadata: {})
  if json_mode?
    error_data = {
      status: 'error',
      error_class: error.is_a?(Exception) ? error.class.name : 'Error',
      error_message: error.is_a?(Exception) ? error.message : error.to_s
    }
    error_data.merge!() unless .empty?
    puts JSON.generate(error_data)
  else
    message = error.is_a?(Exception) ? "#{error.class} - #{error.message}" : error.to_s
    say message, :red
  end
end

#output_result(data, metadata: {}, format_method: nil) ⇒ Object

Output final result (JSON format in –json mode, otherwise uses format_method or default)

Parameters:

  • data (Object)

    The result data to output

  • metadata (Hash) (defaults to: {})

    Additional metadata to include (e.g., session_id)

  • format_method (Symbol) (defaults to: nil)

    Method name to call for human-friendly formatting



25
26
27
28
29
30
31
32
33
# File 'lib/legate/cli/output_helper.rb', line 25

def output_result(data, metadata: {}, format_method: nil)
  if json_mode?
    output_json(data, )
  elsif format_method && respond_to?(format_method, true)
    send(format_method, data)
  else
    say data.inspect
  end
end

#status_message(message, color = nil) ⇒ Object

Write status/progress message (suppressed in quiet or json mode)

Parameters:

  • message (String)

    The status message to display

  • color (Symbol, nil) (defaults to: nil)

    Optional color for Thor’s say method



15
16
17
18
19
# File 'lib/legate/cli/output_helper.rb', line 15

def status_message(message, color = nil)
  return if quiet_mode?

  say message, color
end