Class: Bashly::Script::Base

Inherits:
Object
  • Object
show all
Includes:
Renderable
Defined in:
lib/bashly/script/base.rb

Direct Known Subclasses

Argument, Command, EnvironmentVariable, Flag

Constant Summary collapse

OPTION_KEYS =
%i[
  allowed
  arg
  catch_all
  completions
  conflicts
  default
  dependencies
  description
  environment_variables
  examples
  extensible
  filters
  flags
  footer
  group
  help
  long
  name
  parent_name
  private
  repeatable
  required
  short
  validate
  version
]

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Renderable

#render, #strings

Constructor Details

#initialize(options) ⇒ Base

Returns a new instance of Base.

Raises:



36
37
38
39
# File 'lib/bashly/script/base.rb', line 36

def initialize(options)
  raise Error, "Invalid options provided" unless options.respond_to? :keys
  @options = options
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *arguments, &block) ⇒ Object



53
54
55
56
# File 'lib/bashly/script/base.rb', line 53

def method_missing(method_name, *arguments, &block)
  key = method_name.to_s
  respond_to?(method_name) ? options[key] : super
end

Instance Attribute Details

#optionsObject (readonly)

Returns the value of attribute options.



6
7
8
# File 'lib/bashly/script/base.rb', line 6

def options
  @options
end

Instance Method Details

#helpObject



49
50
51
# File 'lib/bashly/script/base.rb', line 49

def help
  options['help'] ||= ''
end

#optionalObject



41
42
43
# File 'lib/bashly/script/base.rb', line 41

def optional
  !required
end

#respond_to_missing?(method_name, include_private = false) ⇒ Boolean

Returns:

  • (Boolean)


58
59
60
# File 'lib/bashly/script/base.rb', line 58

def respond_to_missing?(method_name, include_private = false)
  OPTION_KEYS.include?(method_name) || super
end

#summaryObject



45
46
47
# File 'lib/bashly/script/base.rb', line 45

def summary
  help.empty? ? "" : help.split("\n").first
end