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
-
#coerce_types(options, conversions) ⇒ Object
Type coercion for CLI option values.
- #debug?(options) ⇒ Boolean
- #debug_log(message, options) ⇒ Object
- #format_pairs(hash) ⇒ Object
- #help?(options) ⇒ Boolean
- #quiet?(options) ⇒ Boolean
- #raise_cli_error(message, exit_code: 1) ⇒ Object
- #validate_required!(options, *required) ⇒ Object
- #verbose?(options) ⇒ Boolean
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(, conversions) conversions.each do |key, type| next if [key].nil? case type when :integer begin [key] = Integer([key]) rescue ArgumentError, TypeError raise ArgumentError, "Invalid value for --#{key.to_s.tr("_", "-")}: " \ "'#{[key]}' is not a valid integer" end when :float begin [key] = Float([key]) rescue ArgumentError, TypeError raise ArgumentError, "Invalid value for --#{key.to_s.tr("_", "-")}: " \ "'#{[key]}' is not a valid number" end end end end |
#debug?(options) ⇒ Boolean
22 23 24 |
# File 'lib/ace/support/cli/base.rb', line 22 def debug?() [:debug] == true end |
#debug_log(message, options) ⇒ Object
30 31 32 |
# File 'lib/ace/support/cli/base.rb', line 30 def debug_log(, ) warn "DEBUG: #{}" if debug?() 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
26 27 28 |
# File 'lib/ace/support/cli/base.rb', line 26 def help?() [:help] == true || [:h] == true end |
#quiet?(options) ⇒ Boolean
18 19 20 |
# File 'lib/ace/support/cli/base.rb', line 18 def quiet?() [: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(, exit_code: 1) raise Ace::Support::Cli::Error.new(, exit_code: exit_code) end |
#validate_required!(options, *required) ⇒ Object
38 39 40 41 42 43 |
# File 'lib/ace/support/cli/base.rb', line 38 def validate_required!(, *required) missing = required - .keys.select { |key| ![key].nil? } return if missing.empty? raise ArgumentError, "Missing required options: #{missing.join(", ")}" end |
#verbose?(options) ⇒ Boolean
14 15 16 |
# File 'lib/ace/support/cli/base.rb', line 14 def verbose?() [:verbose] == true end |