class Toys::StandardCLI

Subclass of `Toys::CLI` configured for the behavior of the standard Toys executable.

Constants

CONFIG_DIR_NAME

Standard toys configuration directory name. @return [String]

CONFIG_FILE_NAME

Standard toys configuration file name. @return [String]

DATA_DIR_NAME

Standard data directory name in a toys configuration. @return [String]

DEFAULT_ROOT_DESC

Short description for the standard root tool. @return [String]

DEFAULT_ROOT_LONG_DESC

Help text for the standard root tool. @return [String]

DEFAULT_VERSION_FLAG_DESC

Short description for the version flag. @return [String]

EXECUTABLE_NAME

Name of the standard toys executable. @return [String]

EXTRA_DELIMITERS

Delimiter characters recognized. @return [String]

INDEX_FILE_NAME

Standard index file name in a toys configuration. @return [String]

LIB_DIR_NAME

Standard lib directory name in a toys configuration. @return [String]

PRELOAD_DIR_NAME

Standard preload directory name in a toys configuration. @return [String]

PRELOAD_FILE_NAME

Standard preload file name in a toys configuration. @return [String]

TOYS_PATH_ENV

Name of the toys path environment variable. @return [String]

Public Class Methods

new(cur_dir: nil) click to toggle source

Create a standard CLI, configured with the appropriate paths and middleware.

@param cur_dir [String,nil] Starting search directory for configs.

Defaults to the current working directory.
Calls superclass method
# File lib/toys/standard_cli.rb, line 99
def initialize(cur_dir: nil)
  super(
    executable_name: EXECUTABLE_NAME,
    config_dir_name: CONFIG_DIR_NAME,
    config_file_name: CONFIG_FILE_NAME,
    index_file_name: INDEX_FILE_NAME,
    preload_file_name: PRELOAD_FILE_NAME,
    preload_dir_name: PRELOAD_DIR_NAME,
    data_dir_name: DATA_DIR_NAME,
    lib_dir_name: LIB_DIR_NAME,
    extra_delimiters: EXTRA_DELIMITERS,
    middleware_stack: default_middleware_stack,
    template_lookup: default_template_lookup
  )
  add_standard_paths(cur_dir: cur_dir, toys_dir_name: CONFIG_DIR_NAME)
end

Private Instance Methods

add_standard_paths(cur_dir: nil, global_dirs: nil, toys_dir_name: nil) click to toggle source

Add paths for a toys standard CLI. Paths added, in order from high to low priority, are:

*  Search the current directory and all ancestors for config files and
   directories.
*  Read the `TOYS_PATH` environment variable and search for config files
   and directories in the given paths. If this variable is empty, use
   `$HOME:/etc` by default.
*  The builtins for the standard toys executable.

@param cur_dir [String,nil] Starting search directory for configs.

Defaults to the current working directory.

@param global_dirs [Array<String>,nil] Optional list of global

directories, or `nil` to use the defaults.

@return [self]

# File lib/toys/standard_cli.rb, line 135
def add_standard_paths(cur_dir: nil, global_dirs: nil, toys_dir_name: nil)
  cur_dir ||= ::Dir.pwd
  cur_dir = skip_toys_dir(cur_dir, toys_dir_name) if toys_dir_name
  global_dirs ||= default_global_dirs
  add_search_path_hierarchy(start: cur_dir, terminate: global_dirs)
  global_dirs.each { |path| add_search_path(path) }
  builtins_path = ::File.join(::File.dirname(::File.dirname(__dir__)), "builtins")
  add_config_path(builtins_path, source_name: "(builtin tools)", context_directory: nil)
  self
end
default_global_dirs() click to toggle source

Returns the default set of global config directories.

@return [Array<String>]

# File lib/toys/standard_cli.rb, line 196
def default_global_dirs
  paths = ::ENV[TOYS_PATH_ENV].to_s.split(::File::PATH_SEPARATOR)
  paths = [::Dir.home, "/etc"] if paths.empty?
  paths
    .compact
    .uniq
    .select { |path| ::File.directory?(path) && ::File.readable?(path) }
    .map { |path| ::File.realpath(::File.expand_path(path)) }
end
default_middleware_stack() click to toggle source

Returns the middleware for the standard Toys CLI.

@return [Array]

# File lib/toys/standard_cli.rb, line 165
def default_middleware_stack
  [
    Middleware.spec(:set_default_descriptions,
                    default_root_desc: DEFAULT_ROOT_DESC,
                    default_root_long_desc: DEFAULT_ROOT_LONG_DESC),
    Middleware.spec(:show_help,
                    help_flags: true,
                    usage_flags: true,
                    list_flags: true,
                    recursive_flags: true,
                    search_flags: true,
                    show_all_subtools_flags: true,
                    default_recursive: true,
                    allow_root_args: true,
                    show_source_path: true,
                    separate_sources: true,
                    use_less: true,
                    fallback_execution: true),
    Middleware.spec(:show_root_version,
                    version_string: ::Toys::VERSION,
                    version_flag_desc: DEFAULT_VERSION_FLAG_DESC),
    Middleware.spec(:handle_usage_errors),
    Middleware.spec(:add_verbosity_flags),
  ]
end
default_template_lookup() click to toggle source

Returns a ModuleLookup for the default templates.

@return [Toys::ModuleLookup]

# File lib/toys/standard_cli.rb, line 211
def default_template_lookup
  ModuleLookup.new.add_path("toys/templates")
end
skip_toys_dir(dir, toys_dir_name) click to toggle source

Step out of any toys dir

# File lib/toys/standard_cli.rb, line 147
def skip_toys_dir(dir, toys_dir_name)
  cur_dir = dir
  loop do
    parent = ::File.dirname(dir)
    return cur_dir if parent == dir
    if ::File.basename(dir) == toys_dir_name
      cur_dir = dir = parent
    else
      dir = parent
    end
  end
end