Class: Bashly::Script::Command
- Includes:
- Completions::Command
- Defined in:
- lib/bashly/script/command.rb
Instance Attribute Summary collapse
-
#parent_command ⇒ Object
Returns the value of attribute parent_command.
-
#parents ⇒ Object
Returns an array of all parents.
Attributes inherited from Base
Class Method Summary collapse
Instance Method Summary collapse
-
#action_name ⇒ Object
Returns the name to be used as an action.
-
#aliases ⇒ Object
Returns all the possible aliases for this command.
-
#alt ⇒ Object
Returns an array of alternative aliases if any.
-
#args ⇒ Object
Returns an array of Arguments.
-
#caption_string ⇒ Object
Returns a string suitable to be a headline.
-
#catch_all ⇒ Object
Returns an object representing the catch_all configuration.
-
#command_aliases ⇒ Object
Returns a full list of the Command names and aliases combined.
-
#command_help_data ⇒ Object
Returns a data structure for displaying subcommands help.
-
#command_names ⇒ Object
Returns only the names of the Commands.
-
#commands ⇒ Object
Returns an array of the Commands.
-
#deep_commands ⇒ Object
Returns a flat array containing all the commands in this tree.
-
#default_args ⇒ Object
Returns an array of all the default Args.
-
#default_command ⇒ Object
If any of this command’s subcommands has the default option set to true, this default command will be returned, nil otherwise.
-
#default_environment_variables ⇒ Object
Returns an array of all the default Environment Variables.
-
#default_flags ⇒ Object
Returns an array of all the default Flags.
-
#dependencies ⇒ Object
Returns an array of Dependency objects.
-
#environment_variables ⇒ Object
Returns an array of EnvironmentVariable objects.
-
#examples ⇒ Object
Returns an array of examples.
-
#filename ⇒ Object
Returns the filename that is expected to hold the user code for this command.
-
#flags ⇒ Object
Returns an array of Flags.
-
#full_name ⇒ Object
Returns the name of the command, including its parent name (in case this is a subcommand).
-
#function_name ⇒ Object
Returns a unique name, suitable to be used in a bash function.
-
#global_flags? ⇒ Boolean
Returns true if this command’s flags should be considered as gloal flags, and cascade to subcommands.
-
#group_string ⇒ Object
Returns the string for the group caption.
-
#grouped_commands ⇒ Object
Returns subcommands by group.
-
#mode ⇒ Object
Returns a mode identifier.
-
#public_command_aliases ⇒ Object
Returns a full list of the public Command names and aliases combined.
-
#public_commands ⇒ Object
Returns only commands that are not private.
-
#public_environment_variables ⇒ Object
Returns only environment variables that are not private.
-
#public_flags ⇒ Object
Returns only flags that are not private.
-
#repeatable_arg_exist? ⇒ Boolean
Returns true if one of the args is repeatable.
-
#required_args ⇒ Object
Returns an array of all the required Arguments.
-
#required_environment_variables ⇒ Object
Returns an array of all the required EnvironmentVariables.
-
#required_flags ⇒ Object
Returns an array of all the required Flags.
-
#root_command? ⇒ Boolean
Returns true if this is the root command (no parents).
-
#short_flag_exist?(flag) ⇒ Boolean
Returns true if one of the flags matches the provided short code.
-
#summary_string ⇒ Object
Returns the summary string.
-
#usage_string ⇒ Object
Returns a constructed string suitable for Usage pattern.
-
#usage_string_args ⇒ Object
Returns an array of args usage_string for the command’s usage_string.
-
#user_lib ⇒ Object
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.
-
#whitelisted_args ⇒ Object
Returns an array of all the args with a whitelist.
-
#whitelisted_flags ⇒ Object
Returns an array of all the flags with a whitelist arg.
Methods included from Completions::Command
#completion_data, #completion_function, #completion_script
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, #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_command ⇒ Object
Returns the value of attribute parent_command.
18 19 20 |
# File 'lib/bashly/script/command.rb', line 18 def parent_command @parent_command end |
#parents ⇒ Object
Returns an array of all parents. For example, the command “docker container run” will have [docker, container] as its parents
234 235 236 |
# File 'lib/bashly/script/command.rb', line 234 def parents @parents ||= [] end |
Class Method Details
.option_keys ⇒ Object
7 8 9 10 11 12 13 14 15 |
# 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 function group help name private version ] end |
Instance Method Details
#action_name ⇒ Object
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”.
26 27 28 |
# File 'lib/bashly/script/command.rb', line 26 def action_name parents.any? ? (parents[1..] + [name]).join(' ') : 'root' end |
#aliases ⇒ Object
Returns all the possible aliases for this command
31 32 33 |
# File 'lib/bashly/script/command.rb', line 31 def aliases [name] + alt end |
#alt ⇒ Object
Returns an array of alternative aliases if any
36 37 38 39 40 |
# File 'lib/bashly/script/command.rb', line 36 def alt return [] unless ['alias'] ['alias'].is_a?(String) ? [['alias']] : ['alias'] end |
#args ⇒ Object
Returns an array of Arguments
43 44 45 46 47 48 49 |
# File 'lib/bashly/script/command.rb', line 43 def args return [] unless ['args'] ['args'].map do || Argument.new end end |
#caption_string ⇒ Object
Returns a string suitable to be a headline
52 53 54 |
# File 'lib/bashly/script/command.rb', line 52 def caption_string help.empty? ? full_name : "#{full_name} - #{summary}" end |
#catch_all ⇒ Object
Returns an object representing the catch_all configuration
57 58 59 |
# File 'lib/bashly/script/command.rb', line 57 def catch_all @catch_all ||= CatchAll.from_config ['catch_all'] end |
#command_aliases ⇒ Object
Returns a full list of the Command names and aliases combined
62 63 64 |
# File 'lib/bashly/script/command.rb', line 62 def command_aliases commands.map(&:aliases).flatten end |
#command_help_data ⇒ Object
Returns a data structure for displaying subcommands help
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 |
# 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 ⇒ Object
Returns only the names of the Commands
87 88 89 |
# File 'lib/bashly/script/command.rb', line 87 def command_names commands.map(&:name) end |
#commands ⇒ Object
Returns an array of the Commands
92 93 94 95 96 97 98 99 100 101 |
# File 'lib/bashly/script/command.rb', line 92 def commands return [] unless ['commands'] ['commands'].map do || result = Command.new result.parents = parents + [name] result.parent_command = self result end end |
#deep_commands ⇒ Object
Returns a flat array containing all the commands in this tree. This includes self + children + grandchildres + …
105 106 107 108 109 110 111 112 113 114 |
# 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 ⇒ Object
Returns an array of all the default Args
123 124 125 |
# File 'lib/bashly/script/command.rb', line 123 def default_args args.select(&:default) end |
#default_command ⇒ Object
If any of this command’s subcommands has the default option set to true, this default command will be returned, nil otherwise.
118 119 120 |
# File 'lib/bashly/script/command.rb', line 118 def default_command commands.find(&:default) end |
#default_environment_variables ⇒ Object
Returns an array of all the default Environment Variables
128 129 130 |
# File 'lib/bashly/script/command.rb', line 128 def default_environment_variables environment_variables.select(&:default) end |
#default_flags ⇒ Object
Returns an array of all the default Flags
133 134 135 |
# File 'lib/bashly/script/command.rb', line 133 def default_flags flags.select(&:default) end |
#dependencies ⇒ Object
Returns an array of Dependency objects
138 139 140 141 142 143 144 |
# File 'lib/bashly/script/command.rb', line 138 def dependencies return [] unless ['dependencies'] @dependencies ||= ['dependencies'].map do |key, value| Dependency.from_config key, value end end |
#environment_variables ⇒ Object
Returns an array of EnvironmentVariable objects
147 148 149 150 151 152 153 |
# File 'lib/bashly/script/command.rb', line 147 def environment_variables return [] unless ['environment_variables'] ['environment_variables'].map do || EnvironmentVariable.new end end |
#examples ⇒ Object
Returns an array of examples
156 157 158 159 160 |
# File 'lib/bashly/script/command.rb', line 156 def examples return nil unless ['examples'] ['examples'].is_a?(Array) ? ['examples'] : [['examples']] end |
#filename ⇒ Object
Returns the filename that is expected to hold the user code for this command
164 165 166 |
# File 'lib/bashly/script/command.rb', line 164 def filename ['filename'] || implicit_filename end |
#flags ⇒ Object
Returns an array of Flags
169 170 171 172 173 174 175 |
# File 'lib/bashly/script/command.rb', line 169 def flags return [] unless ['flags'] ['flags'].map do || Flag.new end end |
#full_name ⇒ Object
Returns the name of the command, including its parent name (in case this is a subcommand)
184 185 186 |
# File 'lib/bashly/script/command.rb', line 184 def full_name parents.any? ? (parents + [name]).join(' ') : name end |
#function_name ⇒ Object
Returns a unique name, suitable to be used in a bash function
178 179 180 |
# File 'lib/bashly/script/command.rb', line 178 def function_name ['function'] || full_name.to_underscore end |
#global_flags? ⇒ Boolean
Returns true if this command’s flags should be considered as gloal flags, and cascade to subcommands
190 191 192 |
# File 'lib/bashly/script/command.rb', line 190 def global_flags? flags.any? and commands.any? end |
#group_string ⇒ Object
Returns the string for the group caption
195 196 197 198 199 200 201 |
# File 'lib/bashly/script/command.rb', line 195 def group_string if group strings[:group] % { group: group } else strings[:commands] end end |
#grouped_commands ⇒ Object
Returns subcommands by group
204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 |
# File 'lib/bashly/script/command.rb', line 204 def grouped_commands result = {} public_commands.each do |command| result[command.group_string] ||= [] result[command.group_string] << command next unless command.expose command.public_commands.each do |subcommand| result[command.group_string] << subcommand end end result end |
#mode ⇒ Object
Returns a mode identifier
221 222 223 224 225 226 227 228 229 230 |
# File 'lib/bashly/script/command.rb', line 221 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 |
#public_command_aliases ⇒ Object
Returns a full list of the public Command names and aliases combined
244 245 246 |
# File 'lib/bashly/script/command.rb', line 244 def public_command_aliases public_commands.map(&:aliases).flatten end |
#public_commands ⇒ Object
Returns only commands that are not private
239 240 241 |
# File 'lib/bashly/script/command.rb', line 239 def public_commands commands.reject(&:private) end |
#public_environment_variables ⇒ Object
Returns only environment variables that are not private
249 250 251 |
# File 'lib/bashly/script/command.rb', line 249 def public_environment_variables environment_variables.reject(&:private) end |
#public_flags ⇒ Object
Returns only flags that are not private
254 255 256 |
# File 'lib/bashly/script/command.rb', line 254 def public_flags flags.reject(&:private) end |
#repeatable_arg_exist? ⇒ Boolean
Returns true if one of the args is repeatable
259 260 261 |
# File 'lib/bashly/script/command.rb', line 259 def repeatable_arg_exist? args.any?(&:repeatable) end |
#required_args ⇒ Object
Returns an array of all the required Arguments
264 265 266 |
# File 'lib/bashly/script/command.rb', line 264 def required_args args.select(&:required) end |
#required_environment_variables ⇒ Object
Returns an array of all the required EnvironmentVariables
269 270 271 |
# File 'lib/bashly/script/command.rb', line 269 def required_environment_variables environment_variables.select(&:required) end |
#required_flags ⇒ Object
Returns an array of all the required Flags
274 275 276 |
# File 'lib/bashly/script/command.rb', line 274 def required_flags flags.select(&:required) end |
#root_command? ⇒ Boolean
Returns true if this is the root command (no parents)
279 280 281 |
# File 'lib/bashly/script/command.rb', line 279 def root_command? parents.empty? end |
#short_flag_exist?(flag) ⇒ Boolean
Returns true if one of the flags matches the provided short code
284 285 286 |
# File 'lib/bashly/script/command.rb', line 284 def short_flag_exist?(flag) flags.any? { |f| f.short == flag } end |
#summary_string ⇒ Object
Returns the summary string
289 290 291 292 293 294 295 |
# File 'lib/bashly/script/command.rb', line 289 def summary_string if default strings[:default_command_summary] % { summary: summary } else summary end end |
#usage_string ⇒ Object
Returns a constructed string suitable for Usage pattern
298 299 300 301 302 303 304 305 306 307 308 309 310 311 |
# File 'lib/bashly/script/command.rb', line 298 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 ⇒ Object
Returns an array of args usage_string for the command’s usage_string
314 315 316 |
# File 'lib/bashly/script/command.rb', line 314 def usage_string_args args.map(&:usage_string) end |
#user_lib ⇒ Object
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
321 322 323 |
# File 'lib/bashly/script/command.rb', line 321 def user_lib @user_lib ||= Dir["#{Settings.full_lib_dir}/**/*.#{Settings.partials_extension}"] end |
#whitelisted_args ⇒ Object
Returns an array of all the args with a whitelist
326 327 328 |
# File 'lib/bashly/script/command.rb', line 326 def whitelisted_args args.select(&:allowed) end |
#whitelisted_flags ⇒ Object
Returns an array of all the flags with a whitelist arg
331 332 333 |
# File 'lib/bashly/script/command.rb', line 331 def whitelisted_flags flags.select(&:allowed) end |