Module: Bovem::CommandMethods::Children

Included in:
Bovem::Command
Defined in:
lib/bovem/command.rb

Overview

Methods to manage options and subcommands.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#commandsHashWithIndifferentAccess (readonly)

Returns the list of subcommands of this command.

Returns:

  • (HashWithIndifferentAccess)

    The list of subcommands of this command.



176
177
178
# File 'lib/bovem/command.rb', line 176

def commands
  @commands
end

#optionsHashWithIndifferentAccess (readonly)

Returns the list of options of this command.

Returns:

  • (HashWithIndifferentAccess)

    The list of options of this command.



197
198
199
# File 'lib/bovem/command.rb', line 197

def options
  @options
end

Instance Method Details

#argument(value) ⇒ Object

Adds a new argument to this command.

Parameters:

  • value (String)

    The argument to add.



217
218
219
220
# File 'lib/bovem/command.rb', line 217

def argument(value)
  @args ||= []
  @args << value
end

#argumentsArray

Returns the list of arguments of this command.

Returns:

  • (Array)

    The list of arguments of this command.



225
226
227
# File 'lib/bovem/command.rb', line 225

def arguments
  @args || []
end

#clear_commandsHash

Clear all subcommands of this commands.

Returns:

  • (Hash)

    The new (empty) list of subcommands of this command.



183
184
185
# File 'lib/bovem/command.rb', line 183

def clear_commands
  @commands = {}
end

#clear_optionsHash

Clear all the options of this commands.

Returns:

  • (Hash)

    The new (empty) list of the options of this command.



203
204
205
# File 'lib/bovem/command.rb', line 203

def clear_options
  @options = {}
end

#command(name, options = {}, &block) ⇒ Command

Adds a new subcommand to this command.

Parameters:

  • name (String)

    The name of this command. Must be unique.

  • options (Hash) (defaults to: {})

    A set of options for this command.

Returns:

  • (Command)

    The newly added command.

Raises:



141
142
143
144
145
146
147
148
# 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

#commands?Boolean

Check if this command has subcommands.

Returns:

  • (Boolean)

    true if this command has subcommands, false otherwise.



190
191
192
# File 'lib/bovem/command.rb', line 190

def commands?
  !commands.empty?
end

#get_options(unprovided: false, application: "application_", prefix: "", whitelist: []) ⇒ HashWithIndifferentAccess

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.

Parameters:

  • unprovided (Boolean) (defaults to: false)

    If to include also options that were not provided by the user and that don't have any default value.

  • application (String) (defaults to: "application_")

    The prefix to use for including application's options. If falsy, only current command options will be included.

  • prefix (String) (defaults to: "")

    The prefix to add to the option of this command.

  • whitelist (Array) (defaults to: [])

    The list of options to include. By default all options are included.

Returns:

  • (HashWithIndifferentAccess)

    The requested options.



239
240
241
242
243
244
245
246
247
248
# 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

#option(name, forms = [], options = {}, &action) ⇒ Option

Adds a new option to this command.

Parameters:

  • name (String)

    The name of the option. Must be unique.

  • forms (Array) (defaults to: [])

    An array of short and long forms for this option.

  • options (Hash) (defaults to: {})

    The settings for the option.

  • action (Proc)

    An optional action to pass to the option.

Returns:

  • (Option)

    The newly added option.

See Also:



159
160
161
162
163
164
165
166
167
168
169
170
171
# 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

#options?Boolean

Check if this command has options.

Returns:

  • (Boolean)

    true if this command has options, false otherwise.



210
211
212
# File 'lib/bovem/command.rb', line 210

def options?
  !options.empty?
end