class Bashly::Script::Command

Attributes

parent_command[RW]
parents[W]

Public Class Methods

option_keys() click to toggle source
# File lib/bashly/script/command.rb, line 15
def option_keys
  @option_keys ||= %i[
    alias args catch_all commands completions
    default dependencies environment_variables examples
    extensible expose filename filters flags
    footer function group help name
    private variables version
  ]
end

Public Instance Methods

action_name() click to toggle source

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”.

# File lib/bashly/script/command.rb, line 34
def action_name
  parents.any? ? (parents[1..] + [name]).join(' ') : 'root'
end
aliases() click to toggle source

Returns all the possible aliases for this command

# File lib/bashly/script/command.rb, line 39
def aliases
  [name] + alt
end
alt() click to toggle source

Returns an array of alternative aliases if any

# File lib/bashly/script/command.rb, line 44
def alt
  return [] unless options['alias']

  options['alias'].is_a?(String) ? [options['alias']] : options['alias']
end
base_usage_pattern() click to toggle source

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

# File lib/bashly/script/command.rb, line 147
def base_usage_pattern
  usage_pattern = default ? "[#{name}]" : name
  parents.any? ? (parents + [usage_pattern]).join(' ') : usage_pattern
end
caption_string() click to toggle source

Returns a string suitable to be a headline

# File lib/bashly/script/command.rb, line 51
def caption_string
  help.empty? ? full_name : "#{full_name} - #{summary}"
end
catch_all() click to toggle source

Returns an object representing the catch_all configuration

# File lib/bashly/script/command.rb, line 56
def catch_all
  @catch_all ||= CatchAll.from_config options['catch_all']
end
filename() click to toggle source

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

# File lib/bashly/script/command.rb, line 62
def filename
  options['filename'] || implicit_filename
end
full_name() click to toggle source

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

# File lib/bashly/script/command.rb, line 73
def full_name
  parents.any? ? (parents + [name]).join(' ') : name
end
function_name() click to toggle source

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

# File lib/bashly/script/command.rb, line 67
def function_name
  options['function'] || full_name.to_underscore
end
group_string() click to toggle source

Returns the string for the group caption

# File lib/bashly/script/command.rb, line 78
def group_string
  if group
    strings[:group] % { group: group }
  else
    strings[:commands]
  end
end
has_unique_args_or_flags?() click to toggle source

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

# File lib/bashly/script/command.rb, line 88
def has_unique_args_or_flags?
  deep_commands(include_self: true).each do |command|
    return true if command.args.count(&:unique).positive? ||
      command.flags.count(&:unique).positive?
  end
  false
end
mode() click to toggle source

Returns a mode identifier

# File lib/bashly/script/command.rb, line 97
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
parents() click to toggle source

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

# File lib/bashly/script/command.rb, line 110
def parents
  @parents ||= []
end
root_command?() click to toggle source

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

# File lib/bashly/script/command.rb, line 115
def root_command?
  parents.empty?
end
summary_string() click to toggle source

Returns the summary string

# File lib/bashly/script/command.rb, line 120
def summary_string
  if default
    strings[:default_command_summary] % { summary: summary }
  else
    summary
  end
end
usage_string() click to toggle source

Returns a constructed string suitable for Usage pattern

# File lib/bashly/script/command.rb, line 129
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_lib() click to toggle source

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

# File lib/bashly/script/command.rb, line 155
def user_lib
  @user_lib ||= Dir["#{Settings.full_lib_dir}/**/*.#{Settings.partials_extension}"]
end
validatables() click to toggle source

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

# File lib/bashly/script/command.rb, line 160
def validatables
  @validatables ||= args.select(&:validate) + flags.select(&:validate)
end

Private Instance Methods

implicit_filename() click to toggle source

Returns either a flat filename (docker_status_command.sh) or a nested path (commands/docker/status.sh)

# File lib/bashly/script/command.rb, line 168
def implicit_filename
  if Settings.commands_dir
    "#{Settings.commands_dir}/#{action_name.to_path}.#{Settings.partials_extension}"
  else
    "#{action_name.to_underscore}_command.#{Settings.partials_extension}"
  end
end