# frozen_string_literal: true require 'yaml' def sort_yaml(input_yaml) # Parse the input YAML data = YAML.load(input_yaml) # Ensure data is an array of hashes unless data.is_a?(Array) && data.all? { |item| item.is_a?(Hash) } raise ArgumentError, 'Input YAML must be an array of hashes.' end # Sort items by 'opt_name' values sorted_data = data.sort_by do |item| (item[:opt_name] || item[:long_name] || item[:short_name]).to_s end # Sort keys in each item sorted_data.each do |item| item.replace(item.sort.to_h) end # Convert the sorted data back to YAML and write to stdout puts YAML.dump(sorted_data).gsub("\n-", "\n\n-") end # Read YAML from stdin input_yaml = $stdin.read # Call the function with input YAML sort_yaml(input_yaml)