Class: Bashly::Script::CatchAll

Inherits:
Object
  • Object
show all
Defined in:
lib/bashly/script/catch_all.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(label: nil, help: nil, required: false, catch_help: false, enabled: true) ⇒ CatchAll

Returns a new instance of CatchAll.



25
26
27
28
29
30
31
# File 'lib/bashly/script/catch_all.rb', line 25

def initialize(label: nil, help: nil, required: false, catch_help: false, enabled: true)
  @label = label
  @help = help
  @required = required
  @enabled = enabled
  @catch_help = catch_help
end

Class Method Details

.from_config(config) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/bashly/script/catch_all.rb', line 9

def from_config(config)
  options = case config
  when nil
    { enabled: false }
  when String
    { label: config }
  when Hash
    config.transform_keys(&:to_sym).slice(*option_keys)
  else
    {}
  end

  new(**options)
end

.option_keysObject



5
6
7
# File 'lib/bashly/script/catch_all.rb', line 5

def option_keys
  @option_keys ||= %i[label help required catch_help]
end

Instance Method Details

#catch_help?Boolean

Returns:

  • (Boolean)


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

def catch_help?
  @catch_help
end

#enabled?Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/bashly/script/catch_all.rb', line 33

def enabled?
  @enabled
end

#helpObject



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

def help
  enabled? ? @help : nil
end

#labelObject



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

def label
  enabled? ? "#{@label&.upcase}..." : nil
end

#required?Boolean

Returns:

  • (Boolean)


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

def required?
  @required
end

#usage_stringObject



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

def usage_string
  return nil unless enabled?

  required? ? "[--] #{label}" : "[--] [#{label}]"
end