Skip to content

Commit

Permalink
Apply help command sort to options, too
Browse files Browse the repository at this point in the history
Closes #114
  • Loading branch information
davetron5000 committed Nov 21, 2012
1 parent 9187ca0 commit 1b1dd50
Show file tree
Hide file tree
Showing 6 changed files with 12 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,4 @@ gli.wiki
Gemfile.lock
results.html
.rbx
.DS_Store
4 changes: 2 additions & 2 deletions features/todo.feature
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,10 @@ Feature: The todo app has a nice user interface
GLOBAL OPTIONS
--flag=arg - (default: none)
--help - Show this message
--[no-]otherswitch -
--[no-]switch -
--[no-]otherswitch -
--version -
--help - Show this message
COMMANDS
help - Shows a list of commands or help for one command
Expand Down
6 changes: 3 additions & 3 deletions lib/gli/app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -224,10 +224,10 @@ def exit_code; 64; end
raise exception
end

# Control how help commands are sorted. By default, the commands are sorted alphabetically.
# Control how commands and options are sorted in help output. By default, they are sorted alphabetically.
#
# sort_type:: How you want help commands sorted:
# +:manually+:: help commands are ordered in the order declared.
# sort_type:: How you want help commands/options sorted:
# +:manually+:: help commands/options are ordered in the order declared.
# +:alpha+:: sort alphabetically (default)
def sort_help(sort_type)
@help_sort_type = sort_type
Expand Down
2 changes: 1 addition & 1 deletion lib/gli/commands/help_modules/command_help_format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def format
command_wrapper = @wrapper_class.new(Terminal.instance.size[0],4 + @command.name.to_s.size + 3)
wrapper = @wrapper_class.new(Terminal.instance.size[0],4)
flags_and_switches = Hash[@command.topmost_ancestor.flags.merge(@command.topmost_ancestor.switches).select { |_,option| option.associated_command == @command }]
options_description = OptionsFormatter.new(flags_and_switches,@wrapper_class).format
options_description = OptionsFormatter.new(flags_and_switches,@sorter,@wrapper_class).format
commands_description = format_subcommands(@command)

synopses = []
Expand Down
2 changes: 1 addition & 1 deletion lib/gli/commands/help_modules/global_help_format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def format
command_formatter.output(stringio)
commands = stringio.string

global_option_descriptions = OptionsFormatter.new(global_flags_and_switches,@wrapper_class).format
global_option_descriptions = OptionsFormatter.new(global_flags_and_switches,@sorter,@wrapper_class).format

GLOBAL_HELP.result(binding)
end
Expand Down
8 changes: 4 additions & 4 deletions lib/gli/commands/help_modules/options_formatter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,15 @@ module GLI
module Commands
module HelpModules
class OptionsFormatter
def initialize(flags_and_switches,wrapper_class)
def initialize(flags_and_switches,sorter,wrapper_class)
@flags_and_switches = flags_and_switches
@sorter = sorter
@wrapper_class = wrapper_class
end

def format
list_formatter = ListFormatter.new(@flags_and_switches.values.sort { |a,b|
a.name.to_s <=> b.name.to_s
}.map { |option|
flags_and_switches = @sorter.call(@flags_and_switches.values)
list_formatter = ListFormatter.new(flags_and_switches.map { |option|
if option.respond_to? :argument_name
[option_names_for_help_string(option,option.argument_name),description_with_default(option)]
else
Expand Down

0 comments on commit 1b1dd50

Please sign in to comment.