Sha256: 540ba47fb3b0b0d7c6c931b234af83eb8246b2dc02f4dfd366a03aa3de5a936a
Contents?: true
Size: 779 Bytes
Versions: 12
Compression:
Stored size: 779 Bytes
Contents
# 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)
Version data entries
12 entries across 12 versions & 1 rubygems