Class: Bashly::Script::Command

Instance Attribute Summary collapse

Attributes inherited from Base

#options

Attributes included from Renderable

#render_options

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Introspection::Visibility

#visibility

Methods included from Introspection::Variables

#variables

Methods included from Introspection::Flags

#default_flags, #fixed_flags?, #flags, #global_flags?, #needy_flags, #public_flags, #required_flags, #short_flag_exist?, #visible_flags, #whitelisted_flags

Methods included from Introspection::Examples

#examples

Methods included from Introspection::EnvironmentVariables

#default_environment_variables, #environment_variables, #public_environment_variables, #required_environment_variables, #validated_environment_variables, #visible_environment_variables, #whitelisted_environment_variables

Methods included from Introspection::Dependencies

#dependencies

Methods included from Introspection::Commands

#catch_all_used_anywhere?, #command_aliases, #command_help_data, #command_names, #commands, #completion_aliases, #deep_commands, #default_command, #grouped_commands, #public_command_aliases, #public_commands, #visible_command_aliases, #visible_commands

Methods included from Introspection::Arguments

#args, #default_args, #repeatable_arg_exist?, #required_args, #usage_string_args, #whitelisted_args

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

Instance Attribute Details

#parent_commandObject

Returns the value of attribute parent_command.



25
26
27
# File 'lib/bashly/script/command.rb', line 25

def parent_command
  @parent_command
end

#parentsObject

Returns an array of all parents. For example, the command "docker container run" will have [docker, container] as its parents



109
110
111
# File 'lib/bashly/script/command.rb', line 109

def parents
  @parents ||= []
end

Class Method Details

.option_keysObject



14
15
16
17
18
19
20
21
22
# File 'lib/bashly/script/command.rb', line 14

def option_keys
  @option_keys ||= %i[
    alias argfile args catch_all commands
    default dependencies environment_variables examples
    extensible expose filename filters flags
    footer function group help help_header_override name
    private variables version
  ]
end

Instance Method Details

#action_nameObject

Returns the name to be used as an action.

  • If it is the root command, the action is "root"
  • Else, it is all the parents, except the first one (root) joined by space. For example, for a command like "docker container run" the action name is "container run".


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

def action_name
  parents.any? ? (parents[1..] + [name]).join(' ') : 'root'
end

#aliasesObject

Returns all the possible aliases for this command



38
39
40
# File 'lib/bashly/script/command.rb', line 38

def aliases
  [name] + alt
end

#altObject

Returns an array of alternative aliases if any



43
44
45
46
47
# File 'lib/bashly/script/command.rb', line 43

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

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

#base_usage_patternObject

Returns a base usage string that considers whether this command is the default, and if it has any parents. Used internally by usage_string.



146
147
148
149
# File 'lib/bashly/script/command.rb', line 146

def base_usage_pattern
  usage_pattern = default ? "[#{name}]" : name
  parents.any? ? (parents + [usage_pattern]).join(' ') : usage_pattern
end

#caption_stringObject

Returns a string suitable to be a headline



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

def caption_string
  help.empty? ? full_name : "#{full_name} - #{summary}"
end

#catch_allObject

Returns an object representing the catch_all configuration



55
56
57
# File 'lib/bashly/script/command.rb', line 55

def catch_all
  @catch_all ||= CatchAll.from_config options['catch_all']
end

#filenameObject

Returns the filename that is expected to hold the user code for this command



61
62
63
# File 'lib/bashly/script/command.rb', line 61

def filename
  options['filename'] || implicit_filename
end

#full_nameObject

Returns the name of the command, including its parent name (in case this is a subcommand)



72
73
74
# File 'lib/bashly/script/command.rb', line 72

def full_name
  parents.any? ? (parents + [name]).join(' ') : name
end

#function_nameObject

Returns a unique name, suitable to be used in a bash function



66
67
68
# File 'lib/bashly/script/command.rb', line 66

def function_name
  options['function'] || full_name.to_underscore
end

#group_stringObject

Returns the string for the group caption



77
78
79
80
81
82
83
# File 'lib/bashly/script/command.rb', line 77

def group_string
  if group
    strings[:group] % { group: group }
  else
    strings[:commands]
  end
end

#has_unique_args_or_flags?Boolean

Returns true if this command, or any subcommand (deep) as any arg or flag with arg that is defined as unique

Returns:

  • (Boolean)


87
88
89
90
91
92
93
# File 'lib/bashly/script/command.rb', line 87

def has_unique_args_or_flags?
  deep_commands(include_self: true).each do |command|
    return true if command.args.any?(&:unique) ||
      command.flags.any?(&:unique)
  end
  false
end

#modeObject

Returns a mode identifier



96
97
98
99
100
101
102
103
104
105
# File 'lib/bashly/script/command.rb', line 96

def mode
  @mode ||= if global_flags?    then :global_flags
  elsif commands.any?           then :commands
  elsif args.any? && flags.any? then :args_and_flags
  elsif args.any?               then :args
  elsif flags.any?              then :flags
  else
    :empty
  end
end

#root_command?Boolean

Returns true if this is the root command (no parents)

Returns:

  • (Boolean)


114
115
116
# File 'lib/bashly/script/command.rb', line 114

def root_command?
  parents.empty?
end

#summary_stringObject

Returns the summary string



119
120
121
122
123
124
125
# File 'lib/bashly/script/command.rb', line 119

def summary_string
  if default
    strings[:default_command_summary] % { summary: summary }
  else
    summary
  end
end

#usage_stringObject

Returns a constructed string suitable for Usage pattern



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# File 'lib/bashly/script/command.rb', line 128

def usage_string
  result = [base_usage_pattern]
  command_string = default_command&.default == 'force' ? '[COMMAND]' : 'COMMAND'

  result.push case mode
  when :global_flags    then ['[OPTIONS]', command_string]
  when :commands        then [command_string]
  when :args_and_flags  then usage_string_args + ['[OPTIONS]']
  when :args            then usage_string_args
  when :flags           then ['[OPTIONS]']
  end

  result.push catch_all.usage_string if catch_all.enabled? && commands.empty?
  result.compact.join ' '
end

#user_libObject

Returns an array of files to include as is inside the script This is meant to provide the user with the ability to add custom functions



154
155
156
157
158
159
160
161
162
# File 'lib/bashly/script/command.rb', line 154

def user_lib
  @user_lib ||= begin
    result = Settings.all_lib_dirs.map do |dir|
      Dir["#{dir}/**/*.#{Settings.partials_extension}"]
    end

    result.flatten
  end
end

#validatablesObject

Returns a mixed array of Argument and Flag objects that have validations



165
166
167
# File 'lib/bashly/script/command.rb', line 165

def validatables
  @validatables ||= args.select(&:validate?) + flags.select(&:validate?)
end