module GLI::AppSupport

Internals for make App work

Public Class Methods

included(klass) click to toggle source
# File lib/gli/app_support.rb, line 128
def self.included(klass)
  @stderr = $stderr
end

Public Instance Methods

argument_handling_strategy() click to toggle source
# File lib/gli/app_support.rb, line 208
def argument_handling_strategy
  @argument_handling_strategy || :loose
end
around_blocks() click to toggle source
# File lib/gli/app_support.rb, line 161
def around_blocks
  @around_blocks || []
end
autocomplete() click to toggle source
# File lib/gli/app_support.rb, line 216
def autocomplete
  @autocomplete.nil? ? true : @autocomplete
end
context_description() click to toggle source
# File lib/gli/app_support.rb, line 9
def context_description
  "in global context"
end
exe_name() click to toggle source
# File lib/gli/app_support.rb, line 36
def exe_name
  File.basename($0)
end
get_default_command() click to toggle source

Get the default command for the entire app

# File lib/gli/app_support.rb, line 51
def get_default_command
  @default_command
end
help_sort_type() click to toggle source
# File lib/gli/app_support.rb, line 165
def help_sort_type
  @help_sort_type || :alpha
end
help_text_wrap_type() click to toggle source
# File lib/gli/app_support.rb, line 169
def help_text_wrap_type
  @help_text_wrap_type || :to_terminal
end
override_command_defaults(command_list,config) click to toggle source
# File lib/gli/app_support.rb, line 185
def override_command_defaults(command_list,config)
  command_list.each do |command_name,command|
    next if command_name == :initconfig || command.nil?
    command_config = (config['commands'] || {})[command_name] || {}

    if @subcommand_option_handling_strategy == :legacy
      override_default(command.topmost_ancestor.flags,command_config)
      override_default(command.topmost_ancestor.switches,command_config)
    else
      override_default(command.flags,command_config)
      override_default(command.switches,command_config)
    end

    override_command_defaults(command.commands,command_config)
  end
end
override_default(tokens,config) click to toggle source
# File lib/gli/app_support.rb, line 202
def override_default(tokens,config)
  tokens.each do |name,token|
    token.default_value=config[name] unless config[name].nil?
  end
end
override_defaults_based_on_config(config) click to toggle source

Sets the default values for flags based on the configuration

# File lib/gli/app_support.rb, line 178
def override_defaults_based_on_config(config)
  override_default(flags,config)
  override_default(switches,config)

  override_command_defaults(commands,config)
end
post_block() click to toggle source
# File lib/gli/app_support.rb, line 156
def post_block
  @post_block ||= Proc.new do
  end
end
pre_block() click to toggle source
# File lib/gli/app_support.rb, line 150
def pre_block
  @pre_block ||= Proc.new do
    true
  end
end
stderr() click to toggle source
# File lib/gli/app_support.rb, line 124
def stderr
  @stderr ||= STDERR
end
subcommand_option_handling_strategy() click to toggle source
# File lib/gli/app_support.rb, line 212
def subcommand_option_handling_strategy
  @subcommand_option_handling_strategy || :legacy
end
synopsis_format_type() click to toggle source
# File lib/gli/app_support.rb, line 173
def synopsis_format_type
  @synopsis_format_type || :full
end

Private Instance Methods

add_help_switch_if_needed(target) click to toggle source
# File lib/gli/app_support.rb, line 253
def add_help_switch_if_needed(target)
  help_switch_exists = target.switches.values.find { |switch| 
    switch.names_and_aliases.map(&:to_s).find { |an_alias| an_alias == 'help' } 
  }
  unless help_switch_exists
    target.desc 'Show this message'
    target.switch :help, :negatable => false
  end
end
call_command(parsing_result) click to toggle source
# File lib/gli/app_support.rb, line 290
def call_command(parsing_result)
  command        = parsing_result.command
  global_options = parsing_result.global_options
  options        = parsing_result.command_options
  arguments      = parsing_result.arguments.map { |arg| arg.dup } # unfreeze

  code = lambda { command.execute(global_options,options,arguments) }
  nested_arounds = unless command.skips_around
                     around_blocks.inject do |outer_around, inner_around|
                       lambda { |go,c,o,a, code1|
                         inner = lambda { inner_around.call(go,c,o,a, code1) }
                         outer_around.call(go,c,o,a, inner)
                       }
                     end
                   end

  if nested_arounds
    nested_arounds.call(global_options,command, options, arguments, code)
  else
    code.call
  end

  unless command.skips_post
    post_block.call(global_options,command,options,arguments)
  end
end
handle_exception(ex,command) click to toggle source
# File lib/gli/app_support.rb, line 222
def handle_exception(ex,command)
  if regular_error_handling?(ex)
    output_error_message(ex)
    if ex.kind_of?(OptionParser::ParseError) || ex.kind_of?(BadCommandLine) || ex.kind_of?(RequestHelp)
      if commands[:help]
        command_for_help = command.nil? ? [] : command.name_for_help
        commands[:help].execute({},{},command_for_help)
      end
    end
  elsif ENV['GLI_DEBUG'] == 'true'
    stderr.puts "Custom error handler exited false, skipping normal error handling"
  end

  raise ex if ENV['GLI_DEBUG'] == 'true' && !ex.kind_of?(RequestHelp)

  ex.extend(GLI::StandardException)
  ex.exit_code
end
no_message_given?(ex) click to toggle source
# File lib/gli/app_support.rb, line 248
def no_message_given?(ex)
  ex.message == ex.class.name

end
output_error_message(ex) click to toggle source
# File lib/gli/app_support.rb, line 241
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