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 7
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 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 26
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 31
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 36
def alt
  return [] unless options['alias']

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

Returns an array of Arguments

# File lib/bashly/script/command.rb, line 43
def args
  return [] unless options['args']

  options['args'].map do |options|
    Argument.new options
  end
end
caption_string() click to toggle source

Returns a string suitable to be a headline

# File lib/bashly/script/command.rb, line 52
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 57
def catch_all
  @catch_all ||= CatchAll.from_config options['catch_all']
end
command_aliases() click to toggle source

Returns a full list of the Command names and aliases combined

# File lib/bashly/script/command.rb, line 62
def command_aliases
  commands.map(&:aliases).flatten
end
command_help_data() click to toggle source

Returns a data structure for displaying subcommands help

# File lib/bashly/script/command.rb, line 67
def command_help_data
  result = {}

  public_commands.each do |command|
    result[command.group_string] ||= {}
    result[command.group_string][command.name] = { summary: command.summary_string }
    next unless command.expose

    command.public_commands.each do |subcommand|
      result[command.group_string]["#{command.name} #{subcommand.name}"] = {
        summary:   subcommand.summary_string,
        help_only: command.expose != 'always',
      }
    end
  end

  result
end
command_names() click to toggle source

Returns only the names of the Commands

# File lib/bashly/script/command.rb, line 87
def command_names
  commands.map(&:name)
end
commands() click to toggle source

Returns an array of the Commands

# File lib/bashly/script/command.rb, line 92
def commands
  return [] unless options['commands']

  options['commands'].map do |options|
    result = Command.new options
    result.parents = parents + [name]
    result.parent_command = self
    result
  end
end
deep_commands() click to toggle source

Returns a flat array containing all the commands in this tree. This includes self + children + grandchildres + …

# File lib/bashly/script/command.rb, line 105
def deep_commands
  result = []
  commands.each do |command|
    result << command
    if command.commands.any?
      result += command.deep_commands
    end
  end
  result
end
default_args() click to toggle source

Returns an array of all the default Args

# File lib/bashly/script/command.rb, line 123
def default_args
  args.select(&:default)
end
default_command() click to toggle source

If any of this command’s subcommands has the default option set to true, this default command will be returned, nil otherwise.

# File lib/bashly/script/command.rb, line 118
def default_command
  commands.find(&:default)
end
default_environment_variables() click to toggle source

Returns an array of all the default Environment Variables

# File lib/bashly/script/command.rb, line 128
def default_environment_variables
  environment_variables.select(&:default)
end
default_flags() click to toggle source

Returns an array of all the default Flags

# File lib/bashly/script/command.rb, line 133
def default_flags
  flags.select(&:default)
end
dependencies() click to toggle source

Returns an array of Dependency objects

# File lib/bashly/script/command.rb, line 138
def dependencies
  return [] unless options['dependencies']

  @dependencies ||= options['dependencies'].map do |key, value|
    Dependency.from_config key, value
  end
end
environment_variables() click to toggle source

Returns an array of EnvironmentVariable objects

# File lib/bashly/script/command.rb, line 147
def environment_variables
  return [] unless options['environment_variables']

  options['environment_variables'].map do |options|
    EnvironmentVariable.new options
  end
end
examples() click to toggle source

Returns an array of examples

# File lib/bashly/script/command.rb, line 156
def examples
  return nil unless options['examples']

  options['examples'].is_a?(Array) ? options['examples'] : [options['examples']]
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 164
def filename
  options['filename'] || implicit_filename
end
flags() click to toggle source

Returns an array of Flags

# File lib/bashly/script/command.rb, line 169
def flags
  return [] unless options['flags']

  options['flags'].map do |options|
    Flag.new options
  end
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 184
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 178
def function_name
  options['function'] || full_name.to_underscore
end
global_flags?() click to toggle source

Returns true if this command’s flags should be considered as gloal flags, and cascade to subcommands

# File lib/bashly/script/command.rb, line 190
def global_flags?
  flags.any? and commands.any?
end
group_string() click to toggle source

Returns the string for the group caption

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

Returns a mode identifier

# File lib/bashly/script/command.rb, line 204
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 217
def parents
  @parents ||= []
end
public_command_aliases() click to toggle source

Returns a full list of the public Command names and aliases combined

# File lib/bashly/script/command.rb, line 227
def public_command_aliases
  public_commands.map(&:aliases).flatten
end
public_commands() click to toggle source

Returns only commands that are not private

# File lib/bashly/script/command.rb, line 222
def public_commands
  commands.reject(&:private)
end
public_environment_variables() click to toggle source

Returns only environment variables that are not private

# File lib/bashly/script/command.rb, line 232
def public_environment_variables
  environment_variables.reject(&:private)
end
public_flags() click to toggle source

Returns only flags that are not private

# File lib/bashly/script/command.rb, line 237
def public_flags
  flags.reject(&:private)
end
repeatable_arg_exist?() click to toggle source

Returns true if one of the args is repeatable

# File lib/bashly/script/command.rb, line 242
def repeatable_arg_exist?
  args.any?(&:repeatable)
end
required_args() click to toggle source

Returns an array of all the required Arguments

# File lib/bashly/script/command.rb, line 247
def required_args
  args.select(&:required)
end
required_environment_variables() click to toggle source

Returns an array of all the required EnvironmentVariables

# File lib/bashly/script/command.rb, line 252
def required_environment_variables
  environment_variables.select(&:required)
end
required_flags() click to toggle source

Returns an array of all the required Flags

# File lib/bashly/script/command.rb, line 257
def required_flags
  flags.select(&:required)
end
root_command?() click to toggle source

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

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

Returns true if one of the flags matches the provided short code

# File lib/bashly/script/command.rb, line 267
def short_flag_exist?(flag)
  flags.any? { |f| f.short == flag }
end
summary_string() click to toggle source

Returns the summary string

# File lib/bashly/script/command.rb, line 272
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 281
def usage_string
  result = [full_name]

  result.push case mode
  when :global_flags    then ['[OPTIONS]', 'COMMAND']
  when :commands        then ['COMMAND']
  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
usage_string_args() click to toggle source

Returns an array of args usage_string for the command’s usage_string

# File lib/bashly/script/command.rb, line 297
def usage_string_args
  args.map(&:usage_string)
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 304
def user_lib
  @user_lib ||= Dir["#{Settings.full_lib_dir}/**/*.#{Settings.partials_extension}"]
end
whitelisted_args() click to toggle source

Returns an array of all the args with a whitelist

# File lib/bashly/script/command.rb, line 309
def whitelisted_args
  args.select(&:allowed)
end
whitelisted_flags() click to toggle source

Returns an array of all the flags with a whitelist arg

# File lib/bashly/script/command.rb, line 314
def whitelisted_flags
  flags.select(&:allowed)
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 322
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