Module: Ace::Support::Cli::Base

Defined in:
lib/ace/support/cli/base.rb

Overview

Shared CLI helper methods and option constants used across ACE commands.

Constant Summary collapse

STANDARD_OPTIONS =
%i[quiet verbose debug].freeze
RESERVED_FLAGS =
%i[h v q d o].freeze

Instance Method Summary collapse

Instance Method Details

#coerce_types(options, conversions) ⇒ Object

Type coercion for CLI option values.



50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ace/support/cli/base.rb', line 50

def coerce_types(options, conversions)
  conversions.each do |key, type|
    next if options[key].nil?

    case type
    when :integer
      begin
        options[key] = Integer(options[key])
      rescue ArgumentError, TypeError
        raise ArgumentError, "Invalid value for --#{key.to_s.tr("_", "-")}: " \
                             "'#{options[key]}' is not a valid integer"
      end
    when :float
      begin
        options[key] = Float(options[key])
      rescue ArgumentError, TypeError
        raise ArgumentError, "Invalid value for --#{key.to_s.tr("_", "-")}: " \
                             "'#{options[key]}' is not a valid number"
      end
    end
  end
  options
end

#debug?(options) ⇒ Boolean

Returns:

  • (Boolean)


22
23
24
# File 'lib/ace/support/cli/base.rb', line 22

def debug?(options)
  options[:debug] == true
end

#debug_log(message, options) ⇒ Object



30
31
32
# File 'lib/ace/support/cli/base.rb', line 30

def debug_log(message, options)
  warn "DEBUG: #{message}" if debug?(options)
end

#format_pairs(hash) ⇒ Object



45
46
47
# File 'lib/ace/support/cli/base.rb', line 45

def format_pairs(hash)
  hash.map { |key, value| "#{key}=#{value}" }.join(" ")
end

#help?(options) ⇒ Boolean

Returns:

  • (Boolean)


26
27
28
# File 'lib/ace/support/cli/base.rb', line 26

def help?(options)
  options[:help] == true || options[:h] == true
end

#quiet?(options) ⇒ Boolean

Returns:

  • (Boolean)


18
19
20
# File 'lib/ace/support/cli/base.rb', line 18

def quiet?(options)
  options[:quiet] == true
end

#raise_cli_error(message, exit_code: 1) ⇒ Object



34
35
36
# File 'lib/ace/support/cli/base.rb', line 34

def raise_cli_error(message, exit_code: 1)
  raise Ace::Support::Cli::Error.new(message, exit_code: exit_code)
end

#validate_required!(options, *required) ⇒ Object

Raises:

  • (ArgumentError)


38
39
40
41
42
43
# File 'lib/ace/support/cli/base.rb', line 38

def validate_required!(options, *required)
  missing = required - options.keys.select { |key| !options[key].nil? }
  return if missing.empty?

  raise ArgumentError, "Missing required options: #{missing.join(", ")}"
end

#verbose?(options) ⇒ Boolean

Returns:

  • (Boolean)


14
15
16
# File 'lib/ace/support/cli/base.rb', line 14

def verbose?(options)
  options[:verbose] == true
end