Module: Common

Defined in:
lib/get/commons/common.rb

Overview

Utility module

Class Method Summary collapse

Class Method Details

.error(message, &block) ⇒ Object

Print an error message and optionally run a block. Stdout becomes stderr, so every print is performed to stderr. This behavior is wanted as this method is called on errors.



27
28
29
# File 'lib/get/commons/common.rb', line 27

def self.error(message, &block)
  Common.print_then_do_and_exit("Error: #{message}", 1, block)
end

Print the given message, execute a block if given, and exit the program with the given exit status. If exit_status is not 0, the stdout is redirected to stderr.



47
48
49
50
51
52
53
# File 'lib/get/commons/common.rb', line 47

def self.print_then_do_and_exit(message, exit_code = 0, action = proc {})
  $stdout = $stderr unless exit_code.zero?

  puts message
  action.call if action.respond_to?('call')
  exit(exit_code)
end

.with_subcommand_exception_handling(parser) ⇒ Object

Subcommand exception handling for Optimist. Generally subcommands do not have a version to print.



33
34
35
36
37
38
39
40
41
42
# File 'lib/get/commons/common.rb', line 33

def self.with_subcommand_exception_handling(parser)
  yield
rescue Optimist::CommandlineError => e
  parser.die(e.message, nil, e.error_code)
rescue Optimist::HelpNeeded
  parser.educate
  exit
rescue Optimist::VersionNeeded
  # Version is not needed in this command
end