lib/gli/app_support.rb in gli-2.1.0 vs lib/gli/app_support.rb in gli-2.2.0
- old
+ new
@@ -13,10 +13,11 @@
# Reset the GLI module internal data structures; mostly useful for testing
def reset # :nodoc:
switches.clear
flags.clear
@commands = nil
+ @commands_declaration_order = []
@version = nil
@config_file = nil
@use_openstruct = false
@prog_desc = nil
@error_block = false
@@ -25,12 +26,17 @@
@default_command = :help
@around_block = nil
clear_nexts
end
+ # Get an array of commands, ordered by when they were declared
+ def commands_declaration_order # :nodoc:
+ @commands_declaration_order
+ end
+
# Get the version string
- def version_string #:nodoc
+ def version_string #:nodoc:
@version
end
# Get the default command for the entire app
def get_default_command
@@ -117,11 +123,17 @@
def switches # :nodoc:
@switches ||= {}
end
def commands # :nodoc:
- @commands ||= { :help => GLI::Commands::Help.new(self), :_doc => GLI::Commands::Doc.new(self) }
+ if !@commands
+ @commands = { :help => GLI::Commands::Help.new(self), :_doc => GLI::Commands::Doc.new(self) }
+ @commands_declaration_order ||= []
+ @commands_declaration_order << @commands[:help]
+ @commands_declaration_order << @commands[:_doc]
+ end
+ @commands
end
def pre_block
@pre_block ||= Proc.new do
true
@@ -135,10 +147,18 @@
def around_blocks
@around_blocks || []
end
+ def help_sort_type
+ @help_sort_type || :alpha
+ end
+
+ def help_text_wrap_type
+ @help_text_wrap_type || :to_terminal
+ end
+
# Sets the default values for flags based on the configuration
def override_defaults_based_on_config(config)
override_default(flags,config)
override_default(switches,config)
@@ -165,20 +185,30 @@
private
def handle_exception(ex,command)
if regular_error_handling?(ex)
- stderr.puts error_message(ex)
+ output_error_message(ex)
if ex.kind_of?(OptionParser::ParseError) || ex.kind_of?(BadCommandLine)
- stderr.puts
commands[:help] and commands[:help].execute({},{},command.nil? ? [] : [command.name.to_s])
end
end
raise ex if ENV['GLI_DEBUG'] == 'true'
ex.extend(GLI::StandardException)
ex.exit_code
+ end
+
+ def output_error_message(ex)
+ stderr.puts error_message(ex) unless no_message_given?(ex)
+ if ex.kind_of?(OptionParser::ParseError) || ex.kind_of?(BadCommandLine)
+ stderr.puts unless no_message_given?(ex)
+ end
+ end
+
+ def no_message_given?(ex)
+ ex.message == ex.class.name
end
# Possibly returns a copy of the passed-in Hash as an instance of GLI::Option.
# By default, it will *not*. However by putting <tt>use_openstruct true</tt>
# in your CLI definition, it will