Class: Ukiryu::Models::Arguments
- Inherits:
-
Lutaml::Model::Serializable
- Object
- Lutaml::Model::Serializable
- Ukiryu::Models::Arguments
- 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
-
.determine_argument_type(key, value, command_definition) ⇒ Symbol
Determine the type of an argument.
-
.from_params(params, command_definition = nil) ⇒ Arguments
Create Arguments from a hash of parameters.
-
.stringify_value(value) ⇒ String
Convert a value to string for serialization.
Class Method Details
.determine_argument_type(key, value, command_definition) ⇒ Symbol
Determine the type of an argument
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.&.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
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) = [] 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 << 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. = unless .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
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 |