Class: Ukiryu::Models::Arguments

Inherits:
Lutaml::Model::Serializable
  • Object
show all
Defined in:
lib/ukiryu/models/arguments.rb

Overview

Structured command arguments

Contains the structured arguments passed to a command execution. Each argument has a name, value, and type.

Class Method Summary collapse

Class Method Details

.determine_argument_type(key, value, command_definition) ⇒ Symbol

Determine the type of an argument

Parameters:

  • key (Symbol)

    the argument key

  • value (Object)

    the argument value

  • command_definition (CommandDefinition)

    the command definition

Returns:

  • (Symbol)

    :option, :flag, or :positional



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
# File 'lib/ukiryu/models/arguments.rb', line 62

def self.determine_argument_type(key, value, command_definition)
  # Check command definition if available
  if command_definition
    flag_def = command_definition.flags&.find { |f| f.name.to_sym == key }
    return :flag if flag_def

    option_def = command_definition.options&.find { |o| o.name.to_sym == key }
    return :option if option_def

    arg_def = command_definition.arguments&.find { |a| a.name.to_sym == key }
    return :positional if arg_def
  end

  # Fall back to heuristics
  if value.is_a?(TrueClass) || value.is_a?(FalseClass)
    :flag
  elsif value.is_a?(Array)
    # Arrays are typically options (like inputs)
    :option
  else
    :option
  end
end

.from_params(params, command_definition = nil) ⇒ Arguments

Create Arguments from a hash of parameters

Parameters:

  • params (Hash)

    the parameters hash

  • command_definition (CommandDefinition) (defaults to: nil)

    the command definition for context

Returns:



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# File 'lib/ukiryu/models/arguments.rb', line 25

def self.from_params(params, command_definition = nil)
  options_arr = []
  flags_arr = []
  positional_arr = []

  params.each do |key, value|
    # Determine argument type based on command definition or heuristics
    arg_type = determine_argument_type(key, value, command_definition)

    case arg_type
    when :flag
      # Boolean flags
      flags_arr << key.to_s if value
    when :option
      # Named options with values
      options_arr << Argument.new(name: key.to_s, value: stringify_value(value), type: 'option')
    when :positional
      # Positional arguments
      positional_arr << Argument.new(name: key.to_s, value: stringify_value(value), type: 'positional')
    end
  end

  # Set arrays directly to ensure proper serialization
  args = new
  args.options = options_arr unless options_arr.empty?
  args.flags = flags_arr unless flags_arr.empty?
  args.positional = positional_arr unless positional_arr.empty?

  args
end

.stringify_value(value) ⇒ String

Convert a value to string for serialization

Parameters:

  • value (Object)

    the value

Returns:

  • (String)

    string representation



90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/ukiryu/models/arguments.rb', line 90

def self.stringify_value(value)
  case value
  when Array
    value.map(&:to_s).join(', ')
  when Symbol, Integer, Float, TrueClass, FalseClass
    value.to_s
  when NilClass
    ''
  else
    value.to_s
  end
end