From 84cd7b4d8c836382c60935e901a7b321bc37a741 Mon Sep 17 00:00:00 2001 From: Alejandro Huertas Herrero Date: Fri, 26 Jun 2020 14:09:51 +0200 Subject: [PATCH] F #4943: alphabetical order cmds, opts and format (#42) (cherry picked from commit 32d8b9b14c1583b5455a38b4cb90c8d5b9328485) --- src/cli/command_parser.rb | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/src/cli/command_parser.rb b/src/cli/command_parser.rb index 4c8c799ed54..5d7d7328b08 100644 --- a/src/cli/command_parser.rb +++ b/src/cli/command_parser.rb @@ -295,6 +295,11 @@ def command(name, desc, *args_format, &block) cmd[:arity]+=1 unless args.include?(nil) cmd[:args_format] << args elsif args.instance_of?(Hash) && args[:options] + if args[:options].is_a? Array + args[:options].flatten! + args[:options] = args[:options].sort_by {|o| o[:name] } + end + cmd[:options] << args[:options] else cmd[:arity]+=1 @@ -679,7 +684,9 @@ def print_command_help(name) def print_options puts "## OPTIONS" - shown_opts = Array.new + shown_opts = [] + options = [] + @command_list.each do |key| value = @commands[key] @@ -688,14 +695,17 @@ def print_options next else shown_opts << o[:name] - - print_option(o) + options << o end end end - @available_options.each do |o| - print_option o + options << @available_options + options.flatten! + options = options.sort_by {|o| o[:name] } + + options.each do |o| + print_option(o) end end @@ -724,6 +734,8 @@ def print_commands else puts "## COMMANDS" + @command_list.sort! if @command_list + @command_list.each do |key| value = @commands[key] printf cmd_format5, "* #{key} " @@ -766,6 +778,9 @@ def print_formatters cmd_format5 = "#{' '*3}%s" cmd_format10 = "#{' '*8}%s" + + @formats = @formats.sort_by {|key, _| key } if @formats + @formats.each{ |key,value| printf cmd_format5, "* #{key}" puts