Class: Bashly::Script::Flag

Inherits:
Base
  • Object
show all
Includes:
Introspection::Validate, Introspection::Visibility
Defined in:
lib/bashly/script/flag.rb

Instance Attribute Summary

Attributes inherited from Base

#options

Attributes included from Renderable

#render_options

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Introspection::Validate

#validate, #validate?

Methods included from Introspection::Visibility

#visibility

Methods inherited from Base

#help, #initialize, #method_missing, #optional, #respond_to_missing?, #summary

Methods included from Renderable

#load_user_file, #render, #strings, #user_file_exist?, #user_file_path, #user_string, #view_marker

Constructor Details

This class inherits a constructor from Bashly::Script::Base

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Bashly::Script::Base

Class Method Details

.option_keysObject



10
11
12
13
14
15
# File 'lib/bashly/script/flag.rb', line 10

def option_keys
  @option_keys ||= %i[
    alias allowed arg completions conflicts default help long needs
    negatable private repeatable required short unique validate
  ]
end

Instance Method Details

#aliasesObject



24
25
26
# File 'lib/bashly/script/flag.rb', line 24

def aliases
  [long, negated_long, short].compact + alt
end

#altObject



18
19
20
21
22
# File 'lib/bashly/script/flag.rb', line 18

def alt
  return [] unless options['alias']

  options['alias'].is_a?(String) ? [options['alias']] : options['alias']
end

#completion_dynamicObject



50
51
52
# File 'lib/bashly/script/flag.rb', line 50

def completion_dynamic
  completions&.fetch('dynamic', []) || []
end

#completion_optionsObject



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

def completion_options
  completions&.fetch('options', []) || []
end

#completion_staticObject



46
47
48
# File 'lib/bashly/script/flag.rb', line 46

def completion_static
  completions&.fetch('static', []) || []
end

#default_stringObject



36
37
38
39
40
41
42
43
44
# File 'lib/bashly/script/flag.rb', line 36

def default_string
  if default.is_a?(Array)
    Shellwords.shelljoin default
  elsif default.is_a?(String) && repeatable
    Shellwords.shellescape default
  else
    default
  end
end

#nameObject



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

def name
  long || short
end

#negated_longObject



32
33
34
# File 'lib/bashly/script/flag.rb', line 32

def negated_long
  "--no-#{long_without_prefix}" if negatable && long
end

#positive_aliasesObject



28
29
30
# File 'lib/bashly/script/flag.rb', line 28

def positive_aliases
  primary_aliases + alt
end

#usage_string(extended: false) ⇒ Object



62
63
64
65
66
67
68
# File 'lib/bashly/script/flag.rb', line 62

def usage_string(extended: false)
  result = [usage_aliases.join(', ')]
  result << arg.upcase if arg
  result << strings[:required] if required && extended
  result << strings[:repeatable] if repeatable && extended
  result.join ' '
end