module Bovem::CommandMethods::Children
Methods to manage options and subcommands.
Attributes
Public Instance Methods
Adds a new argument to this command.
@param value [String] The argument to add.
# File lib/bovem/command.rb, line 217 def argument(value) @args ||= [] @args << value end
Returns the list of arguments of this command.
@return [Array] The list of arguments of this command.
# File lib/bovem/command.rb, line 225 def arguments @args || [] end
Clear all subcommands of this commands.
@return [Hash] The new (empty) list of subcommands of this command.
# File lib/bovem/command.rb, line 183 def clear_commands @commands = {} end
Clear all the options of this commands. @return [Hash] The new (empty) list of the options of this command.
# File lib/bovem/command.rb, line 203 def clear_options @options = {} end
Adds a new subcommand to this command.
@param name [String] The name of this command. Must be unique. @param options [Hash] A set of options for this command. @return [Command] The newly added command.
# File lib/bovem/command.rb, line 141 def command(name, options = {}, &block) @commands ||= HashWithIndifferentAccess.new options = {name: name.to_s, parent: self, application: application}.merge(options.ensure_hash) raise Bovem::Errors::Error.new(self, :duplicate_command, i18n.existing_command(full_name(name))) if @commands[name.to_s] create_command(name, options, &block) end
Check if this command has subcommands.
@return [Boolean] `true` if this command has subcommands, `false` otherwise.
# File lib/bovem/command.rb, line 190 def commands? !commands.empty? end
Get the list of the options of this command as an hash, where the keys are the options and the values are either the user inputs or the defaults values.
If the two prefixes collides, the command options take precedence over application options.
@param unprovided [Boolean] If to include also options that were not provided by the user and that don't have any default value. @param application [String] The prefix to use for including application's options. If falsy, only current command options will be included. @param prefix [String] The prefix to add to the option of this command. @param whitelist [Array] The list of options to include. By default all options are included. @return [HashWithIndifferentAccess] The requested options.
# File lib/bovem/command.rb, line 239 def get_options(unprovided: false, application: "application_", prefix: "", whitelist: []) rv = HashWithIndifferentAccess.new if application && !application? rv.merge!(self.application.get_options(unprovided: unprovided, application: nil, prefix: application, whitelist: whitelist)) end rv.merge!(get_current_options(unprovided, prefix, whitelist)) rv end
Adds a new option to this command.
@see Option#initialize
@param name [String] The name of the option. Must be unique. @param forms [Array] An array of short and long forms for this option. @param options [Hash] The settings for the option. @param action [Proc] An optional action to pass to the option. @return [Option] The newly added option.
# File lib/bovem/command.rb, line 159 def option(name, forms = [], options = {}, &action) name = name.ensure_string @options ||= HashWithIndifferentAccess.new if @options[name] raise Bovem::Errors::Error.new(self, :duplicate_option, application? ? i18n.existing_option_global(name) : i18n.existing_option(name, full_name)) end option = Bovem::Option.new(name, forms, options, &action) option.parent = self @options[name] = option option end
Check if this command has options.
@return [Boolean] `true` if this command has options, `false` otherwise.
# File lib/bovem/command.rb, line 210 def options? !options.empty? end