lib/gli/app_support.rb in gli-2.0.0.rc3 vs lib/gli/app_support.rb in gli-2.0.0.rc4
- old
+ new
@@ -12,19 +12,20 @@
# Reset the GLI module internal data structures; mostly useful for testing
def reset # :nodoc:
switches.clear
flags.clear
- commands.clear
+ @commands = nil
@version = nil
@config_file = nil
@use_openstruct = false
@prog_desc = nil
@error_block = false
@pre_block = false
@post_block = false
@default_command = :help
+ @around_block = nil
clear_nexts
end
# Get the version string
def version_string #:nodoc
@@ -41,19 +42,18 @@
begin
override_defaults_based_on_config(parse_config)
add_help_switch_if_needed(switches)
- global_options,command,options,arguments = GLIOptionParser.new(commands,flags,switches,accepts).parse_options(args)
+ global_options,command,options,arguments = GLIOptionParser.new(commands,flags,switches,accepts,@default_command).parse_options(args)
copy_options_to_aliased_versions(global_options,command,options)
global_options = convert_to_openstruct_if_needed(global_options)
options = convert_to_openstruct_if_needed(options)
if proceed?(global_options,command,options,arguments)
- command ||= commands[:help]
call_command(command,global_options,options,arguments)
end
0
rescue Exception => ex
handle_exception(ex,command)
@@ -91,10 +91,11 @@
def clear_nexts # :nodoc:
super
@skips_post = false
@skips_pre = false
+ @skips_around = false
end
def stderr
@stderr ||= STDERR
end
@@ -124,10 +125,16 @@
def post_block
@post_block ||= Proc.new do
end
end
+ def around_block
+ @around_block ||= Proc.new do |global,command,options,args,code|
+ code.call
+ end
+ 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)
@@ -214,10 +221,15 @@
"error: #{ex.message}"
end
def call_command(command,global_options,options,arguments)
arguments = arguments.map { |arg| arg.dup } # unfreeze
- command.execute(global_options,options,arguments)
+ code = lambda { command.execute(global_options,options,arguments) }
+ if command.skips_around
+ code.call
+ else
+ around_block.call(global_options,command,options,arguments,code)
+ end
unless command.skips_post
post_block.call(global_options,command,options,arguments)
end
end